Skip to content

Commit 993bc56

Browse files
committed
split FTS annotations into two: isEnabled and language
1 parent 65cc1e5 commit 993bc56

File tree

13 files changed

+247
-78
lines changed

13 files changed

+247
-78
lines changed

src/EFCore.Cosmos/Extensions/CosmosPropertyBuilderExtensions.cs

Lines changed: 100 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -245,26 +245,28 @@ private static CosmosVectorType CreateVectorType(DistanceFunction distanceFuncti
245245
CoreStrings.InvalidEnumValue(distanceFunction, nameof(distanceFunction), typeof(DistanceFunction)));
246246

247247
/// <summary>
248-
/// Configures the property to be used with a full-text search.
248+
/// Enables full-text search for this property using a specified language.
249249
/// </summary>
250250
/// <remarks>
251251
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
252252
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
253253
/// </remarks>
254254
/// <param name="propertyBuilder">The builder for the property being configured.</param>
255-
/// <param name="language">The language for the full-text search.</param>
255+
/// <param name="language">The language used for full-text search. Setting this to (<see langword="null" /> will use the default language for the container, or "en-US" if default language was not specified.</param>
256256
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
257257
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
258-
public static PropertyBuilder IsFullTextProperty(
258+
public static PropertyBuilder EnableFullTextSearch(
259259
this PropertyBuilder propertyBuilder,
260260
string? language = null)
261261
{
262+
propertyBuilder.Metadata.SetIsFullTextSearchEnabled(true);
262263
propertyBuilder.Metadata.SetFullTextSearchLanguage(language);
264+
263265
return propertyBuilder;
264266
}
265267

266268
/// <summary>
267-
/// Configures the property to enable full-text search for Azure Cosmos DB using a specified language.
269+
/// Enables full-text search for this property using a specified language.
268270
/// </summary>
269271
/// <remarks>
270272
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
@@ -276,62 +278,142 @@ public static PropertyBuilder IsFullTextProperty(
276278
/// </remarks>
277279
/// <typeparam name="TProperty">The type of the property being configured.</typeparam>
278280
/// <param name="propertyBuilder">The builder for the property being configured.</param>
279-
/// <param name="language">The language for the full-text search.</param>
281+
/// <param name="language">The language used for full-text search. Setting this to (<see langword="null" /> will use the default language for the container, or "en-US" if default language was not specified.</param>
280282
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
281283
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
282-
public static PropertyBuilder<TProperty> IsFullTextProperty<TProperty>(
284+
public static PropertyBuilder<TProperty> EnableFullTextSearch<TProperty>(
283285
this PropertyBuilder<TProperty> propertyBuilder,
284286
string? language = null)
285-
=> (PropertyBuilder<TProperty>)IsFullTextProperty((PropertyBuilder)propertyBuilder, language);
287+
=> (PropertyBuilder<TProperty>)EnableFullTextSearch((PropertyBuilder)propertyBuilder, language);
286288

287289
/// <summary>
288-
/// Configures the property to enable full-text search for Azure Cosmos DB using a specified language.
290+
/// Enables full-text search for this property using a specified language.
289291
/// </summary>
290292
/// <remarks>
291293
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
292294
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
293295
/// </remarks>
294296
/// <param name="propertyBuilder">The builder for the property being configured.</param>
295-
/// <param name="language">The language for the full-text search.</param>
297+
/// <param name="language">The language used for full-text search. Setting this to (<see langword="null" /> will use the default language for the container, or "en-US" if default language was not specified.</param>
296298
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
297299
/// <returns>
298300
/// The same builder instance if the configuration was applied,
299301
/// <see langword="null" /> otherwise.
300302
/// </returns>
301303
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
302-
public static IConventionPropertyBuilder? IsFullTextProperty(
304+
public static IConventionPropertyBuilder? EnableFullTextSearch(
303305
this IConventionPropertyBuilder propertyBuilder,
304306
string? language,
305307
bool fromDataAnnotation = false)
306308
{
307-
if (!propertyBuilder.CanSetIsFullTextProperty(language, fromDataAnnotation))
309+
if (!propertyBuilder.CanSetEnableFullTextSearch(language, fromDataAnnotation))
308310
{
309311
return null;
310312
}
311313

314+
propertyBuilder.Metadata.SetIsFullTextSearchEnabled(true, fromDataAnnotation);
312315
propertyBuilder.Metadata.SetFullTextSearchLanguage(language, fromDataAnnotation);
313316

314317
return propertyBuilder;
315318
}
316319

317320
/// <summary>
318-
/// Returns a value indicating whether the property can be used with full-text search using a specified language.
321+
/// Returns a value indicating whether full-text search can be enabled for this property using a specified language.
319322
/// </summary>
320323
/// <remarks>
321324
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
322325
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
323326
/// </remarks>
324327
/// <param name="propertyBuilder">The builder for the property being configured.</param>
325-
/// <param name="language">The language for the full-text search.</param>
328+
/// <param name="language">The language for full-text search.</param>
326329
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
327330
/// <returns><see langword="true" /> if the vector type can be set.</returns>
328331
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
329-
public static bool CanSetIsFullTextProperty(
332+
public static bool CanSetEnableFullTextSearch(
330333
this IConventionPropertyBuilder propertyBuilder,
331334
string? language,
332335
bool fromDataAnnotation = false)
333-
=> propertyBuilder.CanSetAnnotation(
334-
CosmosAnnotationNames.FullTextSearchLanguage,
335-
language,
336-
fromDataAnnotation);
336+
=> propertyBuilder.CanSetAnnotation(CosmosAnnotationNames.IsFullTextSearchEnabled, true, fromDataAnnotation)
337+
&& propertyBuilder.CanSetAnnotation(CosmosAnnotationNames.FullTextSearchLanguage, language, fromDataAnnotation);
338+
339+
/// <summary>
340+
/// Disables full-text search, if it was prevously enabled for this property.
341+
/// </summary>
342+
/// <remarks>
343+
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
344+
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
345+
/// </remarks>
346+
/// <param name="propertyBuilder">The builder for the property being configured.</param>
347+
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
348+
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
349+
public static PropertyBuilder DisableFullTextSearch(
350+
this PropertyBuilder propertyBuilder)
351+
{
352+
propertyBuilder.Metadata.SetIsFullTextSearchEnabled(false);
353+
354+
return propertyBuilder;
355+
}
356+
357+
/// <summary>
358+
/// Disables full-text search, if it was prevously enabled for this property.
359+
/// </summary>
360+
/// <remarks>
361+
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
362+
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
363+
/// </remarks>
364+
/// <remarks>
365+
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
366+
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
367+
/// </remarks>
368+
/// <typeparam name="TProperty">The type of the property being configured.</typeparam>
369+
/// <param name="propertyBuilder">The builder for the property being configured.</param>
370+
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
371+
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
372+
public static PropertyBuilder<TProperty> DisableFullTextSearch<TProperty>(
373+
this PropertyBuilder<TProperty> propertyBuilder)
374+
=> (PropertyBuilder<TProperty>)DisableFullTextSearch((PropertyBuilder)propertyBuilder);
375+
376+
/// <summary>
377+
/// Disables full-text search, if it was prevously enabled for this property.
378+
/// </summary>
379+
/// <remarks>
380+
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
381+
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
382+
/// </remarks>
383+
/// <param name="propertyBuilder">The builder for the property being configured.</param>
384+
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
385+
/// <returns>
386+
/// The same builder instance if the configuration was applied,
387+
/// <see langword="null" /> otherwise.
388+
/// </returns>
389+
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
390+
public static IConventionPropertyBuilder? DisableFullTextSearch(
391+
this IConventionPropertyBuilder propertyBuilder,
392+
bool fromDataAnnotation = false)
393+
{
394+
if (!propertyBuilder.CanSetDisableFullTextSearch(fromDataAnnotation))
395+
{
396+
return null;
397+
}
398+
399+
propertyBuilder.Metadata.SetIsFullTextSearchEnabled(false, fromDataAnnotation);
400+
401+
return propertyBuilder;
402+
}
403+
404+
/// <summary>
405+
/// Returns a value indicating whether full-text search can be disabled for this property.
406+
/// </summary>
407+
/// <remarks>
408+
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
409+
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
410+
/// </remarks>
411+
/// <param name="propertyBuilder">The builder for the property being configured.</param>
412+
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
413+
/// <returns><see langword="true" /> if the vector type can be set.</returns>
414+
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
415+
public static bool CanSetDisableFullTextSearch(
416+
this IConventionPropertyBuilder propertyBuilder,
417+
bool fromDataAnnotation = false)
418+
=> propertyBuilder.CanSetAnnotation(CosmosAnnotationNames.IsFullTextSearchEnabled, false, fromDataAnnotation);
337419
}

src/EFCore.Cosmos/Extensions/CosmosPropertyExtensions.cs

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,32 +129,72 @@ public static void SetVectorType(this IMutableProperty property, CosmosVectorTyp
129129
public static ConfigurationSource? GetVectorTypeConfigurationSource(this IConventionProperty property)
130130
=> property.FindAnnotation(CosmosAnnotationNames.VectorType)?.GetConfigurationSource();
131131

132+
/// <summary>
133+
/// Returns the value indicating whether full-text search is enabled for this property.
134+
/// </summary>
135+
/// <param name="property">The property.</param>
136+
/// <returns><see langword="true" /> if full-text search is enabled for this property, <see langword="false" /> otherwise.</returns>
137+
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
138+
public static bool? GetIsFullTextSearchEnabled(this IReadOnlyProperty property)
139+
=> (bool?)property[CosmosAnnotationNames.IsFullTextSearchEnabled];
140+
141+
/// <summary>
142+
/// Enables full-text search for this property.
143+
/// </summary>
144+
/// <param name="property">The property.</param>
145+
/// <param name="enabled">Indicates whether full-text search is enabled for the property.</param>
146+
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
147+
public static void SetIsFullTextSearchEnabled(this IMutableProperty property, bool enabled)
148+
=> property.SetAnnotation(CosmosAnnotationNames.IsFullTextSearchEnabled, enabled);
149+
150+
/// <summary>
151+
/// Enables full-text search for this property.
152+
/// </summary>
153+
/// <param name="property">The property.</param>
154+
/// <param name="enabled">Indicates whether full-text search is enabled for the property.</param>
155+
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
156+
/// <returns>The configured value.</returns>
157+
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
158+
public static string? SetIsFullTextSearchEnabled(
159+
this IConventionProperty property,
160+
bool enabled,
161+
bool fromDataAnnotation = false)
162+
=> (string?)property.SetAnnotation(
163+
CosmosAnnotationNames.IsFullTextSearchEnabled,
164+
enabled,
165+
fromDataAnnotation)?.Value;
166+
167+
/// <summary>
168+
/// Gets the <see cref="ConfigurationSource" /> for enabling full-text search for this property.
169+
/// </summary>
170+
/// <param name="property">The property.</param>
171+
/// <returns>
172+
/// The <see cref="ConfigurationSource" /> for enabling full-text search for this property.
173+
/// </returns>
174+
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
175+
public static ConfigurationSource? GetIsFullTextSearchEnabledConfigurationSource(this IConventionProperty property)
176+
=> property.FindAnnotation(CosmosAnnotationNames.IsFullTextSearchEnabled)?.GetConfigurationSource();
177+
132178
/// <summary>
133179
/// Returns the full-text search language defined for this property.
134180
/// </summary>
135181
/// <param name="property">The property.</param>
136-
/// <returns>Returns the definition of the vector stored in this property.</returns>
182+
/// <returns>The full-text search language for this property.</returns>
137183
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
138184
public static string? GetFullTextSearchLanguage(this IReadOnlyProperty property)
139-
{
140-
var annotation = property.FindAnnotation(CosmosAnnotationNames.FullTextSearchLanguage);
141-
142-
return annotation != null
143-
? (string?)annotation.Value ?? (property.DeclaringType as IReadOnlyEntityType)?.GetDefaultFullTextSearchLanguage() ?? "en-US"
144-
: null;
145-
}
185+
=> (string?)property[CosmosAnnotationNames.FullTextSearchLanguage];
146186

147187
/// <summary>
148188
/// Sets the full-text search language defined for this property.
149189
/// </summary>
150190
/// <param name="property">The property.</param>
151-
/// <param name="language">The full-text search language for the property.</param>
191+
/// <param name="language">The full-text search language for this property.</param>
152192
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
153193
public static void SetFullTextSearchLanguage(this IMutableProperty property, string? language)
154194
=> property.SetAnnotation(CosmosAnnotationNames.FullTextSearchLanguage, language);
155195

156196
/// <summary>
157-
/// Sets the definition of the vector stored in this property.
197+
/// Sets the full-text search language defined for this property.
158198
/// </summary>
159199
/// <param name="property">The property.</param>
160200
/// <param name="language">The full-text search language for the property.</param>

src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,15 @@ protected virtual void ValidateIndexes(
594594
}
595595
else if (index.IsFullTextIndex() == true)
596596
{
597-
// allow these, validation happens during container creation
597+
// composite vector validation is done during container creation
598+
if (index.Properties[0].GetIsFullTextSearchEnabled() != true)
599+
{
600+
throw new InvalidOperationException(
601+
CosmosStrings.FullTextIndexOnNonFullTextProperty(
602+
index.DeclaringEntityType.DisplayName(),
603+
index.Properties[0].Name,
604+
nameof(CosmosPropertyBuilderExtensions.EnableFullTextSearch)));
605+
}
598606
}
599607
else
600608
{

src/EFCore.Cosmos/Metadata/Internal/CosmosAnnotationNames.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ public static class CosmosAnnotationNames
6363
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
6464
public const string DefaultFullTextSearchLanguage = Prefix + "DefaultFullTextSearchLanguage";
6565

66+
/// <summary>
67+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
68+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
69+
/// any release. You should only use it directly in your code with extreme caution and knowing that
70+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
71+
/// </summary>
72+
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
73+
public const string IsFullTextSearchEnabled = Prefix + "IsFullTextSearchEnabled";
74+
6675
/// <summary>
6776
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
6877
/// the same compatibility standards as public APIs. It may be changed or removed without notice in

src/EFCore.Cosmos/Properties/CosmosStrings.Designer.cs

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)