Skip to content

Cosmos Full Text Search support #35868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 15, 2025
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
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<PackageVersion Include="Microsoft.DotNet.Build.Tasks.Templating" Version="$(MicrosoftDotNetBuildTasksTemplatingVersion)" />

<!-- Azure SDK for .NET dependencies -->
<PackageVersion Include="Microsoft.Azure.Cosmos" Version="3.46.0" />
<PackageVersion Include="Microsoft.Azure.Cosmos" Version="3.49.0-preview.0" />

<!-- SQL Server dependencies -->
<PackageVersion Include="Microsoft.Data.SqlClient" Version="6.0.1" />
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Cosmos/EFCore.Cosmos.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>Azure Cosmos provider for Entity Framework Core.</Description>
Expand Down
49 changes: 49 additions & 0 deletions src/EFCore.Cosmos/Extensions/CosmosDbFunctionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,55 @@ public static T CoalesceUndefined<T>(
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>
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>
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>
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>
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>
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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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;
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal;

// ReSharper disable once CheckNamespace
Expand Down Expand Up @@ -883,6 +884,88 @@ public static bool CanSetDefaultTimeToLive(
bool fromDataAnnotation = false)
=> entityTypeBuilder.CanSetAnnotation(CosmosAnnotationNames.DefaultTimeToLive, seconds, fromDataAnnotation);

/// <summary>
/// Configures a default language to use for full-text search at container scope.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
/// </remarks>
/// <param name="entityTypeBuilder">The builder for the entity type being configured.</param>
/// <param name="language">The default language.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static EntityTypeBuilder HasDefaultFullTextLanguage(
this EntityTypeBuilder entityTypeBuilder,
string? language)
{
entityTypeBuilder.Metadata.SetDefaultFullTextSearchLanguage(language);

return entityTypeBuilder;
}

/// <summary>
/// Configures a default language to use for full-text search at container scope.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
/// </remarks>
/// <param name="entityTypeBuilder">The builder for the entity type being configured.</param>
/// <param name="language">The default language.</param>
/// <returns>The same builder instance so that multiple calls can be chained.</returns>
public static EntityTypeBuilder<TEntity> HasDefaultFullTextLanguage<TEntity>(
this EntityTypeBuilder<TEntity> entityTypeBuilder,
string? language)
where TEntity : class
=> (EntityTypeBuilder<TEntity>)HasDefaultFullTextLanguage((EntityTypeBuilder)entityTypeBuilder, language);

/// <summary>
/// Configures a default language to use for full-text search at container scope.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
/// </remarks>
/// <param name="entityTypeBuilder">The builder for the entity type being configured.</param>
/// <param name="language">The default language.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>
/// The same builder instance if the configuration was applied,
/// <see langword="null" /> otherwise.
/// </returns>
public static IConventionEntityTypeBuilder? HasDefaultFullTextLanguage(
this IConventionEntityTypeBuilder entityTypeBuilder,
string? language,
bool fromDataAnnotation = false)
{
if (!entityTypeBuilder.CanSetDefaultFullTextLanguage(language, fromDataAnnotation))
{
return null;
}

entityTypeBuilder.Metadata.SetDefaultFullTextSearchLanguage(language, fromDataAnnotation);

return entityTypeBuilder;
}

/// <summary>
/// Returns a value indicating whether the default full-text language can be set
/// from the current configuration source
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
/// </remarks>
/// <param name="entityTypeBuilder">The builder for the entity type being configured.</param>
/// <param name="language">The default language.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns><see langword="true" /> if the configuration can be applied.</returns>
public static bool CanSetDefaultFullTextLanguage(
this IConventionEntityTypeBuilder entityTypeBuilder,
string? language,
bool fromDataAnnotation = false)
=> entityTypeBuilder.CanSetAnnotation(CosmosAnnotationNames.DefaultFullTextSearchLanguage, language, fromDataAnnotation);

