Skip to content
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
10 changes: 10 additions & 0 deletions src/EFCore.Cosmos/Extensions/CosmosDbFunctionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ public static double FullTextScore(this DbFunctions _, string property, params s
public static double Rrf(this DbFunctions _, params double[] scores)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Rrf)));

/// <summary>
/// Combines scores provided by two or more specified functions.
/// </summary>
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
/// <param name="scores">Scoring function calls to be combined.</param>
/// <param name="weights">An array of numbers defining an importance weight for each scoring function.</param>
/// <returns>The combined score.</returns>
public static double Rrf(this DbFunctions _, double[] scores, double[] weights)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Rrf)));

#region VectorDistance

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,69 +31,57 @@ public class CosmosFullTextSearchTranslator(ISqlExpressionFactory sqlExpressionF

return method.Name switch
{
nameof(CosmosDbFunctionsExtensions.FullTextContains)
when arguments is [_, var property, var keyword] => sqlExpressionFactory.Function(
nameof(CosmosDbFunctionsExtensions.FullTextContains) when arguments is [_, var property, var keyword]
=> sqlExpressionFactory.Function(
"FullTextContains",
[property, keyword],
typeof(bool),
typeMappingSource.FindMapping(typeof(bool))),

nameof(CosmosDbFunctionsExtensions.FullTextScore)
when arguments is
[_, { } property, SqlConstantExpression { Type: var keywordClrType, Value: string[] values } keywords]
&& keywordClrType == typeof(string[]) => sqlExpressionFactory.ScoringFunction(
"FullTextScore",
[property, .. values.Select(x => sqlExpressionFactory.Constant(x))],
typeof(double),
typeMappingSource.FindMapping(typeof(double))),
nameof(CosmosDbFunctionsExtensions.FullTextContainsAny) or nameof(CosmosDbFunctionsExtensions.FullTextContainsAll)
when arguments is [_, SqlExpression property, var keywords]
=> GetKeywords(keywords) is IEnumerable<SqlExpression> values
? sqlExpressionFactory.Function(
method.Name == nameof(CosmosDbFunctionsExtensions.FullTextContainsAny) ? "FullTextContainsAny" : "FullTextContainsAll",
[property, .. values],
typeof(bool),
typeMappingSource.FindMapping(typeof(bool)))
: null,

nameof(CosmosDbFunctionsExtensions.FullTextScore)
when arguments is [_, { } property, SqlParameterExpression { Type: var keywordClrType } keywords]
&& keywordClrType == typeof(string[]) => sqlExpressionFactory.ScoringFunction(
"FullTextScore",
[property, keywords],
typeof(double),
typeMappingSource.FindMapping(typeof(double))),
nameof(CosmosDbFunctionsExtensions.FullTextScore) when arguments is [_, SqlExpression property, var keywords]
=> GetKeywords(keywords) is IEnumerable<SqlExpression> values
? sqlExpressionFactory.ScoringFunction(
"FullTextScore",
[property, .. values],
typeof(double),
typeMappingSource.FindMapping(typeof(double)))
: null,

nameof(CosmosDbFunctionsExtensions.FullTextScore)
when arguments is [_, { } property, ArrayConstantExpression keywords] => sqlExpressionFactory.ScoringFunction(
"FullTextScore",
[property, .. keywords.Items],
nameof(CosmosDbFunctionsExtensions.Rrf) when arguments is [_, ArrayConstantExpression functions]
=> sqlExpressionFactory.ScoringFunction(
"RRF",
functions.Items,
typeof(double),
typeMappingSource.FindMapping(typeof(double))),

nameof(CosmosDbFunctionsExtensions.Rrf)
when arguments is [_, ArrayConstantExpression functions] => sqlExpressionFactory.ScoringFunction(
nameof(CosmosDbFunctionsExtensions.Rrf) when arguments is [_, ArrayConstantExpression functions, var weights]
=> sqlExpressionFactory.ScoringFunction(
"RRF",
functions.Items,
[.. functions.Items, weights],
typeof(double),
typeMappingSource.FindMapping(typeof(double))),

nameof(CosmosDbFunctionsExtensions.FullTextContainsAny) or nameof(CosmosDbFunctionsExtensions.FullTextContainsAll)
when arguments is
[_, { } property, SqlConstantExpression { Type: var keywordClrType, Value: string[] values }]
&& keywordClrType == typeof(string[]) => sqlExpressionFactory.Function(
method.Name == nameof(CosmosDbFunctionsExtensions.FullTextContainsAny) ? "FullTextContainsAny" : "FullTextContainsAll",
[property, .. values.Select(x => sqlExpressionFactory.Constant(x))],
typeof(bool),
typeMappingSource.FindMapping(typeof(bool))),

nameof(CosmosDbFunctionsExtensions.FullTextContainsAny) or nameof(CosmosDbFunctionsExtensions.FullTextContainsAll)
when arguments is [_, { } property, SqlParameterExpression { Type: var keywordClrType } keywords]
&& keywordClrType == typeof(string[]) => sqlExpressionFactory.Function(
method.Name == nameof(CosmosDbFunctionsExtensions.FullTextContainsAny) ? "FullTextContainsAny" : "FullTextContainsAll",
[property, keywords],
typeof(bool),
typeMappingSource.FindMapping(typeof(bool))),
_ => throw new UnreachableException()
};

nameof(CosmosDbFunctionsExtensions.FullTextContainsAny) or nameof(CosmosDbFunctionsExtensions.FullTextContainsAll)
when arguments is [_, { } property, ArrayConstantExpression keywords] => sqlExpressionFactory.Function(
method.Name == nameof(CosmosDbFunctionsExtensions.FullTextContainsAny) ? "FullTextContainsAny" : "FullTextContainsAll",
[property, .. keywords.Items],
typeof(bool),
typeMappingSource.FindMapping(typeof(bool))),
IEnumerable<SqlExpression>? GetKeywords(Expression inputExpression)
=> inputExpression switch
{
SqlConstantExpression { Value: string[] v } => v.Select(x => sqlExpressionFactory.Constant(x)),
SqlParameterExpression { Type: var keywordClrType } v when keywordClrType == typeof(string[]) => [v],
ArrayConstantExpression v => v.Items,

_ => null
};
_ => null
};
}
}
Loading
Loading