Skip to content
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

Cosmos Full Text Search support #35868

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 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 All @@ -12,6 +12,7 @@
<NoWarn>$(NoWarn);EF9101</NoWarn> <!-- Metrics is experimental -->
<NoWarn>$(NoWarn);EF9102</NoWarn> <!-- Paging is experimental -->
<NoWarn>$(NoWarn);EF9103</NoWarn> <!-- Vector search is experimental -->
<NoWarn>$(NoWarn);EF9104</NoWarn> <!-- Full-text search is experimental -->
</PropertyGroup>

<ItemGroup>
Expand Down
54 changes: 54 additions & 0 deletions src/EFCore.Cosmos/Extensions/CosmosDbFunctionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,60 @@ 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>
[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 search.</param>
/// <param name="keywords">The keywords to search for.</param>
/// <returns>The full-text search score.</returns>
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
public static double FullTextScore(this DbFunctions _, string property, 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
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,92 @@ 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>
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
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>
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
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>
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
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>
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
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
49 changes: 49 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,52 @@ 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>
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
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>
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
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>
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
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>
[Experimental(EFDiagnostics.CosmosFullTextSearchExperimental)]
public static ConfigurationSource? GetDefaultFullTextSearchLanguageConfigurationSource(this IConventionEntityType entityType)
=> entityType.FindAnnotation(CosmosAnnotationNames.DefaultFullTextSearchLanguage)
?.GetConfigurationSource();
}
Loading