Skip to content

Commit 7d1c314

Browse files
authored
Fix Query input in Explain & ExplainCli (#149)
* change the structure of AggregationRequest * Fix Explain Commands * fixes * dialect 2 * default dialect * add tests * Params > 1 for AggregationRequest * coverage * fix * review * generate explainQuery once
1 parent 69d7896 commit 7d1c314

File tree

7 files changed

+77
-39
lines changed

7 files changed

+77
-39
lines changed

src/NRedisStack/Search/ISearchCommands.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,19 +146,21 @@ public interface ISearchCommands
146146
/// Return the execution plan for a complex query
147147
/// </summary>
148148
/// <param name="indexName">The index name</param>
149-
/// <param name="q">The query to explain</param>
149+
/// <param name="query">The query to explain</param>
150+
/// <param name="dialect">Dialect version under which to execute the query</param>
150151
/// <returns>String that representing the execution plan</returns>
151152
/// <remarks><seealso href="https://redis.io/commands/ft.explain/"/></remarks>
152-
string Explain(string indexName, Query q);
153+
string Explain(string indexName, string query, int? dialect = null);
153154

154155
/// <summary>
155156
/// Return the execution plan for a complex query
156157
/// </summary>
157158
/// <param name="indexName">The index name</param>
158-
/// <param name="q">The query to explain</param>
159+
/// <param name="query">The query to explain</param>
160+
/// <param name="dialect">Dialect version under which to execute the query</param>
159161
/// <returns>An array reply with a string representing the execution plan</returns>
160162
/// <remarks><seealso href="https://redis.io/commands/ft.explaincli/"/></remarks>
161-
RedisResult[] ExplainCli(string indexName, Query q);
163+
RedisResult[] ExplainCli(string indexName, string query, int? dialect = null);
162164

163165
/// <summary>
164166
/// Return information and statistics on the index.

src/NRedisStack/Search/ISearchCommandsAsync.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,21 @@ public interface ISearchCommandsAsync
145145
/// Return the execution plan for a complex query
146146
/// </summary>
147147
/// <param name="indexName">The index name</param>
148-
/// <param name="q">The query to explain</param>
148+
/// <param name="query">The query to explain</param>
149+
/// <param name="dialect">Dialect version under which to execute the query</param>
149150
/// <returns>String that representing the execution plan</returns>
150151
/// <remarks><seealso href="https://redis.io/commands/ft.explain/"/></remarks>
151-
Task<string> ExplainAsync(string indexName, Query q);
152+
Task<string> ExplainAsync(string indexName, string query, int? dialect = null);
152153

153154
/// <summary>
154155
/// Return the execution plan for a complex query
155156
/// </summary>
156157
/// <param name="indexName">The index name</param>
157-
/// <param name="q">The query to explain</param>
158+
/// <param name="query">The query to explain</param>
159+
/// <param name="dialect">Dialect version under which to execute the query</param>
158160
/// <returns>An array reply with a string representing the execution plan</returns>
159161
/// <remarks><seealso href="https://redis.io/commands/ft.explaincli/"/></remarks>
160-
Task<RedisResult[]> ExplainCliAsync(string indexName, Query q);
162+
Task<RedisResult[]> ExplainCliAsync(string indexName, string query, int? dialect = null);
161163

162164
/// <summary>
163165
/// Return information and statistics on the index.

src/NRedisStack/Search/Query.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,12 @@ public HighlightTags(string open, string close)
191191
public string Scorer { get; set; }
192192
// public bool ExplainScore { get; set; } // TODO: Check if this is needed because Jedis doesn't have it
193193

194-
private Dictionary<String, Object> _params = new Dictionary<string, object>();
194+
private Dictionary<string, object> _params = new Dictionary<string, object>();
195195
public int? dialect { get; private set;} = null;
196196
private int _slop = -1;
197197
private long _timeout = -1;
198198
private bool _inOrder = false;
199-
private string _expander = null;
199+
private string? _expander = null;
200200

201201
public Query() : this("*") { }
202202

src/NRedisStack/Search/SearchCommandBuilder.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,17 +126,25 @@ public static SerializedCommand DropIndex(string indexName, bool dd = false)
126126
: new SerializedCommand(FT.DROPINDEX, indexName));
127127
}
128128

