Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] default name without attribute for json module #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/NRediSearch/FieldName.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;

namespace NRediSearch
{
Expand All @@ -12,7 +12,10 @@ public FieldName(string name) : this(name, null) {

public FieldName(string name, string attribute) {
this.name = name;
this.attribute = attribute;
if (attribute == null && name.StartsWith("$."))
this.attribute = name.Substring(2);
else
this.attribute = attribute;
}

public int AddCommandArguments(List<object> args) {
Expand Down
77 changes: 76 additions & 1 deletion tests/NRediSearch.Tests/ClientTests/ClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using static NRediSearch.Client;
using static NRediSearch.Schema;
using static NRediSearch.SuggestionOptions;

using System.Threading.Tasks;

namespace NRediSearch.Tests.ClientTests
{
Expand Down Expand Up @@ -1156,5 +1156,80 @@ public void TestJsonIndex()
Assert.Equal("king:1", res.Documents[0].Id);
Assert.Equal("{\"name\":\"henry\"}", res.Documents[0]["json"]);
}

[Fact]
public async Task TestJsonSearchProp()
{
if (getModuleSearchVersion() <= 20200)
{
return;
}

Client cl = GetClient();
IndexDefinition defenition = new IndexDefinition(prefixes: new string[] { "hero:" }, type: IndexDefinition.IndexType.Json);
Schema sc = new Schema()
.AddSortableTextField("$.name")
.AddTextField("$.desc")
.AddSortableTextField("$.address.city");
Assert.True(await cl.CreateIndexAsync(sc, new ConfiguredIndexOptions(defenition)));

await Db.ExecuteAsync("JSON.SET", "hero:1", ".", "{\"name\":\"harry\",\"lastName\":\"potter\",\"desc\":\"It's going to shock you\",\"email\":\"[email protected]\",\"address\":{\"city\":\"London\",\"street\":\"Privet Drive\",\"house\":\"5\"}}");
await Db.ExecuteAsync("JSON.SET", "hero:2", ".", "{\"name\":\"GEORGE\",\"lastName\":\"WEASLEY\",\"desc\":\"go home\",\"email\":\"[email protected]\"}");
await Db.ExecuteAsync("JSON.SET", "hero:3", ".", "{\"name\":\"FRED\",\"lastName\":\"WEASLEY\",\"desc\":\"live home\",\"email\":\"[email protected]\"}");

// Query
SearchResult res = cl.Search(new Query("@desc:go"));
Assert.Equal(2, res.TotalResults);
}

[Fact]
public async Task TestJsonSearchPropWithAttribute()
{
if (getModuleSearchVersion() <= 20200)
{
return;
}

Client cl = GetClient();
IndexDefinition defenition = new IndexDefinition(prefixes: new string[] { "hero:" }, type: IndexDefinition.IndexType.Json);
Schema sc = new Schema()
.AddSortableTextField("$.name")
.AddTextField(FieldName.Of("$.desc").As("description"))
.AddSortableTextField("$.address.city");
Assert.True(await cl.CreateIndexAsync(sc, new ConfiguredIndexOptions(defenition)));

await Db.ExecuteAsync("JSON.SET", "hero:1", ".", "{\"name\":\"harry\",\"lastName\":\"potter\",\"desc\":\"It's going to shock you\",\"email\":\"[email protected]\",\"address\":{\"city\":\"London\",\"street\":\"Privet Drive\",\"house\":\"5\"}}");
await Db.ExecuteAsync("JSON.SET", "hero:2", ".", "{\"name\":\"GEORGE\",\"lastName\":\"WEASLEY\",\"desc\":\"go home\",\"email\":\"[email protected]\"}");
await Db.ExecuteAsync("JSON.SET", "hero:3", ".", "{\"name\":\"FRED\",\"lastName\":\"WEASLEY\",\"desc\":\"live home\",\"email\":\"[email protected]\"}");

// Query
SearchResult res = cl.Search(new Query("@description:go"));
Assert.Equal(2, res.TotalResults);
}

[Fact]
public async Task TestJsonSearch()
{
if (getModuleSearchVersion() <= 20200)
{
return;
}

Client cl = GetClient();
IndexDefinition defenition = new IndexDefinition(prefixes: new string[] { "hero:" }, type: IndexDefinition.IndexType.Json);
Schema sc = new Schema()
.AddSortableTextField("$.name")
.AddTextField("$.desc")
.AddSortableTextField("$.address.city");
Assert.True(await cl.CreateIndexAsync(sc, new ConfiguredIndexOptions(defenition)));

await Db.ExecuteAsync("JSON.SET", "hero:1", ".", "{\"name\":\"harry\",\"lastName\":\"potter\",\"desc\":\"It's going to shock you\",\"email\":\"[email protected]\",\"address\":{\"city\":\"London\",\"street\":\"Privet Drive\",\"house\":\"5\"}}");
await Db.ExecuteAsync("JSON.SET", "hero:2", ".", "{\"name\":\"GEORGE\",\"lastName\":\"WEASLEY\",\"desc\":\"go home\",\"email\":\"[email protected]\"}");
await Db.ExecuteAsync("JSON.SET", "hero:3", ".", "{\"name\":\"FRED\",\"lastName\":\"WEASLEY\",\"desc\":\"live home\",\"email\":\"[email protected]\"}");

// Query
SearchResult res = cl.Search(new Query("go"));
Assert.Equal(2, res.TotalResults);
}
}
}