/// <summary>
/// Configures the manual provisioned throughput offering.
/// </summary>
Expand Down
45 changes: 45 additions & 0 deletions src/EFCore.Cosmos/Extensions/CosmosEntityTypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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;
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal;

// ReSharper disable once CheckNamespace
Expand Down Expand Up @@ -586,4 +587,48 @@ public static void SetThroughput(this IMutableEntityType entityType, int? throug
public static ConfigurationSource? GetThroughputConfigurationSource(this IConventionEntityType entityType)
=> entityType.FindAnnotation(CosmosAnnotationNames.Throughput)
?.GetConfigurationSource();

/// <summary>
/// Returns the default language for the full-text search at container scope.
/// </summary>
/// <param name="entityType">The entity type.</param>
/// <returns>The default language for the full-text search.</returns>
public static string? GetDefaultFullTextSearchLanguage(this IReadOnlyEntityType entityType)
=> entityType.BaseType != null
? entityType.GetRootType().GetDefaultFullTextSearchLanguage()
: (string?)entityType[CosmosAnnotationNames.DefaultFullTextSearchLanguage];

/// <summary>
/// Sets the default language for the full-text search at container scope.
/// </summary>
/// <param name="entityType">The entity type.</param>
/// <param name="language">The default language for the full-text search.</param>
public static void SetDefaultFullTextSearchLanguage(this IMutableEntityType entityType, string? language)
=> entityType.SetOrRemoveAnnotation(
CosmosAnnotationNames.DefaultFullTextSearchLanguage,
language);

/// <summary>
/// Sets the default language for the full-text search at container scope.
/// </summary>
/// <param name="entityType">The entity type.</param>
/// <param name="language">The default language for the full-text search.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
public static string? SetDefaultFullTextSearchLanguage(
this IConventionEntityType entityType,
string? language,
bool fromDataAnnotation = false)
=> (string?)entityType.SetOrRemoveAnnotation(
CosmosAnnotationNames.DefaultFullTextSearchLanguage,
language,
fromDataAnnotation)?.Value;

/// <summary>
/// Gets the <see cref="ConfigurationSource" /> for the default full-text search language at container scope.
/// </summary>
/// <param name="entityType">The entity type to find configuration source for.</param>
/// <returns>The <see cref="ConfigurationSource" /> for the default full-text search language.</returns>
public static ConfigurationSource? GetDefaultFullTextSearchLanguageConfigurationSource(this IConventionEntityType entityType)
=> entityType.FindAnnotation(CosmosAnnotationNames.DefaultFullTextSearchLanguage)
?.GetConfigurationSource();
}
85 changes: 84 additions & 1 deletion src/EFCore.Cosmos/Extensions/CosmosIndexBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace Microsoft.EntityFrameworkCore;
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
/// </remarks>
[Experimental(EFDiagnostics.CosmosVectorSearchExperimental)]
public static class CosmosIndexBuilderExtensions
{
/// <summary>
Expand All @@ -28,6 +27,7 @@ public static class CosmosIndexBuilderExtensions
/// <param name="indexBuilder">The builder for the index being configured.</param>
/// <param name="indexType">The type of vector index to create.</param>
/// <returns>A builder to further configure the index.</returns>
[Experimental(EFDiagnostics.CosmosVectorSearchExperimental)]
public static IndexBuilder ForVectors(this IndexBuilder indexBuilder, VectorIndexType? indexType)
{
indexBuilder.Metadata.SetVectorIndexType(indexType);
Expand All @@ -46,6 +46,7 @@ public static IndexBuilder ForVectors(this IndexBuilder indexBuilder, VectorInde
/// <param name="indexBuilder">The builder for the index being configured.</param>
/// <param name="indexType">The type of vector index to create.</param>
/// <returns>A builder to further configure the index.</returns>
[Experimental(EFDiagnostics.CosmosVectorSearchExperimental)]
public static IndexBuilder<TEntity> ForVectors<TEntity>(
this IndexBuilder<TEntity> indexBuilder,
VectorIndexType? indexType)
Expand All @@ -66,6 +67,7 @@ public static IndexBuilder<TEntity> ForVectors<TEntity>(
/// The same builder instance if the configuration was applied,
/// <see langword="null" /> otherwise.
/// </returns>
[Experimental(EFDiagnostics.CosmosVectorSearchExperimental)]
public static IConventionIndexBuilder? ForVectors(
this IConventionIndexBuilder indexBuilder,
VectorIndexType? indexType,
Expand All @@ -91,9 +93,90 @@ public static IndexBuilder<TEntity> ForVectors<TEntity>(
/// <param name="indexType">The index type to use.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns><see langword="true" /> if the index can be configured for vectors.</returns>
[Experimental(EFDiagnostics.CosmosVectorSearchExperimental)]
public static bool CanSetVectorIndexType(
this IConventionIndexBuilder indexBuilder,
VectorIndexType? indexType,
bool fromDataAnnotation = false)
=> indexBuilder.CanSetAnnotation(CosmosAnnotationNames.VectorIndexType, indexType, fromDataAnnotation);

/// <summary>
/// Configures the index as a full-text index.
/// See <see href="https://learn.microsoft.com/azure/cosmos-db/gen-ai/full-text-search">Full-text search in Azure Cosmos DB for NoSQL</see> for more information.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
/// </remarks>
/// <param name="indexBuilder">The builder for the index being configured.</param>
/// <param name="value">The value indicating whether the index is configured for Full-text search.</param>
/// <returns>A builder to further configure the index.</returns>
public static IndexBuilder IsFullTextIndex(this IndexBuilder indexBuilder, bool? value = true)
{
indexBuilder.Metadata.SetIsFullTextIndex(value);

return indexBuilder;
}

/// <summary>
/// Configures the index as a full-text index.
/// See <see href="https://learn.microsoft.com/azure/cosmos-db/gen-ai/full-text-search">Full-text search in Azure Cosmos DB for NoSQL</see> for more information.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
/// </remarks>
/// <param name="indexBuilder">The builder for the index being configured.</param>
/// <param name="value">The value indicating whether the index is configured for Full-text search.</param>
/// <returns>A builder to further configure the index.</returns>
public static IndexBuilder<TEntity> IsFullTextIndex<TEntity>(
this IndexBuilder<TEntity> indexBuilder,
bool? value = true)
=> (IndexBuilder<TEntity>)IsFullTextIndex((IndexBuilder)indexBuilder, value);

/// <summary>
/// Configures the index as a full-text index.
/// See <see href="https://learn.microsoft.com/azure/cosmos-db/gen-ai/full-text-search">Full-text search in Azure Cosmos DB for NoSQL</see> for more information.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
/// </remarks>
/// <param name="indexBuilder">The builder for the index being configured.</param>
/// <param name="value">The value indicating whether the index is configured for Full-text search.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns>
/// The same builder instance if the configuration was applied,
/// <see langword="null" /> otherwise.
/// </returns>
public static IConventionIndexBuilder? IsFullTextIndex(
this IConventionIndexBuilder indexBuilder,
bool? value,
bool fromDataAnnotation = false)
{
if (indexBuilder.CanSetIsFullTextIndex(fromDataAnnotation))
{
indexBuilder.Metadata.SetIsFullTextIndex(value, fromDataAnnotation);
return indexBuilder;
}

return null;
}

/// <summary>
/// Returns a value indicating whether the index can be configured as a full-text index.
/// </summary>
/// <remarks>
/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and
/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.
/// </remarks>
/// <param name="indexBuilder">The builder for the index being configured.</param>
/// <param name="value">The value indicating whether the index is configured for Full-text search.</param>
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param>
/// <returns><see langword="true" /> if the index can be configured as a Full-text index.</returns>
public static bool CanSetIsFullTextIndex(
this IConventionIndexBuilder indexBuilder,
bool? value,
bool fromDataAnnotation = false)
=> indexBuilder.CanSetAnnotation(CosmosAnnotationNames.FullTextIndex, value, fromDataAnnotation);
}
Loading