-
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. Known issues: - Float16 (Half) is not working in Cosmos--needs investigation - Exception on int array case--could be EF or Cosmos--needs investigation - Owned types mess up the materialization--this will be fixed by the ReadItem improvements I am working on
- Loading branch information
1 parent
d6fd3d2
commit ede4d33
Showing
45 changed files
with
1,694 additions
and
239 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
94 changes: 94 additions & 0 deletions
94
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,94 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
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> | ||
public static class CosmosIndexBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Configures whether the index as a vector index with the given <see cref="VectorIndexType"/>. | ||
/// </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 <see cref="VectorIndexType"/>. | ||
/// </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 <see cref="VectorIndexType"/>. | ||
/// </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 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,59 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
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> | ||
public static class CosmosIndexExtensions | ||
{ | ||
/// <summary> | ||
/// Returns the <see cref="VectorIndexType"/> to use for this index. | ||
/// </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 <see cref="VectorIndexType"/> to use for this index. | ||
/// </summary> | ||
/// <param name="indexType">The index type to use.</param> | ||
/// <param name="index">The index.</param> | ||
public static void SetVectorIndexType(this IMutableIndex index, VectorIndexType? indexType) | ||
=> index.SetAnnotation(CosmosAnnotationNames.VectorIndexType, indexType); | ||
|
||
/// <summary> | ||
/// Sets the <see cref="VectorIndexType"/> to use for this index. | ||
/// </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 VectorIndexType? SetVectorIndexType( | ||
this IConventionIndex index, | ||
VectorIndexType? indexType, | ||
bool fromDataAnnotation = false) | ||
=> (VectorIndexType?)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(); | ||
} |
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
Oops, something went wrong.