-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #33783 This PR introduces: - `IsVector()` to configure a property to be configured as a vector (embedding) in the document. - The distance function and dimensions are specified. - The data type can be specified, or otherwise is inferred. - `HasIndex().ForVectors()` to configure a vector index over a vector property. - `VectorDistance()` which translates to the Cosmos `VectorDistance` function - The distance function and data type are taken from the property mapping, or can be overridden.
- Loading branch information
1 parent
757c823
commit c43d3e8
Showing
35 changed files
with
1,787 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/EFCore.Cosmos/Extensions/CosmosIndexBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// 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 | ||
namespace Microsoft.EntityFrameworkCore; | ||
|
||
/// <summary> | ||
/// Azure Cosmos DB-specific extension methods for <see cref="IndexBuilder"/>. | ||
/// </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> | ||
[Experimental(EFDiagnostics.CosmosVectorSearchExperimental)] | ||
public static class CosmosIndexBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Configures the index as a vector index with the given vector index type, such as "flat", "diskANN", or "quantizedFlat". | ||
/// See <see href="https://aka.ms/ef-cosmos-vectors">Vector 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="indexType">The type of vector index to create.</param> | ||
/// <returns>A builder to further configure the index.</returns> | ||
public static IndexBuilder ForVectors(this IndexBuilder indexBuilder, VectorIndexType? indexType) | ||
{ | ||
indexBuilder.Metadata.SetVectorIndexType(indexType); | ||
|
||
return indexBuilder; | ||
} | ||
|
||
/// <summary> | ||
/// Configures whether the index as a vector index with the given vector index type, such as "flat", "diskANN", or "quantizedFlat". | ||
/// See <see href="https://aka.ms/ef-cosmos-vectors">Vector 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="indexType">The type of vector index to create.</param> | ||
/// <returns>A builder to further configure the index.</returns> | ||
public static IndexBuilder<TEntity> ForVectors<TEntity>( | ||
this IndexBuilder<TEntity> indexBuilder, | ||
VectorIndexType? indexType) | ||
=> (IndexBuilder<TEntity>)ForVectors((IndexBuilder)indexBuilder, indexType); | ||
|
||
/// <summary> | ||
/// Configures whether the index as a vector index with the given vector index type, such as "flat", "diskANN", or "quantizedFlat". | ||
/// See <see href="https://aka.ms/ef-cosmos-vectors">Vector 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="indexType">The type of vector index to create.</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? ForVectors( | ||
this IConventionIndexBuilder indexBuilder, | ||
VectorIndexType? indexType, | ||
bool fromDataAnnotation = false) | ||
{ | ||
if (indexBuilder.CanSetVectorIndexType(indexType, fromDataAnnotation)) | ||
{ | ||
indexBuilder.Metadata.SetVectorIndexType(indexType, fromDataAnnotation); | ||
return indexBuilder; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/// <summary> | ||
/// Returns a value indicating whether the vector index can be configured for vectors. | ||
/// </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="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> | ||
public static bool CanSetVectorIndexType( | ||
this IConventionIndexBuilder indexBuilder, | ||
VectorIndexType? indexType, | ||
bool fromDataAnnotation = false) | ||
=> indexBuilder.CanSetAnnotation(CosmosAnnotationNames.VectorIndexType, indexType, fromDataAnnotation); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// 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 | ||
namespace Microsoft.EntityFrameworkCore; | ||
|
||
/// <summary> | ||
/// Index extension methods for Azure Cosmos DB-specific metadata. | ||
/// </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> | ||
[Experimental(EFDiagnostics.CosmosVectorSearchExperimental)] | ||
public static class CosmosIndexExtensions | ||
{ | ||
/// <summary> | ||
/// Returns the vector index type to use, such as "flat", "diskANN", or "quantizedFlat". | ||
/// See <see href="https://aka.ms/ef-cosmos-vectors">Vector Search in Azure Cosmos DB for NoSQL</see> for more information. | ||
/// </summary> | ||
/// <param name="index">The index.</param> | ||
/// <returns>The index type to use, or <see langword="null" /> if none is set.</returns> | ||
public static VectorIndexType? GetVectorIndexType(this IReadOnlyIndex index) | ||
=> (index is RuntimeIndex) | ||
? throw new InvalidOperationException(CoreStrings.RuntimeModelMissingData) | ||
: (VectorIndexType?)index[CosmosAnnotationNames.VectorIndexType]; | ||
|
||
/// <summary> | ||
/// Sets the vector index type to use, such as "flat", "diskANN", or "quantizedFlat". | ||
/// See <see href="https://aka.ms/ef-cosmos-vectors">Vector Search in Azure Cosmos DB for NoSQL</see> for more information. | ||
/// </summary> | ||
/// <param name="index">The index.</param> | ||
/// <param name="indexType">The index type to use.</param> | ||
public static void SetVectorIndexType(this IMutableIndex index, VectorIndexType? indexType) | ||
=> index.SetAnnotation(CosmosAnnotationNames.VectorIndexType, indexType); | ||
|
||
/// <summary> | ||
/// Sets the vector index type to use, such as "flat", "diskANN", or "quantizedFlat". | ||
/// See <see href="https://aka.ms/ef-cosmos-vectors">Vector Search in Azure Cosmos DB for NoSQL</see> for more information. | ||
/// </summary> | ||
/// <param name="indexType">The index type to use.</param> | ||
/// <param name="index">The index.</param> | ||
/// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param> | ||
/// <returns>The configured value.</returns> | ||
public static string? SetVectorIndexType( | ||
this IConventionIndex index, | ||
VectorIndexType? indexType, | ||
bool fromDataAnnotation = false) | ||
=> (string?)index.SetAnnotation( | ||
CosmosAnnotationNames.VectorIndexType, | ||
indexType, | ||
fromDataAnnotation)?.Value; | ||
|
||
/// <summary> | ||
/// Returns the <see cref="ConfigurationSource" /> for whether the <see cref="GetVectorIndexType"/>. | ||
/// </summary> | ||
/// <param name="property">The property.</param> | ||
/// <returns>The <see cref="ConfigurationSource" /> for whether the index is clustered.</returns> | ||
public static ConfigurationSource? GetVectorIndexTypeConfigurationSource(this IConventionIndex property) | ||
=> property.FindAnnotation(CosmosAnnotationNames.VectorIndexType)?.GetConfigurationSource(); | ||
} |
Oops, something went wrong.