-
Notifications
You must be signed in to change notification settings - Fork 1.3k
CSHARP-5717: Typed builders for vector indexes #1795
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
Open
ajcvickers
wants to merge
3
commits into
mongodb:main
Choose a base branch
from
ajcvickers:csharp5717
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using MongoDB.Bson; | ||
|
||
namespace MongoDB.Driver; | ||
|
||
/// <summary> | ||
/// Defines a vector index model using strongly-typed C# APIs. | ||
/// </summary> | ||
public sealed class CreateVectorIndexModel<TDocument> : CreateSearchIndexModel | ||
{ | ||
/// <summary> | ||
/// The field containing the vectors to index. | ||
/// </summary> | ||
public FieldDefinition<TDocument> Field { get; } | ||
|
||
/// <summary> | ||
/// The <see cref="VectorSimilarity"/> to use to search for top K-nearest neighbors. | ||
/// </summary> | ||
public VectorSimilarity Similarity { get; } | ||
|
||
/// <summary> | ||
/// Number of vector dimensions that vector search enforces at index-time and query-time. | ||
/// </summary> | ||
public int Dimensions { get; } | ||
|
||
/// <summary> | ||
/// Fields that may be used as filters in the vector query. | ||
/// </summary> | ||
public IReadOnlyList<FieldDefinition<TDocument>> FilterFields { get; } | ||
|
||
/// <summary> | ||
/// Type of automatic vector quantization for your vectors. | ||
/// </summary> | ||
public VectorQuantization? Quantization { get; init; } | ||
|
||
/// <summary> | ||
/// Maximum number of edges (or connections) that a node can have in the Hierarchical Navigable Small Worlds graph. | ||
/// </summary> | ||
public int? HnswMaxEdges { get; init; } | ||
|
||
/// <summary> | ||
/// Analogous to numCandidates at query-time, this parameter controls the maximum number of nodes to evaluate to find the closest neighbors to connect to a new node. | ||
/// </summary> | ||
public int? HnswNumEdgeCandidates { get; init; } | ||
|
||
/// <summary> | ||
/// This method should not be called on this subtype. Instead, call <see cref="Render"/> to create a BSON | ||
/// document for the index model. | ||
/// </summary> | ||
public override BsonDocument Definition | ||
=> throw new NotSupportedException( | ||
"This method should not be called on this subtype. Instead, call 'Render' to create a BSON document for the index model."); | ||
ajcvickers marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="CreateVectorIndexModel{TDocument}"/> class, passing the | ||
/// required options for <see cref="VectorSimilarity"/> and the number of vector dimensions to the constructor. | ||
/// </summary> | ||
/// <param name="name">The index name.</param> | ||
/// <param name="field">The field containing the vectors to index.</param> | ||
/// <param name="similarity">The <see cref="VectorSimilarity"/> to use to search for top K-nearest neighbors.</param> | ||
/// <param name="dimensions">Number of vector dimensions that vector search enforces at index-time and query-time.</param> | ||
/// <param name="filterFields">Fields that may be used as filters in the vector query.</param> | ||
public CreateVectorIndexModel( | ||
FieldDefinition<TDocument> field, | ||
string name, | ||
VectorSimilarity similarity, | ||
int dimensions, | ||
params FieldDefinition<TDocument>[] filterFields) | ||
: base(name, SearchIndexType.VectorSearch) | ||
{ | ||
Field = field; | ||
Similarity = similarity; | ||
Dimensions = dimensions; | ||
FilterFields = filterFields?.ToList() ?? []; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="CreateVectorIndexModel{TDocument}"/> class, passing the | ||
/// required options for <see cref="VectorSimilarity"/> and the number of vector dimensions to the constructor. | ||
/// </summary> | ||
/// <param name="name">The index name.</param> | ||
/// <param name="field">An expression pointing to the field containing the vectors to index.</param> | ||
/// <param name="similarity">The <see cref="VectorSimilarity"/> to use to search for top K-nearest neighbors.</param> | ||
/// <param name="dimensions">Number of vector dimensions that vector search enforces at index-time and query-time.</param> | ||
/// <param name="filterFields">Expressions pointing to fields that may be used as filters in the vector query.</param> | ||
public CreateVectorIndexModel( | ||
Expression<Func<TDocument, object>> field, | ||
string name, | ||
VectorSimilarity similarity, | ||
int dimensions, | ||
params Expression<Func<TDocument, object>>[] filterFields) | ||
: this( | ||
new ExpressionFieldDefinition<TDocument>(field), | ||
name, | ||
similarity, | ||
dimensions, | ||
filterFields? | ||
.Select(f => (FieldDefinition<TDocument>)new ExpressionFieldDefinition<TDocument>(f)) | ||
.ToArray()) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Renders the index model to a <see cref="BsonDocument"/>. | ||
/// </summary> | ||
/// <param name="renderArgs">The render arguments.</param> | ||
/// <returns>A <see cref="BsonDocument" />.</returns> | ||
public BsonDocument Render(RenderArgs<TDocument> renderArgs) | ||
{ | ||
var similarityValue = Similarity == VectorSimilarity.DotProduct | ||
? "dotProduct" // Because neither "DotProduct" or "dotproduct" are allowed. | ||
: Similarity.ToString().ToLowerInvariant(); | ||
|
||
var vectorField = new BsonDocument | ||
{ | ||
{ "type", BsonString.Create("vector") }, | ||
{ "path", Field.Render(renderArgs).FieldName }, | ||
{ "numDimensions", BsonInt32.Create(Dimensions) }, | ||
{ "similarity", BsonString.Create(similarityValue) }, | ||
}; | ||
|
||
if (Quantization.HasValue) | ||
{ | ||
vectorField.Add("quantization", BsonString.Create(Quantization.ToString()?.ToLower())); | ||
} | ||
|
||
if (HnswMaxEdges != null || HnswNumEdgeCandidates != null) | ||
{ | ||
var hnswDocument = new BsonDocument | ||
{ | ||
{ "maxEdges", BsonInt32.Create(HnswMaxEdges ?? 16) }, | ||
{ "numEdgeCandidates", BsonInt32.Create(HnswNumEdgeCandidates ?? 100) } | ||
}; | ||
vectorField.Add("hnswOptions", hnswDocument); | ||
} | ||
|
||
var fieldDocuments = new List<BsonDocument> { vectorField }; | ||
|
||
if (FilterFields != null) | ||
{ | ||
foreach (var filterPath in FilterFields) | ||
{ | ||
var fieldDocument = new BsonDocument | ||
{ | ||
{ "type", BsonString.Create("filter") }, | ||
{ "path", BsonString.Create(filterPath.Render(renderArgs).FieldName) } | ||
}; | ||
|
||
fieldDocuments.Add(fieldDocument); | ||
} | ||
} | ||
|
||
return new BsonDocument { { "fields", BsonArray.Create(fieldDocuments) } }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would use
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace MongoDB.Driver; | ||
|
||
/// <summary> | ||
/// Type of automatic vector quantization for your vectors. Use this setting only if your embeddings are float | ||
/// or double vectors. See <see href="https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-quantization/"> | ||
/// Vector Quantization</see> for more information. | ||
/// </summary> | ||
public enum VectorQuantization | ||
{ | ||
/// <summary> | ||
/// Indicates no automatic quantization for the vector embeddings. Use this setting if you have pre-quantized | ||
/// vectors for ingestion. If omitted, this is the default value. | ||
/// </summary> | ||
None, | ||
|
||
/// <summary> | ||
/// Indicates scalar quantization, which transforms values to 1 byte integers. | ||
/// </summary> | ||
Scalar, | ||
|
||
/// <summary> | ||
/// Indicates binary quantization, which transforms values to a single bit. | ||
/// To use this value, numDimensions must be a multiple of 8. | ||
/// If precision is critical, select <see cref="None"/> or <see cref="Scalar"/> instead of <see cref="Binary"/>. | ||
/// </summary> | ||
Binary, | ||
} |
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,39 @@ | ||
/* Copyright 2010-present MongoDB Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
namespace MongoDB.Driver; | ||
|
||
/// <summary> | ||
/// Vector similarity function to use to search for top K-nearest neighbors. | ||
/// See <see href="https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-type/">How to Index Fields for | ||
/// Vector Search</see> for more information. | ||
/// </summary> | ||
public enum VectorSimilarity | ||
{ | ||
/// <summary> | ||
/// Measures the distance between ends of vectors. | ||
/// </summary> | ||
Euclidean, | ||
|
||
/// <summary> | ||
/// Measures similarity based on the angle between vectors. | ||
/// </summary> | ||
Cosine, | ||
|
||
/// <summary> | ||
/// Measures similarity like cosine, but takes into account the magnitude of the vector. | ||
/// </summary> | ||
DotProduct, | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, but should this class be named
CreateVectorSearchIndexModel
?The server calls this type of index a "vector search index"?