-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Support vector search on Cosmos DB #33991
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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) | ||
|
ajcvickers marked this conversation as resolved.
|
||
| { | ||
| 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.