|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Diagnostics.CodeAnalysis; |
| 5 | +using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal; |
| 6 | + |
| 7 | +// ReSharper disable once CheckNamespace |
| 8 | +namespace Microsoft.EntityFrameworkCore; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Azure Cosmos DB-specific extension methods for <see cref="IndexBuilder"/>. |
| 12 | +/// </summary> |
| 13 | +/// <remarks> |
| 14 | +/// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and |
| 15 | +/// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples. |
| 16 | +/// </remarks> |
| 17 | +[Experimental(EFDiagnostics.CosmosVectorSearchExperimental)] |
| 18 | +public static class CosmosIndexBuilderExtensions |
| 19 | +{ |
| 20 | + /// <summary> |
| 21 | + /// Configures the index as a vector index with the given vector index type, such as "flat", "diskANN", or "quantizedFlat". |
| 22 | + /// See <see href="https://aka.ms/ef-cosmos-vectors">Vector Search in Azure Cosmos DB for NoSQL</see> for more information. |
| 23 | + /// </summary> |
| 24 | + /// <remarks> |
| 25 | + /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and |
| 26 | + /// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples. |
| 27 | + /// </remarks> |
| 28 | + /// <param name="indexBuilder">The builder for the index being configured.</param> |
| 29 | + /// <param name="indexType">The type of vector index to create.</param> |
| 30 | + /// <returns>A builder to further configure the index.</returns> |
| 31 | + public static IndexBuilder ForVectors(this IndexBuilder indexBuilder, VectorIndexType? indexType) |
| 32 | + { |
| 33 | + indexBuilder.Metadata.SetVectorIndexType(indexType); |
| 34 | + |
| 35 | + return indexBuilder; |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Configures whether the index as a vector index with the given vector index type, such as "flat", "diskANN", or "quantizedFlat". |
| 40 | + /// See <see href="https://aka.ms/ef-cosmos-vectors">Vector Search in Azure Cosmos DB for NoSQL</see> for more information. |
| 41 | + /// </summary> |
| 42 | + /// <remarks> |
| 43 | + /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and |
| 44 | + /// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples. |
| 45 | + /// </remarks> |
| 46 | + /// <param name="indexBuilder">The builder for the index being configured.</param> |
| 47 | + /// <param name="indexType">The type of vector index to create.</param> |
| 48 | + /// <returns>A builder to further configure the index.</returns> |
| 49 | + public static IndexBuilder<TEntity> ForVectors<TEntity>( |
| 50 | + this IndexBuilder<TEntity> indexBuilder, |
| 51 | + VectorIndexType? indexType) |
| 52 | + => (IndexBuilder<TEntity>)ForVectors((IndexBuilder)indexBuilder, indexType); |
| 53 | + |
| 54 | + /// <summary> |
| 55 | + /// Configures whether the index as a vector index with the given vector index type, such as "flat", "diskANN", or "quantizedFlat". |
| 56 | + /// See <see href="https://aka.ms/ef-cosmos-vectors">Vector Search in Azure Cosmos DB for NoSQL</see> for more information. |
| 57 | + /// </summary> |
| 58 | + /// <remarks> |
| 59 | + /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and |
| 60 | + /// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples. |
| 61 | + /// </remarks> |
| 62 | + /// <param name="indexBuilder">The builder for the index being configured.</param> |
| 63 | + /// <param name="indexType">The type of vector index to create.</param> |
| 64 | + /// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param> |
| 65 | + /// <returns> |
| 66 | + /// The same builder instance if the configuration was applied, |
| 67 | + /// <see langword="null" /> otherwise. |
| 68 | + /// </returns> |
| 69 | + public static IConventionIndexBuilder? ForVectors( |
| 70 | + this IConventionIndexBuilder indexBuilder, |
| 71 | + VectorIndexType? indexType, |
| 72 | + bool fromDataAnnotation = false) |
| 73 | + { |
| 74 | + if (indexBuilder.CanSetVectorIndexType(indexType, fromDataAnnotation)) |
| 75 | + { |
| 76 | + indexBuilder.Metadata.SetVectorIndexType(indexType, fromDataAnnotation); |
| 77 | + return indexBuilder; |
| 78 | + } |
| 79 | + |
| 80 | + return null; |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Returns a value indicating whether the vector index can be configured for vectors. |
| 85 | + /// </summary> |
| 86 | + /// <remarks> |
| 87 | + /// See <see href="https://aka.ms/efcore-docs-modeling">Modeling entity types and relationships</see>, and |
| 88 | + /// <see href="https://aka.ms/efcore-docs-cosmos">Accessing Azure Cosmos DB with EF Core</see> for more information and examples. |
| 89 | + /// </remarks> |
| 90 | + /// <param name="indexBuilder">The builder for the index being configured.</param> |
| 91 | + /// <param name="indexType">The index type to use.</param> |
| 92 | + /// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param> |
| 93 | + /// <returns><see langword="true" /> if the index can be configured for vectors.</returns> |
| 94 | + public static bool CanSetVectorIndexType( |
| 95 | + this IConventionIndexBuilder indexBuilder, |
| 96 | + VectorIndexType? indexType, |
| 97 | + bool fromDataAnnotation = false) |
| 98 | + => indexBuilder.CanSetAnnotation(CosmosAnnotationNames.VectorIndexType, indexType, fromDataAnnotation); |
| 99 | +} |
0 commit comments