Skip to content

Commit e152ef2

Browse files
authored
Set DIALECT 2 as default configurable dialect version (#355)
- provide dialect 2 as default configurable dialect version
1 parent 1eeb257 commit e152ef2

File tree

7 files changed

+74
-71
lines changed

7 files changed

+74
-71
lines changed

src/NRedisStack/ModulePrefixes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public static class ModulePrefixes
1717

1818
public static TdigestCommands TDIGEST(this IDatabase db) => new TdigestCommands(db);
1919

20-
public static SearchCommands FT(this IDatabase db, int? searchDialect = null) => new SearchCommands(db, searchDialect);
20+
public static SearchCommands FT(this IDatabase db, int? searchDialect = 2) => new SearchCommands(db, searchDialect);
2121

2222
public static JsonCommands JSON(this IDatabase db) => new JsonCommands(db);
2323

src/NRedisStack/Search/AggregationRequest.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using NRedisStack.Search.Literals;
33

44
namespace NRedisStack.Search;
5-
public class AggregationRequest
5+
public class AggregationRequest : IDialectAwareParam
66
{
77
private List<object> args = new List<object>(); // Check if Readonly
88
private bool isWithCursor = false;
@@ -184,4 +184,10 @@ public bool IsWithCursor()
184184
{
185185
return isWithCursor;
186186
}
187+
188+
int? IDialectAwareParam.Dialect
189+
{
190+
get { return dialect; }
191+
set { dialect = value; }
192+
}
187193
}

src/NRedisStack/Search/FTSpellCheckParams.cs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using NRedisStack.Search.Literals;
22
namespace NRedisStack.Search
33
{
4-
public class FTSpellCheckParams
4+
public class FTSpellCheckParams : IDialectAwareParam
55
{
66
List<object> args = new List<object>();
77
private List<KeyValuePair<string, string>> terms = new List<KeyValuePair<string, string>>();
@@ -59,38 +59,29 @@ public List<object> GetArgs()
5959
}
6060

6161
public void SerializeRedisArgs()
62-
{
63-
Distance();
64-
Terms();
65-
Dialect();
66-
}
67-
68-
private void Dialect()
6962
{
7063
if (dialect != null)
7164
{
7265
args.Add(SearchArgs.DIALECT);
7366
args.Add(dialect);
7467
}
75-
}
76-
77-
private void Terms()
78-
{
7968
foreach (var term in terms)
8069
{
8170
args.Add(SearchArgs.TERMS);
8271
args.Add(term.Value);
8372
args.Add(term.Key);
8473
}
85-
}
86-
87-
private void Distance()
88-
{
8974
if (distance != null)
9075
{
9176
args.Add(SearchArgs.DISTANCE);
9277
args.Add(distance);
9378
}
9479
}
80+
81+
int? IDialectAwareParam.Dialect
82+
{
83+
get { return dialect; }
84+
set { dialect = value; }
85+
}
9586
}
9687
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace NRedisStack.Search
2+
{
3+
/// <summary>
4+
/// Interface for dialect-aware parameters.
5+
/// To provide a single interface to manage default dialect version under which to execute the query.
6+
/// </summary>
7+
internal interface IDialectAwareParam
8+
{
9+
/// <summary>
10+
/// Selects the dialect version under which to execute the query.
11+
/// </summary>
12+
internal int? Dialect { get; set; }
13+
}
14+
15+
}

src/NRedisStack/Search/Query.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace NRedisStack.Search
77
/// <summary>
88
/// Query represents query parameters and filters to load results from the engine
99
/// </summary>
10-
public sealed class Query
10+
public sealed class Query : IDialectAwareParam
1111
{
1212
/// <summary>
1313
/// Filter represents a filtering rules in a query
@@ -691,5 +691,12 @@ public Query SetExpander(String field)
691691
_expander = field;
692692
return this;
693693
}
694+
695+
int? IDialectAwareParam.Dialect
696+
{
697+
get { return dialect; }
698+
set { dialect = value; }
699+
}
700+
694701
}
695702
}