129-
public static SerializedCommand Explain(string indexName, Query q)
129+
public static SerializedCommand Explain(string indexName, string query, int? dialect)
130130
{
131-
var args = new List<object> { indexName };
132-
q.SerializeRedisArgs(args);
131+
var args = new List<object> { indexName, query };
132+
if (dialect != null)
133+
{
134+
args.Add("DIALECT");
135+
args.Add(dialect);
136+
}
133137
return new SerializedCommand(FT.EXPLAIN, args);
134138
}
135139

136-
public static SerializedCommand ExplainCli(string indexName, Query q)
140+
public static SerializedCommand ExplainCli(string indexName, string query, int? dialect)
137141
{
138-
var args = new List<object> { indexName };
139-
q.SerializeRedisArgs(args);
142+
var args = new List<object> { indexName, query };
143+
if (dialect != null)
144+
{
145+
args.Add("DIALECT");
146+
args.Add(dialect);
147+
}
140148
return new SerializedCommand(FT.EXPLAINCLI, args);
141149
}
142150

src/NRedisStack/Search/SearchCommands.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,23 +130,23 @@ public bool DropIndex(string indexName, bool dd = false)
130130
}
131131

132132
/// <inheritdoc/>
133-
public string Explain(string indexName, Query q)
133+
public string Explain(string indexName, string query, int? dialect = null)
134134
{
135-
if (q.dialect == null && defaultDialect != null)
135+
if (dialect == null && defaultDialect != null)
136136
{
137-
q.Dialect((int)defaultDialect);
137+
dialect = defaultDialect;
138138
}
139-
return _db.Execute(SearchCommandBuilder.Explain(indexName, q)).ToString();
139+
return _db.Execute(SearchCommandBuilder.Explain(indexName, query, dialect)).ToString();
140140
}
141141

142142
/// <inheritdoc/>
143-
public RedisResult[] ExplainCli(string indexName, Query q)
143+
public RedisResult[] ExplainCli(string indexName, string query, int? dialect = null)
144144
{
145-
if (q.dialect == null && defaultDialect != null)
145+
if (dialect == null && defaultDialect != null)
146146
{
147-
q.Dialect((int)defaultDialect);
147+
dialect = defaultDialect;
148148
}
149-
return _db.Execute(SearchCommandBuilder.ExplainCli(indexName, q)).ToArray();
149+
return _db.Execute(SearchCommandBuilder.ExplainCli(indexName, query, dialect)).ToArray();
150150
}
151151

152152
/// <inheritdoc/>

src/NRedisStack/Search/SearchCommandsAsync.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,25 +121,25 @@ public async Task<bool> DropIndexAsync(string indexName, bool dd = false)
121121
}
122122

123123
/// <inheritdoc/>
124-
public async Task<string> ExplainAsync(string indexName, Query q)
124+
public async Task<string> ExplainAsync(string indexName, string query, int? dialect = null)
125125
{
126-
if (q.dialect == null && defaultDialect != null)
126+
if (dialect == null && defaultDialect != null)
127127
{
128-
q.Dialect((int)defaultDialect);
128+
dialect = defaultDialect;
129129
}
130130

131-
return (await _db.ExecuteAsync(SearchCommandBuilder.Explain(indexName, q))).ToString();
131+
return (await _db.ExecuteAsync(SearchCommandBuilder.Explain(indexName, query, dialect))).ToString();
132132
}
133133

134134
/// <inheritdoc/>
135-
public async Task<RedisResult[]> ExplainCliAsync(string indexName, Query q)
135+
public async Task<RedisResult[]> ExplainCliAsync(string indexName, string query, int? dialect = null)
136136
{
137-
if (q.dialect == null && defaultDialect != null)
137+
if (dialect == null && defaultDialect != null)
138138
{
139-
q.Dialect((int)defaultDialect);
139+
dialect = defaultDialect;
140140
}
141141

142-
return (await _db.ExecuteAsync(SearchCommandBuilder.ExplainCli(indexName, q))).ToArray();
142+
return (await _db.ExecuteAsync(SearchCommandBuilder.ExplainCli(indexName, query, dialect))).ToArray();
143143
}
144144

145145
/// <inheritdoc/>

tests/NRedisStack.Tests/Search/SearchTests.cs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,7 @@ public async Task TestDictionaryAsync()
13171317
Assert.Equal((await ft.DictDumpAsync("dict")).Length, 0);
13181318
}
13191319

