-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
Copy pathCosmosDbFunctionsExtensions.cs
267 lines (249 loc) · 14.9 KB
/
CosmosDbFunctionsExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics.CodeAnalysis;
namespace Microsoft.EntityFrameworkCore.Cosmos.Extensions;
/// <summary>
/// Provides CLR methods that get translated to database functions when used in LINQ to Entities queries.
/// The methods on this class are accessed via <see cref="EF.Functions" />.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-database-functions">Database functions</see>, and
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Cosmos with EF Core</see> for more information and examples.
/// </remarks>
public static class CosmosDbFunctionsExtensions
{
/// <summary>
/// Returns a boolean indicating if the property has been assigned a value. Corresponds to the Cosmos <c>IS_DEFINED</c> function.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-database-functions">Database functions</see>, and
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Cosmos with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
/// <param name="expression">The expression to check.</param>
/// <seealso href="https://learn.microsoft.com/azure/cosmos-db/nosql/query/is-defined">Cosmos <c>IS_DEFINED_</c> function</seealso>
public static bool IsDefined(this DbFunctions _, object? expression)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(IsDefined)));
/// <summary>
/// Coalesces a Cosmos <c>undefined</c> value via the <c>??</c> operator.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-database-functions">Database functions</see>, and
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Cosmos with EF Core</see>
/// for more information and examples.
/// </remarks>
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
/// <param name="expression1">
/// The expression to coalesce. This expression will be returned unless it is <c>undefined</c>, in which case
/// <paramref name="expression2" /> will be returned.
/// </param>
/// <param name="expression2">The expression to be returned if <paramref name="expression1" /> is <c>undefined</c>.</param>
/// <seealso href="https://learn.microsoft.com/azure/cosmos-db/nosql/query/ternary-coalesce-operators#coalesce-operator">
/// Cosmos coalesce operator
/// </seealso>
public static T CoalesceUndefined<T>(
this DbFunctions _,
T expression1,
T expression2)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(CoalesceUndefined)));
/// <summary>
/// Checks if the specified property contains the given keyword using full-text search.
/// </summary>
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
/// <param name="property">The property to search.</param>
/// <param name="keyword">The keyword to search for.</param>
/// <returns><see langword="true" /> if the property contains the keyword; otherwise, <see langword="false" />.</returns>
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
public static bool FullTextContains(this DbFunctions _, string property, string keyword)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(FullTextContains)));
/// <summary>
/// Checks if the specified property contains all the given keywords using full-text search.
/// </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>
/// <returns><see langword="true" /> if the property contains all the keywords; otherwise, <see langword="false" />.</returns>
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
public static bool FullTextContainsAll(this DbFunctions _, string property, params string[] keywords)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(FullTextContainsAll)));
/// <summary>
/// Checks if the specified property contains any of the given keywords using full-text search.
/// </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>
/// <returns><see langword="true" /> if the property contains any of the keywords; otherwise, <see langword="false" />.</returns>
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
public static bool FullTextContainsAny(this DbFunctions _, string property, params string[] keywords)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(FullTextContainsAny)));
/// <summary>
/// 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 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, params string[] keywords)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(FullTextScore)));
/// <summary>
/// Combines scores provided by two or more specified functions.
/// </summary>
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
/// <param name="functions">The functions to compute the score for.</param>
/// <returns>The combined score.</returns>
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
public static double Rrf(this DbFunctions _, params double[] functions)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(Rrf)));
/// <summary>
/// Returns the distance between two vectors, using the distance function and data type defined using
/// <see
/// cref="CosmosPropertyBuilderExtensions.IsVector(Microsoft.EntityFrameworkCore.Metadata.Builders.PropertyBuilder,Microsoft.Azure.Cosmos.DistanceFunction,int)" />
/// .
/// </summary>
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
/// <param name="vector1">The first vector.</param>
/// <param name="vector2">The second vector.</param>
[Experimental(EFDiagnostics.CosmosVectorSearchExperimental)]
public static double VectorDistance(this DbFunctions _, ReadOnlyMemory<byte> vector1, ReadOnlyMemory<byte> vector2)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(VectorDistance)));
/// <summary>
/// Returns the distance between two vectors, given a distance function (aka similarity measure).
/// </summary>
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
/// <param name="vector1">The first vector.</param>
/// <param name="vector2">The second vector.</param>
/// <param name="useBruteForce">
/// A <see langword="bool" /> specifying how the computed value is used in an ORDER BY
/// expression. If <see langword="true" />, then brute force is used, otherwise any index defined on the vector
/// property is leveraged.
/// </param>
[Experimental(EFDiagnostics.CosmosVectorSearchExperimental)]
public static double VectorDistance(
this DbFunctions _,
ReadOnlyMemory<byte> vector1,
ReadOnlyMemory<byte> vector2,
[NotParameterized] bool useBruteForce)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(VectorDistance)));
/// <summary>
/// Returns the distance between two vectors, given a distance function (aka similarity measure).
/// </summary>
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
/// <param name="vector1">The first vector.</param>
/// <param name="vector2">The second vector.</param>
/// <param name="distanceFunction">The distance function to use.</param>
/// <param name="useBruteForce">
/// A <see langword="bool" /> specifying how the computed value is used in an ORDER BY
/// expression. If <see langword="true" />, then brute force is used, otherwise any index defined on the vector
/// property is leveraged.
/// </param>
[Experimental(EFDiagnostics.CosmosVectorSearchExperimental)]
public static double VectorDistance(
this DbFunctions _,
ReadOnlyMemory<byte> vector1,
ReadOnlyMemory<byte> vector2,
[NotParameterized] bool useBruteForce,
[NotParameterized] DistanceFunction distanceFunction)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(VectorDistance)));
/// <summary>
/// Returns the distance between two vectors, using the distance function and data type defined using
/// <see
/// cref="CosmosPropertyBuilderExtensions.IsVector(Microsoft.EntityFrameworkCore.Metadata.Builders.PropertyBuilder,Microsoft.Azure.Cosmos.DistanceFunction,int)" />
/// .
/// </summary>
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
/// <param name="vector1">The first vector.</param>
/// <param name="vector2">The second vector.</param>
[Experimental(EFDiagnostics.CosmosVectorSearchExperimental)]
public static double VectorDistance(this DbFunctions _, ReadOnlyMemory<sbyte> vector1, ReadOnlyMemory<sbyte> vector2)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(VectorDistance)));
/// <summary>
/// Returns the distance between two vectors, given a distance function (aka similarity measure).
/// </summary>
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
/// <param name="vector1">The first vector.</param>
/// <param name="vector2">The second vector.</param>
/// <param name="useBruteForce">
/// A <see langword="bool" /> specifying how the computed value is used in an ORDER BY
/// expression. If <see langword="true" />, then brute force is used, otherwise any index defined on the vector
/// property is leveraged.
/// </param>
[Experimental(EFDiagnostics.CosmosVectorSearchExperimental)]
public static double VectorDistance(
this DbFunctions _,
ReadOnlyMemory<sbyte> vector1,
ReadOnlyMemory<sbyte> vector2,
[NotParameterized] bool useBruteForce)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(VectorDistance)));
/// <summary>
/// Returns the distance between two vectors, given a distance function (aka similarity measure).
/// </summary>
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
/// <param name="vector1">The first vector.</param>
/// <param name="vector2">The second vector.</param>
/// <param name="distanceFunction">The distance function to use.</param>
/// <param name="useBruteForce">
/// A <see langword="bool" /> specifying how the computed value is used in an ORDER BY
/// expression. If <see langword="true" />, then brute force is used, otherwise any index defined on the vector
/// property is leveraged.
/// </param>
[Experimental(EFDiagnostics.CosmosVectorSearchExperimental)]
public static double VectorDistance(
this DbFunctions _,
ReadOnlyMemory<sbyte> vector1,
ReadOnlyMemory<sbyte> vector2,
[NotParameterized] bool useBruteForce,
[NotParameterized] DistanceFunction distanceFunction)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(VectorDistance)));
/// <summary>
/// Returns the distance between two vectors, using the distance function and data type defined using
/// <see
/// cref="CosmosPropertyBuilderExtensions.IsVector(Microsoft.EntityFrameworkCore.Metadata.Builders.PropertyBuilder,Microsoft.Azure.Cosmos.DistanceFunction,int)" />
/// .
/// </summary>
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
/// <param name="vector1">The first vector.</param>
/// <param name="vector2">The second vector.</param>
[Experimental(EFDiagnostics.CosmosVectorSearchExperimental)]
public static double VectorDistance(this DbFunctions _, ReadOnlyMemory<float> vector1, ReadOnlyMemory<float> vector2)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(VectorDistance)));
/// <summary>
/// Returns the distance between two vectors, given a distance function (aka similarity measure).
/// </summary>
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
/// <param name="vector1">The first vector.</param>
/// <param name="vector2">The second vector.</param>
/// <param name="useBruteForce">
/// A <see langword="bool" /> specifying how the computed value is used in an ORDER BY
/// expression. If <see langword="true" />, then brute force is used, otherwise any index defined on the vector
/// property is leveraged.
/// </param>
[Experimental(EFDiagnostics.CosmosVectorSearchExperimental)]
public static double VectorDistance(
this DbFunctions _,
ReadOnlyMemory<float> vector1,
ReadOnlyMemory<float> vector2,
[NotParameterized] bool useBruteForce)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(VectorDistance)));
/// <summary>
/// Returns the distance between two vectors, given a distance function (aka similarity measure).
/// </summary>
/// <param name="_">The <see cref="DbFunctions" /> instance.</param>
/// <param name="vector1">The first vector.</param>
/// <param name="vector2">The second vector.</param>
/// <param name="distanceFunction">The distance function to use.</param>
/// <param name="useBruteForce">
/// A <see langword="bool" /> specifying how the computed value is used in an ORDER BY
/// expression. If <see langword="true" />, then brute force is used, otherwise any index defined on the vector
/// property is leveraged.
/// </param>
[Experimental(EFDiagnostics.CosmosVectorSearchExperimental)]
public static double VectorDistance(
this DbFunctions _,
ReadOnlyMemory<float> vector1,
ReadOnlyMemory<float> vector2,
[NotParameterized] bool useBruteForce,
[NotParameterized] DistanceFunction distanceFunction)
=> throw new InvalidOperationException(CoreStrings.FunctionOnClient(nameof(VectorDistance)));
}