diff --git a/src/NRedisStack/Search/ISearchCommands.cs b/src/NRedisStack/Search/ISearchCommands.cs index d931a040..33e746ff 100644 --- a/src/NRedisStack/Search/ISearchCommands.cs +++ b/src/NRedisStack/Search/ISearchCommands.cs @@ -146,19 +146,21 @@ public interface ISearchCommands /// Return the execution plan for a complex query /// /// The index name - /// The query to explain + /// The query to explain + /// Dialect version under which to execute the query /// String that representing the execution plan /// - string Explain(string indexName, Query q); + string Explain(string indexName, string query, int? dialect = null); /// /// Return the execution plan for a complex query /// /// The index name - /// The query to explain + /// The query to explain + /// Dialect version under which to execute the query /// An array reply with a string representing the execution plan /// - RedisResult[] ExplainCli(string indexName, Query q); + RedisResult[] ExplainCli(string indexName, string query, int? dialect = null); /// /// Return information and statistics on the index. diff --git a/src/NRedisStack/Search/ISearchCommandsAsync.cs b/src/NRedisStack/Search/ISearchCommandsAsync.cs index 7948459b..e335c10e 100644 --- a/src/NRedisStack/Search/ISearchCommandsAsync.cs +++ b/src/NRedisStack/Search/ISearchCommandsAsync.cs @@ -145,19 +145,21 @@ public interface ISearchCommandsAsync /// Return the execution plan for a complex query /// /// The index name - /// The query to explain + /// The query to explain + /// Dialect version under which to execute the query /// String that representing the execution plan /// - Task ExplainAsync(string indexName, Query q); + Task ExplainAsync(string indexName, string query, int? dialect = null); /// /// Return the execution plan for a complex query /// /// The index name - /// The query to explain + /// The query to explain + /// Dialect version under which to execute the query /// An array reply with a string representing the execution plan /// - Task ExplainCliAsync(string indexName, Query q); + Task ExplainCliAsync(string indexName, string query, int? dialect = null); /// /// Return information and statistics on the index. diff --git a/src/NRedisStack/Search/Query.cs b/src/NRedisStack/Search/Query.cs index 0fa42f6c..ce97017f 100644 --- a/src/NRedisStack/Search/Query.cs +++ b/src/NRedisStack/Search/Query.cs @@ -191,12 +191,12 @@ public HighlightTags(string open, string close) public string Scorer { get; set; } // public bool ExplainScore { get; set; } // TODO: Check if this is needed because Jedis doesn't have it - private Dictionary _params = new Dictionary(); + private Dictionary _params = new Dictionary(); public int? dialect { get; private set;} = null; private int _slop = -1; private long _timeout = -1; private bool _inOrder = false; - private string _expander = null; + private string? _expander = null; public Query() : this("*") { } diff --git a/src/NRedisStack/Search/SearchCommandBuilder.cs b/src/NRedisStack/Search/SearchCommandBuilder.cs index 7899c24e..35d82136 100644 --- a/src/NRedisStack/Search/SearchCommandBuilder.cs +++ b/src/NRedisStack/Search/SearchCommandBuilder.cs @@ -126,17 +126,25 @@ public static SerializedCommand DropIndex(string indexName, bool dd = false) : new SerializedCommand(FT.DROPINDEX, indexName)); } - public static SerializedCommand Explain(string indexName, Query q) + public static SerializedCommand Explain(string indexName, string query, int? dialect) { - var args = new List { indexName }; - q.SerializeRedisArgs(args); + var args = new List { indexName, query }; + if (dialect != null) + { + args.Add("DIALECT"); + args.Add(dialect); + } return new SerializedCommand(FT.EXPLAIN, args); } - public static SerializedCommand ExplainCli(string indexName, Query q) + public static SerializedCommand ExplainCli(string indexName, string query, int? dialect) { - var args = new List { indexName }; - q.SerializeRedisArgs(args); + var args = new List { indexName, query }; + if (dialect != null) + { + args.Add("DIALECT"); + args.Add(dialect); + } return new SerializedCommand(FT.EXPLAINCLI, args); } diff --git a/src/NRedisStack/Search/SearchCommands.cs b/src/NRedisStack/Search/SearchCommands.cs index c6ca622e..ff9ed40a 100644 --- a/src/NRedisStack/Search/SearchCommands.cs +++ b/src/NRedisStack/Search/SearchCommands.cs @@ -130,23 +130,23 @@ public bool DropIndex(string indexName, bool dd = false) } /// - public string Explain(string indexName, Query q) + public string Explain(string indexName, string query, int? dialect = null) { - if (q.dialect == null && defaultDialect != null) + if (dialect == null && defaultDialect != null) { - q.Dialect((int)defaultDialect); + dialect = defaultDialect; } - return _db.Execute(SearchCommandBuilder.Explain(indexName, q)).ToString(); + return _db.Execute(SearchCommandBuilder.Explain(indexName, query, dialect)).ToString(); } /// - public RedisResult[] ExplainCli(string indexName, Query q) + public RedisResult[] ExplainCli(string indexName, string query, int? dialect = null) { - if (q.dialect == null && defaultDialect != null) + if (dialect == null && defaultDialect != null) { - q.Dialect((int)defaultDialect); + dialect = defaultDialect; } - return _db.Execute(SearchCommandBuilder.ExplainCli(indexName, q)).ToArray(); + return _db.Execute(SearchCommandBuilder.ExplainCli(indexName, query, dialect)).ToArray(); } /// diff --git a/src/NRedisStack/Search/SearchCommandsAsync.cs b/src/NRedisStack/Search/SearchCommandsAsync.cs index 90c1f1d4..d0de86b0 100644 --- a/src/NRedisStack/Search/SearchCommandsAsync.cs +++ b/src/NRedisStack/Search/SearchCommandsAsync.cs @@ -121,25 +121,25 @@ public async Task DropIndexAsync(string indexName, bool dd = false) } /// - public async Task ExplainAsync(string indexName, Query q) + public async Task ExplainAsync(string indexName, string query, int? dialect = null) { - if (q.dialect == null && defaultDialect != null) + if (dialect == null && defaultDialect != null) { - q.Dialect((int)defaultDialect); + dialect = defaultDialect; } - return (await _db.ExecuteAsync(SearchCommandBuilder.Explain(indexName, q))).ToString(); + return (await _db.ExecuteAsync(SearchCommandBuilder.Explain(indexName, query, dialect))).ToString(); } /// - public async Task ExplainCliAsync(string indexName, Query q) + public async Task ExplainCliAsync(string indexName, string query, int? dialect = null) { - if (q.dialect == null && defaultDialect != null) + if (dialect == null && defaultDialect != null) { - q.Dialect((int)defaultDialect); + dialect = defaultDialect; } - return (await _db.ExecuteAsync(SearchCommandBuilder.ExplainCli(indexName, q))).ToArray(); + return (await _db.ExecuteAsync(SearchCommandBuilder.ExplainCli(indexName, query, dialect))).ToArray(); } /// diff --git a/tests/NRedisStack.Tests/Search/SearchTests.cs b/tests/NRedisStack.Tests/Search/SearchTests.cs index 6c49e877..34bce220 100644 --- a/tests/NRedisStack.Tests/Search/SearchTests.cs +++ b/tests/NRedisStack.Tests/Search/SearchTests.cs @@ -1317,6 +1317,7 @@ public async Task TestDictionaryAsync() Assert.Equal((await ft.DictDumpAsync("dict")).Length, 0); } + string explainQuery = "@f3:f3_val @f2:f2_val @f1:f1_val"; [Fact] public void TestExplain() { @@ -1329,9 +1330,16 @@ public void TestExplain() .AddTextField("f3", 1.0); ft.Create(index, FTCreateParams.CreateParams(), sc); - string res = ft.Explain(index, new Query("@f3:f3_val @f2:f2_val @f1:f1_val")); + string res = ft.Explain(index, explainQuery); Assert.NotNull(res); Assert.False(res.Length == 0); + + // Test with dialect: + res = ft.Explain(index, explainQuery, 2); + Assert.NotNull(res); + Assert.False(res.Length == 0); + + } [Fact] @@ -1346,7 +1354,13 @@ public async Task TestExplainAsync() .AddTextField("f3", 1.0); ft.Create(index, FTCreateParams.CreateParams(), sc); - string res = await ft.ExplainAsync(index, new Query("@f3:f3_val @f2:f2_val @f1:f1_val")); + + string res = await ft.ExplainAsync(index, explainQuery); + Assert.NotNull(res); + Assert.False(res.Length == 0); + + // Test with dialect: + res = await ft.ExplainAsync(index, explainQuery, 2); Assert.NotNull(res); Assert.False(res.Length == 0); } @@ -1363,7 +1377,13 @@ public void TestExplainCli() .AddTextField("f3", 1.0); ft.Create(index, FTCreateParams.CreateParams(), sc); - var res = ft.ExplainCli(index, new Query("@f3:f3_val @f2:f2_val @f1:f1_val")); + + var res = ft.ExplainCli(index, explainQuery); + Assert.NotNull(res); + Assert.False(res.Length == 0); + + // Test with dialect (ovveride the dialect 2): + res = ft.ExplainCli(index, explainQuery, 1); Assert.NotNull(res); Assert.False(res.Length == 0); } @@ -1380,7 +1400,13 @@ public async Task TestExplainCliAsync() .AddTextField("f3", 1.0); ft.Create(index, FTCreateParams.CreateParams(), sc); - var res = await ft.ExplainCliAsync(index, new Query("@f3:f3_val @f2:f2_val @f1:f1_val")); + + var res = await ft.ExplainCliAsync(index, explainQuery); + Assert.NotNull(res); + Assert.False(res.Length == 0); + + // Test with dialect (ovveride the dialect 2): + res = await ft.ExplainCliAsync(index, explainQuery, 1); Assert.NotNull(res); Assert.False(res.Length == 0); } @@ -1397,7 +1423,7 @@ public void TestExplainWithDefaultDialect() .AddTextField("f3", 1.0); ft.Create(index, FTCreateParams.CreateParams(), sc); - String res = ft.Explain(index, new Query("@f3:f3_val @f2:f2_val @f1:f1_val")); + String res = ft.Explain(index, "@f3:f3_val @f2:f2_val @f1:f1_val"); Assert.NotNull(res); Assert.False(res.Length == 0); } @@ -1414,7 +1440,7 @@ public async Task TestExplainWithDefaultDialectAsync() .AddTextField("f3", 1.0); ft.Create(index, FTCreateParams.CreateParams(), sc); - String res = await ft.ExplainAsync(index, new Query("@f3:f3_val @f2:f2_val @f1:f1_val")); + String res = await ft.ExplainAsync(index, "@f3:f3_val @f2:f2_val @f1:f1_val"); Assert.NotNull(res); Assert.False(res.Length == 0); } @@ -2275,4 +2301,4 @@ public void TestModulePrefixs1() conn.Dispose(); } } -} +} \ No newline at end of file