src/NRedisStack/Search/SearchCommands.cs

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,10 @@ namespace NRedisStack
66
{
77
public class SearchCommands : SearchCommandsAsync, ISearchCommands
88
{
9-
IDatabase _db;
10-
public SearchCommands(IDatabase db, int? defaultDialect) : base(db)
9+
private IDatabase _db;
10+
public SearchCommands(IDatabase db, int? defaultDialect = 2) : base(db, defaultDialect)
1111
{
1212
_db = db;
13-
SetDefaultDialect(defaultDialect);
14-
this.defaultDialect = defaultDialect;
15-
}
16-
17-
public void SetDefaultDialect(int? defaultDialect)
18-
{
19-
if (defaultDialect == 0)
20-
{
21-
throw new System.ArgumentOutOfRangeException("DIALECT=0 cannot be set.");
22-
}
23-
this.defaultDialect = defaultDialect;
2413
}
2514

2615
/// <inheritdoc/>
@@ -32,11 +21,7 @@ public RedisResult[] _List()
3221
/// <inheritdoc/>
3322
public AggregationResult Aggregate(string index, AggregationRequest query)
3423
{
35-
if (query.dialect == null && defaultDialect != null)
36-
{
37-
query.Dialect((int)defaultDialect);
38-
}
39-
24+
setDefaultDialectIfUnset(query);
4025
var result = _db.Execute(SearchCommandBuilder.Aggregate(index, query));
4126
return result.ToAggregationResult(query);
4227
}
@@ -130,20 +115,14 @@ public bool DropIndex(string indexName, bool dd = false)
130115
/// <inheritdoc/>
131116
public string Explain(string indexName, string query, int? dialect = null)
132117
{
133-
if (dialect == null && defaultDialect != null)
134-
{
135-
dialect = defaultDialect;
136-
}
118+
dialect = checkAndGetDefaultDialect(dialect);
137119
return _db.Execute(SearchCommandBuilder.Explain(indexName, query, dialect)).ToString()!;
138120
}
139121

140122
/// <inheritdoc/>
141123
public RedisResult[] ExplainCli(string indexName, string query, int? dialect = null)
142124
{
143-
if (dialect == null && defaultDialect != null)
144-
{
145-
dialect = defaultDialect;
146-
}
125+
dialect = checkAndGetDefaultDialect(dialect);
147126
return _db.Execute(SearchCommandBuilder.ExplainCli(indexName, query, dialect)).ToArray();
148127
}
149128

@@ -160,22 +139,21 @@ public Tuple<SearchResult, Dictionary<string, RedisResult>> ProfileSearch(string
160139
/// <inheritdoc/>
161140
public Tuple<AggregationResult, Dictionary<string, RedisResult>> ProfileAggregate(string indexName, AggregationRequest query, bool limited = false)
162141
{
142+
setDefaultDialectIfUnset(query);
163143
return _db.Execute(SearchCommandBuilder.ProfileAggregate(indexName, query, limited))
164144
.ToProfileAggregateResult(query);
165145
}
166146
/// <inheritdoc/>
167147
public SearchResult Search(string indexName, Query q)
168148
{
169-
if (q.dialect == null && defaultDialect != null)
170-
{
171-
q.Dialect((int)defaultDialect);
172-
}
149+
setDefaultDialectIfUnset(q);
173150
return _db.Execute(SearchCommandBuilder.Search(indexName, q)).ToSearchResult(q);
174151
}
175152

176153
/// <inheritdoc/>
177154
public Dictionary<string, Dictionary<string, double>> SpellCheck(string indexName, string query, FTSpellCheckParams? spellCheckParams = null)
178155
{
156+
setDefaultDialectIfUnset(spellCheckParams);
179157
return _db.Execute(SearchCommandBuilder.SpellCheck(indexName, query, spellCheckParams)).ToFtSpellCheckResult();
180158
}
181159

src/NRedisStack/Search/SearchCommandsAsync.cs

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,33 @@ namespace NRedisStack
66
{
77
public class SearchCommandsAsync : ISearchCommandsAsync
88
{
9-
IDatabaseAsync _db;
9+
private IDatabaseAsync _db;
1010
protected int? defaultDialect;
1111

12-
public SearchCommandsAsync(IDatabaseAsync db)
12+
public SearchCommandsAsync(IDatabaseAsync db, int? defaultDialect = 2)
1313
{
1414
_db = db;
15+
SetDefaultDialect(defaultDialect);
16+
}
17+
18+
internal void setDefaultDialectIfUnset(IDialectAwareParam param)
19+
{
20+
if (param != null && param.Dialect == null && defaultDialect != null)
21+
{
22+
param.Dialect = defaultDialect;
23+
}
24+
}
25+
26+
internal int? checkAndGetDefaultDialect(int? dialect) =>
27+
(dialect == null && defaultDialect != null) ? defaultDialect : dialect;
28+
29+
public void SetDefaultDialect(int? defaultDialect)
30+
{
31+
if (defaultDialect == 0)
32+
{
33+
throw new System.ArgumentOutOfRangeException("DIALECT=0 cannot be set.");
34+
}
35+
this.defaultDialect = defaultDialect;
1536
}
1637

1738
/// <inheritdoc/>
@@ -23,11 +44,7 @@ public async Task<RedisResult[]> _ListAsync()
2344
/// <inheritdoc/>
2445
public async Task<AggregationResult> AggregateAsync(string index, AggregationRequest query)
2546
{
26-
if (query.dialect == null && defaultDialect != null)
27-
{
28-
query.Dialect((int)defaultDialect);
29-
}
30-
47+
setDefaultDialectIfUnset(query);
3148
var result = await _db.ExecuteAsync(SearchCommandBuilder.Aggregate(index, query));
3249
if (query.IsWithCursor())
3350
{
@@ -129,22 +146,14 @@ public async Task<bool> DropIndexAsync(string indexName, bool dd = false)
129146
/// <inheritdoc/>
130147
public async Task<string> ExplainAsync(string indexName, string query, int? dialect = null)
131148
{
132-
if (dialect == null && defaultDialect != null)
133-
{
134-
dialect = defaultDialect;
135-
}
136-
149+
dialect = checkAndGetDefaultDialect(dialect);
137150
return (await _db.ExecuteAsync(SearchCommandBuilder.Explain(indexName, query, dialect))).ToString()!;
138151
}
139152

140153
/// <inheritdoc/>
141154
public async Task<RedisResult[]> ExplainCliAsync(string indexName, string query, int? dialect = null)
142155
{
143-
if (dialect == null && defaultDialect != null)
144-
{
145-
dialect = defaultDialect;
146-
}
147-
156+
dialect = checkAndGetDefaultDialect(dialect);
148157
return (await _db.ExecuteAsync(SearchCommandBuilder.ExplainCli(indexName, query, dialect))).ToArray();
149158
}
150159

@@ -168,17 +177,14 @@ public async Task<Tuple<AggregationResult, Dictionary<string, RedisResult>>> Pro
168177
/// <inheritdoc/>
169178
public async Task<SearchResult> SearchAsync(string indexName, Query q)
170179
{
171-
if (q.dialect == null && defaultDialect != null)
172-
{
173-
q.Dialect((int)defaultDialect);
174-
}
175-
180+
setDefaultDialectIfUnset(q);
176181
return (await _db.ExecuteAsync(SearchCommandBuilder.Search(indexName, q))).ToSearchResult(q);
177182
}
178183

179184
/// <inheritdoc/>
180185
public async Task<Dictionary<string, Dictionary<string, double>>> SpellCheckAsync(string indexName, string query, FTSpellCheckParams? spellCheckParams = null)
181186
{
187+
setDefaultDialectIfUnset(spellCheckParams);
182188
return (await _db.ExecuteAsync(SearchCommandBuilder.SpellCheck(indexName, query, spellCheckParams))).ToFtSpellCheckResult();
183189
}
184190

0 commit comments

Comments
 (0)