Skip to content

Commit fad7f74

Browse files
committed
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)
1 parent da1edaf commit fad7f74

File tree

5 files changed

+46
-7
lines changed

5 files changed

+46
-7
lines changed

src/EFCore.Cosmos/Extensions/CosmosDbFunctionsExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ public static bool FullTextContainsAny(this DbFunctions _, string property, para
8989
/// Returns the full-text search score for the specified property and keywords.
9090
/// </summary>
9191
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
92-
/// <param name="property">The property to search.</param>
93-
/// <param name="keywords">The keywords to search for.</param>
92+
/// <param name="property">The property to score.</param>
93+
/// <param name="keywords">The keywords to score by.</param>
9494
/// <returns>The full-text search score.</returns>
9595
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
96-
public static double FullTextScore(this DbFunctions _, string property, string[] keywords)
96+
public static double FullTextScore(this DbFunctions _, string property, params string[] keywords)
9797
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(FullTextScore)));
9898

9999
/// <summary>

src/EFCore.Cosmos/Query/Internal/CosmosShapedQueryCompilingExpressionVisitor.InExpressionValuesExpandingExpressionVisitor.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protected override Expression VisitExtension(Expression expression)
8585
return base.VisitExtension(expression);
8686
}
8787

88-
// Inlines array parameter of full-text functions, transforming FullTextContains(x, @keywordsArray) to FullTextContains(x, keyword1, keyword2))
88+
// Inlines array parameter of full-text functions, transforming FullTextContainsAll(x, @keywordsArray) to FullTextContainsAll(x, keyword1, keyword2))
8989
case SqlFunctionExpression
9090
{
9191
Name: "FullTextContainsAny" or "FullTextContainsAll",
@@ -106,6 +106,29 @@ protected override Expression VisitExtension(Expression expression)
106106
fullTextContainsAllAnyFunction.TypeMapping);
107107
}
108108

109+
// Inlines array parameter of full-text score, transforming FullTextScore(x, @keywordsArray) to FullTextScore(x, [keyword1, keyword2]))
110+
case SqlFunctionExpression
111+
{
112+
Name: "FullTextScore",
113+
IsScoringFunction: true,
114+
Arguments: [var property, SqlParameterExpression { TypeMapping: { ElementTypeMapping: not null } typeMapping } keywords]
115+
} fullTextScoreFunction
116+
when !UseOldBehavior35476:
117+
{
118+
var keywordValues = new List<string>();
119+
foreach (var value in (IEnumerable)parametersValues[keywords.Name])
120+
{
121+
keywordValues.Add((string)value);
122+
}
123+
124+
return new SqlFunctionExpression(
125+
fullTextScoreFunction.Name,
126+
isScoringFunction: true,
127+
[property, sqlExpressionFactory.Constant(keywordValues, typeMapping)],
128+
fullTextScoreFunction.Type,
129+
fullTextScoreFunction.TypeMapping);
130+
}
131+
109132
default:
110133
return expression;
111134
}

src/EFCore.Cosmos/Query/Internal/Expressions/FragmentExpression.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ public class FragmentExpression(string fragment) : Expression, IPrintableExpress
2323
/// </summary>
2424
public virtual string Fragment { get; } = fragment;
2525

26+
/// <inheritdoc />
27+
public override ExpressionType NodeType
28+
=> base.NodeType;
29+
30+
/// <inheritdoc />
31+
public override Type Type
32+
=> typeof(object);
33+
2634
/// <inheritdoc />
2735
protected override Expression VisitChildren(ExpressionVisitor visitor)
2836
=> this;

src/EFCore.Cosmos/Query/Internal/Expressions/SqlFunctionExpression.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ protected override Expression VisitChildren(ExpressionVisitor visitor)
9292
}
9393

9494
return changed
95-
? new SqlFunctionExpression(Name, arguments, Type, TypeMapping)
95+
? new SqlFunctionExpression(Name, IsScoringFunction, arguments, Type, TypeMapping)
9696
: this;
9797
}
9898

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

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

119119
/// <summary>
120120
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to

src/EFCore.Cosmos/Query/Internal/Translators/CosmosFullTextSearchTranslator.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ public class CosmosFullTextSearchTranslator(ISqlExpressionFactory sqlExpressionF
6565
typeof(double),
6666
typeMappingSource.FindMapping(typeof(double))),
6767

68+
nameof(CosmosDbFunctionsExtensions.FullTextContainsAny) or nameof(CosmosDbFunctionsExtensions.FullTextContainsAll)
69+
when arguments is [_, SqlExpression property, SqlConstantExpression { Type: var keywordClrType, Value: string[] values } keywords]
70+
&& keywordClrType == typeof(string[]) => sqlExpressionFactory.Function(
71+
method.Name == nameof(CosmosDbFunctionsExtensions.FullTextContainsAny) ? "FullTextContainsAny" : "FullTextContainsAll",
72+
[property, .. values.Select(x => sqlExpressionFactory.Constant(x))],
73+
typeof(bool),
74+
typeMappingSource.FindMapping(typeof(bool))),
75+
6876
nameof(CosmosDbFunctionsExtensions.FullTextContainsAny) or nameof(CosmosDbFunctionsExtensions.FullTextContainsAll)
6977
when arguments is [_, SqlExpression property, SqlParameterExpression { Type: var keywordClrType } keywords]
7078
&& keywordClrType == typeof(string[]) => sqlExpressionFactory.Function(

0 commit comments

Comments
 (0)