|  | 
|  | 1 | +/* Copyright 2010-present MongoDB Inc. | 
|  | 2 | + * | 
|  | 3 | + * Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 4 | + * you may not use this file except in compliance with the License. | 
|  | 5 | + * You may obtain a copy of the License at | 
|  | 6 | + * | 
|  | 7 | + * http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 | + * | 
|  | 9 | + * Unless required by applicable law or agreed to in writing, software | 
|  | 10 | + * distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 12 | + * See the License for the specific language governing permissions and | 
|  | 13 | + * limitations under the License. | 
|  | 14 | + */ | 
|  | 15 | + | 
|  | 16 | +using System; | 
|  | 17 | +using System.Collections.Generic; | 
|  | 18 | +using System.Linq; | 
|  | 19 | +using System.Linq.Expressions; | 
|  | 20 | +using MongoDB.Bson; | 
|  | 21 | + | 
|  | 22 | +namespace MongoDB.Driver; | 
|  | 23 | + | 
|  | 24 | +/// <summary> | 
|  | 25 | +/// Defines a vector index model using strongly-typed C# APIs. | 
|  | 26 | +/// </summary> | 
|  | 27 | +public sealed class CreateVectorSearchIndexModel<TDocument> : CreateSearchIndexModel | 
|  | 28 | +{ | 
|  | 29 | +    /// <summary> | 
|  | 30 | +    /// The field containing the vectors to index. | 
|  | 31 | +    /// </summary> | 
|  | 32 | +    public FieldDefinition<TDocument> Field { get; } | 
|  | 33 | + | 
|  | 34 | +    /// <summary> | 
|  | 35 | +    /// The <see cref="VectorSimilarity"/> to use to search for top K-nearest neighbors. | 
|  | 36 | +    /// </summary> | 
|  | 37 | +    public VectorSimilarity Similarity { get; } | 
|  | 38 | + | 
|  | 39 | +    /// <summary> | 
|  | 40 | +    /// Number of vector dimensions that vector search enforces at index-time and query-time. | 
|  | 41 | +    /// </summary> | 
|  | 42 | +    public int Dimensions { get; } | 
|  | 43 | + | 
|  | 44 | +    /// <summary> | 
|  | 45 | +    /// Fields that may be used as filters in the vector query. | 
|  | 46 | +    /// </summary> | 
|  | 47 | +    public IReadOnlyList<FieldDefinition<TDocument>> FilterFields { get; } | 
|  | 48 | + | 
|  | 49 | +    /// <summary> | 
|  | 50 | +    /// Type of automatic vector quantization for your vectors. | 
|  | 51 | +    /// </summary> | 
|  | 52 | +    public VectorQuantization? Quantization { get; init; } | 
|  | 53 | + | 
|  | 54 | +    /// <summary> | 
|  | 55 | +    /// Maximum number of edges (or connections) that a node can have in the Hierarchical Navigable Small Worlds graph. | 
|  | 56 | +    /// </summary> | 
|  | 57 | +    public int? HnswMaxEdges { get; init; } | 
|  | 58 | + | 
|  | 59 | +    /// <summary> | 
|  | 60 | +    /// 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. | 
|  | 61 | +    /// </summary> | 
|  | 62 | +    public int? HnswNumEdgeCandidates { get; init; } | 
|  | 63 | + | 
|  | 64 | +    /// <summary> | 
|  | 65 | +    /// Initializes a new instance of the <see cref="CreateVectorSearchIndexModel{TDocument}"/> class, passing the | 
|  | 66 | +    /// required options for <see cref="VectorSimilarity"/> and the number of vector dimensions to the constructor. | 
|  | 67 | +    /// </summary> | 
|  | 68 | +    /// <param name="name">The index name.</param> | 
|  | 69 | +    /// <param name="field">The field containing the vectors to index.</param> | 
|  | 70 | +    /// <param name="similarity">The <see cref="VectorSimilarity"/> to use to search for top K-nearest neighbors.</param> | 
|  | 71 | +    /// <param name="dimensions">Number of vector dimensions that vector search enforces at index-time and query-time.</param> | 
|  | 72 | +    /// <param name="filterFields">Fields that may be used as filters in the vector query.</param> | 
|  | 73 | +    public CreateVectorSearchIndexModel( | 
|  | 74 | +        FieldDefinition<TDocument> field, | 
|  | 75 | +        string name, | 
|  | 76 | +        VectorSimilarity similarity, | 
|  | 77 | +        int dimensions, | 
|  | 78 | +        params FieldDefinition<TDocument>[] filterFields) | 
|  | 79 | +        : base(name, SearchIndexType.VectorSearch) | 
|  | 80 | +    { | 
|  | 81 | +        Field = field; | 
|  | 82 | +        Similarity = similarity; | 
|  | 83 | +        Dimensions = dimensions; | 
|  | 84 | +        FilterFields = filterFields?.ToList() ?? []; | 
|  | 85 | +    } | 
|  | 86 | + | 
|  | 87 | +    /// <summary> | 
|  | 88 | +    /// Initializes a new instance of the <see cref="CreateVectorSearchIndexModel{TDocument}"/> class, passing the | 
|  | 89 | +    /// required options for <see cref="VectorSimilarity"/> and the number of vector dimensions to the constructor. | 
|  | 90 | +    /// </summary> | 
|  | 91 | +    /// <param name="name">The index name.</param> | 
|  | 92 | +    /// <param name="field">An expression pointing to the field containing the vectors to index.</param> | 
|  | 93 | +    /// <param name="similarity">The <see cref="VectorSimilarity"/> to use to search for top K-nearest neighbors.</param> | 
|  | 94 | +    /// <param name="dimensions">Number of vector dimensions that vector search enforces at index-time and query-time.</param> | 
|  | 95 | +    /// <param name="filterFields">Expressions pointing to fields that may be used as filters in the vector query.</param> | 
|  | 96 | +    public CreateVectorSearchIndexModel( | 
|  | 97 | +        Expression<Func<TDocument, object>> field, | 
|  | 98 | +        string name, | 
|  | 99 | +        VectorSimilarity similarity, | 
|  | 100 | +        int dimensions, | 
|  | 101 | +        params Expression<Func<TDocument, object>>[] filterFields) | 
|  | 102 | +        : this( | 
|  | 103 | +            new ExpressionFieldDefinition<TDocument>(field), | 
|  | 104 | +            name, | 
|  | 105 | +            similarity, | 
|  | 106 | +            dimensions, | 
|  | 107 | +            filterFields? | 
|  | 108 | +                .Select(f => (FieldDefinition<TDocument>)new ExpressionFieldDefinition<TDocument>(f)) | 
|  | 109 | +                .ToArray()) | 
|  | 110 | +    { | 
|  | 111 | +    } | 
|  | 112 | + | 
|  | 113 | +    /// <summary> | 
|  | 114 | +    /// Renders the index model to a <see cref="BsonDocument"/>. | 
|  | 115 | +    /// </summary> | 
|  | 116 | +    /// <param name="renderArgs">The render arguments.</param> | 
|  | 117 | +    /// <returns>A <see cref="BsonDocument" />.</returns> | 
|  | 118 | +    public BsonDocument Render(RenderArgs<TDocument> renderArgs) | 
|  | 119 | +    { | 
|  | 120 | +        var similarityValue = Similarity == VectorSimilarity.DotProduct | 
|  | 121 | +            ? "dotProduct" // Because neither "DotProduct" or "dotproduct" are allowed. | 
|  | 122 | +            : Similarity.ToString().ToLowerInvariant(); | 
|  | 123 | + | 
|  | 124 | +        var vectorField = new BsonDocument | 
|  | 125 | +        { | 
|  | 126 | +            { "type", BsonString.Create("vector") }, | 
|  | 127 | +            { "path", Field.Render(renderArgs).FieldName }, | 
|  | 128 | +            { "numDimensions", BsonInt32.Create(Dimensions) }, | 
|  | 129 | +            { "similarity", BsonString.Create(similarityValue) }, | 
|  | 130 | +        }; | 
|  | 131 | + | 
|  | 132 | +        if (Quantization.HasValue) | 
|  | 133 | +        { | 
|  | 134 | +            vectorField.Add("quantization", BsonString.Create(Quantization.ToString()?.ToLower())); | 
|  | 135 | +        } | 
|  | 136 | + | 
|  | 137 | +        if (HnswMaxEdges != null || HnswNumEdgeCandidates != null) | 
|  | 138 | +        { | 
|  | 139 | +            var hnswDocument = new BsonDocument | 
|  | 140 | +            { | 
|  | 141 | +                { "maxEdges", BsonInt32.Create(HnswMaxEdges ?? 16) }, | 
|  | 142 | +                { "numEdgeCandidates", BsonInt32.Create(HnswNumEdgeCandidates ?? 100) } | 
|  | 143 | +            }; | 
|  | 144 | +            vectorField.Add("hnswOptions", hnswDocument); | 
|  | 145 | +        } | 
|  | 146 | + | 
|  | 147 | +        var fieldDocuments = new List<BsonDocument> { vectorField }; | 
|  | 148 | + | 
|  | 149 | +        if (FilterFields != null) | 
|  | 150 | +        { | 
|  | 151 | +            foreach (var filterPath in FilterFields) | 
|  | 152 | +            { | 
|  | 153 | +                var fieldDocument = new BsonDocument | 
|  | 154 | +                { | 
|  | 155 | +                    { "type", BsonString.Create("filter") }, | 
|  | 156 | +                    { "path", BsonString.Create(filterPath.Render(renderArgs).FieldName) } | 
|  | 157 | +                }; | 
|  | 158 | + | 
|  | 159 | +                fieldDocuments.Add(fieldDocument); | 
|  | 160 | +            } | 
|  | 161 | +        } | 
|  | 162 | + | 
|  | 163 | +        return new BsonDocument { { "fields", new BsonArray(fieldDocuments) } }; | 
|  | 164 | +    } | 
|  | 165 | +} | 
0 commit comments