1320+
string explainQuery = "@f3:f3_val @f2:f2_val @f1:f1_val";
13201321
[Fact]
13211322
public void TestExplain()
13221323
{
@@ -1329,9 +1330,16 @@ public void TestExplain()
13291330
.AddTextField("f3", 1.0);
13301331
ft.Create(index, FTCreateParams.CreateParams(), sc);
13311332

1332-
string res = ft.Explain(index, new Query("@f3:f3_val @f2:f2_val @f1:f1_val"));
1333+
string res = ft.Explain(index, explainQuery);
13331334
Assert.NotNull(res);
13341335
Assert.False(res.Length == 0);
1336+
1337+
// Test with dialect:
1338+
res = ft.Explain(index, explainQuery, 2);
1339+
Assert.NotNull(res);
1340+
Assert.False(res.Length == 0);
1341+
1342+
13351343
}
13361344

13371345
[Fact]
@@ -1346,7 +1354,13 @@ public async Task TestExplainAsync()
13461354
.AddTextField("f3", 1.0);
13471355
ft.Create(index, FTCreateParams.CreateParams(), sc);
13481356

1349-
string res = await ft.ExplainAsync(index, new Query("@f3:f3_val @f2:f2_val @f1:f1_val"));
1357+
1358+
string res = await ft.ExplainAsync(index, explainQuery);
1359+
Assert.NotNull(res);
1360+
Assert.False(res.Length == 0);
1361+
1362+
// Test with dialect:
1363+
res = await ft.ExplainAsync(index, explainQuery, 2);
13501364
Assert.NotNull(res);
13511365
Assert.False(res.Length == 0);
13521366
}
@@ -1363,7 +1377,13 @@ public void TestExplainCli()
13631377
.AddTextField("f3", 1.0);
13641378
ft.Create(index, FTCreateParams.CreateParams(), sc);
13651379

1366-
var res = ft.ExplainCli(index, new Query("@f3:f3_val @f2:f2_val @f1:f1_val"));
1380+
1381+
var res = ft.ExplainCli(index, explainQuery);
1382+
Assert.NotNull(res);
1383+
Assert.False(res.Length == 0);
1384+
1385+
// Test with dialect (ovveride the dialect 2):
1386+
res = ft.ExplainCli(index, explainQuery, 1);
13671387
Assert.NotNull(res);
13681388
Assert.False(res.Length == 0);
13691389
}
@@ -1380,7 +1400,13 @@ public async Task TestExplainCliAsync()
13801400
.AddTextField("f3", 1.0);
13811401
ft.Create(index, FTCreateParams.CreateParams(), sc);
13821402

1383-
var res = await ft.ExplainCliAsync(index, new Query("@f3:f3_val @f2:f2_val @f1:f1_val"));
1403+
1404+
var res = await ft.ExplainCliAsync(index, explainQuery);
1405+
Assert.NotNull(res);
1406+
Assert.False(res.Length == 0);
1407+
1408+
// Test with dialect (ovveride the dialect 2):
1409+
res = await ft.ExplainCliAsync(index, explainQuery, 1);
13841410
Assert.NotNull(res);
13851411
Assert.False(res.Length == 0);
13861412
}
@@ -1397,7 +1423,7 @@ public void TestExplainWithDefaultDialect()
13971423
.AddTextField("f3", 1.0);
13981424
ft.Create(index, FTCreateParams.CreateParams(), sc);
13991425

1400-
String res = ft.Explain(index, new Query("@f3:f3_val @f2:f2_val @f1:f1_val"));
1426+
String res = ft.Explain(index, "@f3:f3_val @f2:f2_val @f1:f1_val");
14011427
Assert.NotNull(res);
14021428
Assert.False(res.Length == 0);
14031429
}
@@ -1414,7 +1440,7 @@ public async Task TestExplainWithDefaultDialectAsync()
14141440
.AddTextField("f3", 1.0);
14151441
ft.Create(index, FTCreateParams.CreateParams(), sc);
14161442

1417-
String res = await ft.ExplainAsync(index, new Query("@f3:f3_val @f2:f2_val @f1:f1_val"));
1443+
String res = await ft.ExplainAsync(index, "@f3:f3_val @f2:f2_val @f1:f1_val");
14181444
Assert.NotNull(res);
14191445
Assert.False(res.Length == 0);
14201446
}
@@ -2275,4 +2301,4 @@ public void TestModulePrefixs1()
22752301
conn.Dispose();
22762302
}
22772303
}
2278-
}
2304+
}

0 commit comments

Comments
 (0)