Skip to content

Commit

Permalink
Key prefix is now optional.
Browse files Browse the repository at this point in the history
  • Loading branch information
tombatron committed Feb 16, 2023
1 parent 0cc520b commit 23bc3be
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 8 deletions.
93 changes: 91 additions & 2 deletions RediSearchClient.IntegrationTests/CreateIndex.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using RediSearchClient.Indexes;
using System;
using System.Threading.Tasks;
using Xunit;

Expand Down Expand Up @@ -46,7 +45,7 @@ public async Task WillCreateAJsonIndex()

Assert.Contains(indexName, indexes);
}

[Fact]
public async Task WillCreateAJsonIndexAsync()
{
Expand All @@ -61,6 +60,62 @@ public async Task WillCreateAJsonIndexAsync()
Assert.Contains(indexName, indexes);
}

[Fact]
public void WillCreateAnIndexWithoutPrefix()
{
var indexDefinition = GetIndexDefinitionWithNoPrefix();

var indexName = $"index_{_indexName}_noprefix";

_db.CreateIndex(indexName, indexDefinition);

var indexes = _db.ListIndexes();

Assert.Contains(indexName, indexes);
}

[Fact]
public async Task WillCreateAnIndexWithoutPrefixAsync()
{
var indexDefinition = GetIndexDefinitionWithNoPrefix();

var indexName = $"index_{_indexName}_noprefix_async";

await _db.CreateIndexAsync(indexName, indexDefinition);

var indexes = await _db.ListIndexesAsync();

Assert.Contains(indexName, indexes);
}

[Fact]
public void WillCreateAJsonIndexWithoutPrefix()
{
var indexDefinition = GetJsonIndexDefinitionWithNoPrefix();

var indexName = $"json_index_{_indexName}_noprefix";

_db.CreateIndex(indexName, indexDefinition);

var indexes = _db.ListIndexes();

Assert.Contains(indexName, indexes);
}

[Fact]
public async Task WillCreateAJsonIndexWithoutPrefixAsync()
{
var indexDefinition = GetJsonIndexDefinitionWithNoPrefix();

var indexName = $"json_index_{_indexName}_noprefix_async";

await _db.CreateIndexAsync(indexName, indexDefinition);

var indexes = await _db.ListIndexesAsync();

Assert.Contains(indexName, indexes);
}

private RediSearchIndexDefinition GetIndexDefinition()
{
return RediSearchIndex
Expand All @@ -82,6 +137,26 @@ private RediSearchIndexDefinition GetIndexDefinition()
.Build();
}

private RediSearchIndexDefinition GetIndexDefinitionWithNoPrefix()
{
return RediSearchIndex
.OnHash()
.UsingFilter("@State=='FL'")
.UsingLanguage("English")
.SetScore(0.5)
.Temporary(600)
.NoHighLights()
.WithSchema(
x => x.Text("ZipCode", sortable: false, nostem: true),
x => x.Text("City", sortable: true),
x => x.Text("State", sortable: true, nostem: true),
x => x.Geo("Coordinates"),
x => x.Numeric("TimeZoneOffset"),
x => x.Numeric("DaylightSavingsFlag")
)
.Build();
}

private RediSearchIndexDefinition GetJsonIndexDefinition()
{
return RediSearchIndex
Expand All @@ -96,5 +171,19 @@ private RediSearchIndexDefinition GetJsonIndexDefinition()
)
.Build();
}

private RediSearchIndexDefinition GetJsonIndexDefinitionWithNoPrefix()
{
return RediSearchIndex
.OnJson()
.WithSchema(
x => x.Text("$.Id", "Id"),
x => x.Text("$.FirstName", "FirstName", sortable: true),
x => x.Text("$.Surname", "LastName", sortable: true),
x => x.Numeric("$.BornSeconds", "Born", sortable: true),
x => x.Numeric("$.DiedSeconds", "Died", sortable: true)
)
.Build();
}
}
}
30 changes: 30 additions & 0 deletions RediSearchClient.Tests/Indexes/RediSearchIndexTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Xunit;
using RediSearchClient.Indexes;

namespace RediSearchClient.Tests.Indexes
{
public class RediSearchIndexTests
{
public class JsonIndexes
{
[Fact]
public void CanBeCreatedWithoutAPrefix()
{
var index = RediSearchIndex.OnJson().WithSchema(x => x.Text("$.Testing", "Testing")).Build();

Assert.NotNull(index);
}
}

public class HashIndexes
{
[Fact]
public void CanBeCreatedWithoutAPrefix()
{
var index = RediSearchIndex.OnHash().WithSchema(x => x.Text("Testing")).Build();

Assert.NotNull(index);
}
}
}
}
19 changes: 13 additions & 6 deletions RediSearchClient/Indexes/BaseRediSearchIndexBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ internal BaseRediSearchIndexBuilder()

private List<string> _prefixes;

private bool HasPrefixes() => _prefixes?.Any() ?? false;

/// <summary>
/// Builder method for defining the key pattern to index.
/// </summary>
Expand Down Expand Up @@ -241,7 +243,8 @@ public BaseRediSearchIndexBuilder<TFieldBuilder> WithSchema(
/// <returns></returns>
public BaseRediSearchIndexBuilder<TFieldBuilder> WithSchema(IRediSearchSchemaField[] fields)
{
_fields = fields.Select(x => {
_fields = fields.Select(x =>
{
IRediSearchSchemaField func(TFieldBuilder y) => x;
return (Func<TFieldBuilder, IRediSearchSchemaField>)func;
}).ToArray();
Expand All @@ -259,7 +262,7 @@ public RediSearchIndexDefinition Build()
{
var argumentLength = 2; // ON {structure}

argumentLength += 2 + _prefixes.Count; // [PREFIX {count} {prefix} [{prefix} ..]
argumentLength += HasPrefixes() ? 2 + _prefixes.Count : 0; // [PREFIX {count} {prefix} [{prefix} ..]
argumentLength += string.IsNullOrEmpty(_filter) ? 0 : 2; // [FILTER {filter}]
argumentLength += string.IsNullOrEmpty(_language) ? 0 : 2; // [LANGUAGE {default_lang}]
argumentLength += string.IsNullOrEmpty(_languageField) ? 0 : 2; // [LANGUAGE_FIELD {lang_field}]
Expand Down Expand Up @@ -293,12 +296,16 @@ public RediSearchIndexDefinition Build()
result[++currentArgumentIndex] = ResolveStructure();

// [PREFIX {count} {prefix} [{prefix} ..]
result[++currentArgumentIndex] = "PREFIX";
result[++currentArgumentIndex] = _prefixes.Count;

foreach (var prefix in _prefixes)
if (HasPrefixes())
{
result[++currentArgumentIndex] = prefix;
result[++currentArgumentIndex] = "PREFIX";
result[++currentArgumentIndex] = _prefixes.Count;

foreach (var prefix in _prefixes)
{
result[++currentArgumentIndex] = prefix;
}
}

// [FILTER {filter}]
Expand Down

0 comments on commit 23bc3be

Please sign in to comment.