Skip to content

Set DIALECT 2 as default configurable dialect version #355

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

Merged
merged 2 commits into from
Dec 30, 2024
Merged
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
2 changes: 1 addition & 1 deletion src/NRedisStack/ModulePrefixes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class ModulePrefixes

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

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

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

Expand Down
8 changes: 7 additions & 1 deletion src/NRedisStack/Search/AggregationRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using NRedisStack.Search.Literals;

namespace NRedisStack.Search;
public class AggregationRequest
public class AggregationRequest : IDialectAwareParam
{
private List<object> args = new List<object>(); // Check if Readonly
private bool isWithCursor = false;
Expand Down Expand Up @@ -184,4 +184,10 @@ public bool IsWithCursor()
{
return isWithCursor;
}

int? IDialectAwareParam.Dialect
{
get { return dialect; }
set { dialect = value; }
}
}
23 changes: 7 additions & 16 deletions src/NRedisStack/Search/FTSpellCheckParams.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using NRedisStack.Search.Literals;
namespace NRedisStack.Search
{
public class FTSpellCheckParams
public class FTSpellCheckParams : IDialectAwareParam
{
List<object> args = new List<object>();
private List<KeyValuePair<string, string>> terms = new List<KeyValuePair<string, string>>();
Expand Down Expand Up @@ -59,38 +59,29 @@ public List<object> GetArgs()
}

public void SerializeRedisArgs()
{
Distance();
Terms();
Dialect();
}

private void Dialect()
{
if (dialect != null)
{
args.Add(SearchArgs.DIALECT);
args.Add(dialect);
}
}

private void Terms()
{
foreach (var term in terms)
{
args.Add(SearchArgs.TERMS);
args.Add(term.Value);
args.Add(term.Key);
}
}

private void Distance()
{
if (distance != null)
{
args.Add(SearchArgs.DISTANCE);
args.Add(distance);
}
}

int? IDialectAwareParam.Dialect
{
get { return dialect; }
set { dialect = value; }
}
}
}
15 changes: 15 additions & 0 deletions src/NRedisStack/Search/IDialectAwareParam.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace NRedisStack.Search
{
/// <summary>
/// Interface for dialect-aware parameters.
/// To provide a single interface to manage default dialect version under which to execute the query.
/// </summary>
internal interface IDialectAwareParam
{
/// <summary>
/// Selects the dialect version under which to execute the query.
/// </summary>
internal int? Dialect { get; set; }
}

}
9 changes: 8 additions & 1 deletion src/NRedisStack/Search/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/// <summary>
/// Query represents query parameters and filters to load results from the engine
/// </summary>
public sealed class Query
public sealed class Query : IDialectAwareParam
{
/// <summary>
/// Filter represents a filtering rules in a query
Expand Down Expand Up @@ -683,7 +683,7 @@
/// <summary>
/// Set the query to use a custom query expander instead of the stemmer
/// </summary>
/// <param name="field the expander field's name"></param>

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / .NET 8 on [redis-stack cluster] / Test

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / .NET 8 on [redis-stack cluster] / Test

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / .NET 7 on [redis-stack cluster] / Test

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / .NET 7 on [redis-stack cluster] / Test

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / .NET 6 on [redis-stack cluster] / Test

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / .NET 6 on [redis-stack cluster] / Test

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / .NET 8 on [redis-stack 6.2.6] / Test

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / .NET 8 on [redis-stack 6.2.6] / Test

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / .NET 6 on [redis-stack 6.2.6] / Test

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / .NET 6 on [redis-stack 6.2.6] / Test

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / .NET 7 on [redis-stack 6.2.6] / Test

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / .NET 7 on [redis-stack 6.2.6] / Test

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / .NET 7 on [redis-stack 7.2.0-RC3] / Test

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / .NET 7 on [redis-stack 7.2.0-RC3] / Test

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / .NET 6 on [redis-stack 7.2.0-RC3] / Test

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / .NET 6 on [redis-stack 7.2.0-RC3] / Test

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / .NET 8 on [redis-stack 7.2.0-RC3] / Test

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / .NET 8 on [redis-stack 7.2.0-RC3] / Test

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / .NET 8 on [redis-stack edge] / Test

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / .NET 8 on [redis-stack edge] / Test

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / .NET 7 on [redis-stack edge] / Test

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / .NET 7 on [redis-stack edge] / Test

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / .NET 6 on [redis-stack edge] / Test

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / .NET 6 on [redis-stack edge] / Test

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / Redis Enterprise (7.4.2-54, enterprise_oss_cluster)

Unexpected character '''. See also error CS1056.

Check warning on line 686 in src/NRedisStack/Search/Query.cs

View workflow job for this annotation

GitHub Actions / Redis Enterprise (7.4.2-54, enterprise)

Unexpected character '''. See also error CS1056.
/// <returns>the query object itself</returns>

public Query SetExpander(String field)
Expand All @@ -691,5 +691,12 @@
_expander = field;
return this;
}

