Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
fix FTContainsAll/Any for constants,
change TFScore signature to accept params,
fixed SqlFunctionExpression to use the new ctor internally,
added mandatory property overrides for FragmentExpression (vector search)
fixed parameter inliner to not match parameter of type string, but only string[] when processing ContainsAll/Any
  • Loading branch information
maumar committed Apr 14, 2025
commit 07c35168e20deadc5710879e5bff78775f63e61f
6 changes: 3 additions & 3 deletions src/EFCore.Cosmos/Extensions/CosmosDbFunctionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ public static bool FullTextContainsAny(this DbFunctions _, string property, para
/// Returns the full-text search score for the specified property and keywords.
/// </summary>
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
/// <param name="property">The property to search.</param>
/// <param name="keywords">The keywords to search for.</param>
/// <param name="property">The property to score.</param>
/// <param name="keywords">The keywords to score by.</param>
/// <returns>The full-text search score.</returns>
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
public static double FullTextScore(this DbFunctions _, string property, string[] keywords)
public static double FullTextScore(this DbFunctions _, string property, params string[] keywords)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(FullTextScore)));

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ protected override Expression VisitExtension(Expression expression)
return base.VisitExtension(expression);
}

// Inlines array parameter of full-text functions, transforming FullTextContains(x, @keywordsArray) to FullTextContains(x, keyword1, keyword2))
// Inlines array parameter of full-text functions, transforming FullTextContainsAll(x, @keywordsArray) to FullTextContainsAll(x, keyword1, keyword2))
case SqlFunctionExpression
{
Name: "FullTextContainsAny" or "FullTextContainsAll",
Arguments: [var property, SqlParameterExpression { TypeMapping: { ElementTypeMapping: var elementTypeMapping } } keywords]
Arguments: [var property, SqlParameterExpression { TypeMapping: { ElementTypeMapping: var elementTypeMapping }, Type: Type type } keywords]
} fullTextContainsAllAnyFunction
when !UseOldBehavior35476:
when !UseOldBehavior35476 && type == typeof(string[]):
{
var keywordValues = new List<SqlExpression>();
foreach (var value in (IEnumerable)parametersValues[keywords.Name])
Expand All @@ -106,6 +106,29 @@ protected override Expression VisitExtension(Expression expression)
fullTextContainsAllAnyFunction.TypeMapping);
}

// Inlines array parameter of full-text score, transforming FullTextScore(x, @keywordsArray) to FullTextScore(x, [keyword1, keyword2]))
case SqlFunctionExpression
{
Name: "FullTextScore",
IsScoringFunction: true,
Arguments: [var property, SqlParameterExpression { TypeMapping: { ElementTypeMapping: not null } typeMapping } keywords]
} fullTextScoreFunction
when !UseOldBehavior35476:
{
var keywordValues = new List<string>();
foreach (var value in (IEnumerable)parametersValues[keywords.Name])
{
keywordValues.Add((string)value);
}

return new SqlFunctionExpression(
fullTextScoreFunction.Name,
isScoringFunction: true,
[property, sqlExpressionFactory.Constant(keywordValues, typeMapping)],
fullTextScoreFunction.Type,
fullTextScoreFunction.TypeMapping);
}

default:
return expression;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ public class FragmentExpression(string fragment) : Expression, IPrintableExpress
/// </summary>
public virtual string Fragment { get; } = fragment;

/// <inheritdoc />
public override ExpressionType NodeType
=> base.NodeType;

/// <inheritdoc />
public override Type Type
=> typeof(object);

/// <inheritdoc />
protected override Expression VisitChildren(ExpressionVisitor visitor)
=> this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected override Expression VisitChildren(ExpressionVisitor visitor)
}

return changed
? new SqlFunctionExpression(Name, arguments, Type, TypeMapping)
? new SqlFunctionExpression(Name, IsScoringFunction, arguments, Type, TypeMapping)
: this;
}

Expand All @@ -103,7 +103,7 @@ protected override Expression VisitChildren(ExpressionVisitor visitor)
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual SqlFunctionExpression ApplyTypeMapping(CoreTypeMapping? typeMapping)
=> new(Name, Arguments, Type, typeMapping ?? TypeMapping);
=> new(Name, IsScoringFunction, Arguments, Type, typeMapping ?? TypeMapping);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -114,7 +114,7 @@ public virtual SqlFunctionExpression ApplyTypeMapping(CoreTypeMapping? typeMappi
public virtual SqlFunctionExpression Update(IReadOnlyList<Expression> arguments)
=> arguments.SequenceEqual(Arguments)
? this
: new SqlFunctionExpression(Name, arguments, Type, TypeMapping);
: new SqlFunctionExpression(Name, IsScoringFunction, arguments, Type, TypeMapping);

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ public class CosmosFullTextSearchTranslator(ISqlExpressionFactory sqlExpressionF
typeof(double),
typeMappingSource.FindMapping(typeof(double))),

nameof(CosmosDbFunctionsExtensions.FullTextContainsAny) or nameof(CosmosDbFunctionsExtensions.FullTextContainsAll)
when arguments is [_, SqlExpression property, SqlConstantExpression { Type: var keywordClrType, Value: string[] values } keywords]
&& 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 [_, SqlExpression property, SqlParameterExpression { Type: var keywordClrType } keywords]
&& keywordClrType == typeof(string[]) => sqlExpressionFactory.Function(
Expand Down