int? IDialectAwareParam.Dialect
{
get { return dialect; }
set { dialect = value; }
}

}
}
38 changes: 8 additions & 30 deletions src/NRedisStack/Search/SearchCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,10 @@ namespace NRedisStack
{
public class SearchCommands : SearchCommandsAsync, ISearchCommands
{
IDatabase _db;
public SearchCommands(IDatabase db, int? defaultDialect) : base(db)
private IDatabase _db;
public SearchCommands(IDatabase db, int? defaultDialect = 2) : base(db, defaultDialect)
{
_db = db;
SetDefaultDialect(defaultDialect);
this.defaultDialect = defaultDialect;
}

public void SetDefaultDialect(int? defaultDialect)
{
if (defaultDialect == 0)
{
throw new System.ArgumentOutOfRangeException("DIALECT=0 cannot be set.");
}
this.defaultDialect = defaultDialect;
}

/// <inheritdoc/>
Expand All @@ -32,11 +21,7 @@ public RedisResult[] _List()
/// <inheritdoc/>
public AggregationResult Aggregate(string index, AggregationRequest query)
{
if (query.dialect == null && defaultDialect != null)
{
query.Dialect((int)defaultDialect);
}

setDefaultDialectIfUnset(query);
var result = _db.Execute(SearchCommandBuilder.Aggregate(index, query));
return result.ToAggregationResult(query);
}
Expand Down Expand Up @@ -130,20 +115,14 @@ public bool DropIndex(string indexName, bool dd = false)
/// <inheritdoc/>
public string Explain(string indexName, string query, int? dialect = null)
{
if (dialect == null && defaultDialect != null)
{
dialect = defaultDialect;
}
dialect = checkAndGetDefaultDialect(dialect);
return _db.Execute(SearchCommandBuilder.Explain(indexName, query, dialect)).ToString()!;
}

/// <inheritdoc/>
public RedisResult[] ExplainCli(string indexName, string query, int? dialect = null)
{
if (dialect == null && defaultDialect != null)
{
dialect = defaultDialect;
}
dialect = checkAndGetDefaultDialect(dialect);
return _db.Execute(SearchCommandBuilder.ExplainCli(indexName, query, dialect)).ToArray();
}

Expand All @@ -160,22 +139,21 @@ public Tuple<SearchResult, Dictionary<string, RedisResult>> ProfileSearch(string
/// <inheritdoc/>
public Tuple<AggregationResult, Dictionary<string, RedisResult>> ProfileAggregate(string indexName, AggregationRequest query, bool limited = false)
{
setDefaultDialectIfUnset(query);
return _db.Execute(SearchCommandBuilder.ProfileAggregate(indexName, query, limited))
.ToProfileAggregateResult(query);
}
/// <inheritdoc/>
public SearchResult Search(string indexName, Query q)
{
if (q.dialect == null && defaultDialect != null)
{
q.Dialect((int)defaultDialect);
}
setDefaultDialectIfUnset(q);
return _db.Execute(SearchCommandBuilder.Search(indexName, q)).ToSearchResult(q);
}

/// <inheritdoc/>
public Dictionary<string, Dictionary<string, double>> SpellCheck(string indexName, string query, FTSpellCheckParams? spellCheckParams = null)
{
setDefaultDialectIfUnset(spellCheckParams);
return _db.Execute(SearchCommandBuilder.SpellCheck(indexName, query, spellCheckParams)).ToFtSpellCheckResult();
}

Expand Down
50 changes: 28 additions & 22 deletions src/NRedisStack/Search/SearchCommandsAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,33 @@ namespace NRedisStack
{
public class SearchCommandsAsync : ISearchCommandsAsync
{
IDatabaseAsync _db;
private IDatabaseAsync _db;
protected int? defaultDialect;

public SearchCommandsAsync(IDatabaseAsync db)
public SearchCommandsAsync(IDatabaseAsync db, int? defaultDialect = 2)
{
_db = db;
SetDefaultDialect(defaultDialect);
}

internal void setDefaultDialectIfUnset(IDialectAwareParam param)
{
if (param != null && param.Dialect == null && defaultDialect != null)
{
param.Dialect = defaultDialect;
}
}

internal int? checkAndGetDefaultDialect(int? dialect) =>
(dialect == null && defaultDialect != null) ? defaultDialect : dialect;

public void SetDefaultDialect(int? defaultDialect)
{
if (defaultDialect == 0)
{
throw new System.ArgumentOutOfRangeException("DIALECT=0 cannot be set.");
}
this.defaultDialect = defaultDialect;
}

/// <inheritdoc/>
Expand All @@ -23,11 +44,7 @@ public async Task<RedisResult[]> _ListAsync()
/// <inheritdoc/>
public async Task<AggregationResult> AggregateAsync(string index, AggregationRequest query)
{
if (query.dialect == null && defaultDialect != null)
{
query.Dialect((int)defaultDialect);
}

setDefaultDialectIfUnset(query);
var result = await _db.ExecuteAsync(SearchCommandBuilder.Aggregate(index, query));
if (query.IsWithCursor())
{
Expand Down Expand Up @@ -129,22 +146,14 @@ public async Task<bool> DropIndexAsync(string indexName, bool dd = false)
/// <inheritdoc/>
public async Task<string> ExplainAsync(string indexName, string query, int? dialect = null)
{
if (dialect == null && defaultDialect != null)
{
dialect = defaultDialect;
}

dialect = checkAndGetDefaultDialect(dialect);
return (await _db.ExecuteAsync(SearchCommandBuilder.Explain(indexName, query, dialect))).ToString()!;
}

/// <inheritdoc/>
public async Task<RedisResult[]> ExplainCliAsync(string indexName, string query, int? dialect = null)
{
if (dialect == null && defaultDialect != null)
{
dialect = defaultDialect;
}

dialect = checkAndGetDefaultDialect(dialect);
return (await _db.ExecuteAsync(SearchCommandBuilder.ExplainCli(indexName, query, dialect))).ToArray();
}

Expand All @@ -168,17 +177,14 @@ public async Task<Tuple<AggregationResult, Dictionary<string, RedisResult>>> Pro
/// <inheritdoc/>
public async Task<SearchResult> SearchAsync(string indexName, Query q)
{
if (q.dialect == null && defaultDialect != null)
{
q.Dialect((int)defaultDialect);
}

setDefaultDialectIfUnset(q);
return (await _db.ExecuteAsync(SearchCommandBuilder.Search(indexName, q))).ToSearchResult(q);
}

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

Expand Down
Loading