-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathMongoDBVectorStoreCollectionCreateMapping.cs
121 lines (103 loc) · 4.77 KB
/
MongoDBVectorStoreCollectionCreateMapping.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright (c) Microsoft. All rights reserved.
using System;
using System.Collections.Generic;
using Microsoft.Extensions.VectorData;
using MongoDB.Bson;
namespace Microsoft.SemanticKernel.Connectors.MongoDB;
/// <summary>
/// Contains mapping helpers to use when creating a collection in MongoDB.
/// </summary>
internal static class MongoDBVectorStoreCollectionCreateMapping
{
/// <summary>
/// Returns an array of indexes to create for vector properties.
/// </summary>
/// <param name="vectorProperties">Collection of vector properties for index creation.</param>
/// <param name="storagePropertyNames">A dictionary that maps from a property name to the storage name.</param>
public static BsonArray GetVectorIndexFields(
IReadOnlyList<VectorStoreRecordVectorProperty> vectorProperties,
Dictionary<string, string> storagePropertyNames)
{
var indexArray = new BsonArray();
// Create separate index for each vector property
foreach (var property in vectorProperties)
{
// Use index name same as vector property name with underscore
var vectorPropertyName = storagePropertyNames[property.DataModelPropertyName];
var indexDocument = new BsonDocument
{
{ "type", "vector" },
{ "numDimensions", property.Dimensions },
{ "path", vectorPropertyName },
{ "similarity", GetDistanceFunction(property.DistanceFunction, vectorPropertyName) },
};
indexArray.Add(indexDocument);
}
return indexArray;
}
/// <summary>
/// Returns an array of indexes to create for filterable data properties.
/// </summary>
/// <param name="dataProperties">Collection of data properties for index creation.</param>
/// <param name="storagePropertyNames">A dictionary that maps from a property name to the storage name.</param>
public static BsonArray GetFilterableDataIndexFields(
IReadOnlyList<VectorStoreRecordDataProperty> dataProperties,
Dictionary<string, string> storagePropertyNames)
{
var indexArray = new BsonArray();
// Create separate index for each data property
foreach (var property in dataProperties)
{
if (property.IsIndexed)
{
// Use index name same as data property name with underscore
var dataPropertyName = storagePropertyNames[property.DataModelPropertyName];
var indexDocument = new BsonDocument
{
{ "type", "filter" },
{ "path", dataPropertyName },
};
indexArray.Add(indexDocument);
}
}
return indexArray;
}
/// <summary>
/// Returns a list of of fields to index for full text search data properties.
/// </summary>
/// <param name="dataProperties">Collection of data properties for index creation.</param>
/// <param name="storagePropertyNames">A dictionary that maps from a property name to the storage name.</param>
public static List<BsonElement> GetFullTextSearchableDataIndexFields(
IReadOnlyList<VectorStoreRecordDataProperty> dataProperties,
Dictionary<string, string> storagePropertyNames)
{
var fieldElements = new List<BsonElement>();
// Create separate index for each data property
foreach (var property in dataProperties)
{
if (property.IsFullTextSearchable)
{
var dataPropertyName = storagePropertyNames[property.DataModelPropertyName];
fieldElements.Add(new BsonElement(dataPropertyName, new BsonArray()
{
new BsonDocument() { { "type", "string" }, }
}));
}
}
return fieldElements;
}
/// <summary>
/// More information about MongoDB distance functions here: <see href="https://www.mongodb.com/docs/atlas/atlas-vector-search/vector-search-type/#atlas-vector-search-index-fields" />.
/// </summary>
private static string GetDistanceFunction(string? distanceFunction, string vectorPropertyName)
{
var vectorPropertyDistanceFunction = MongoDBVectorStoreCollectionSearchMapping.GetVectorPropertyDistanceFunction(distanceFunction);
return vectorPropertyDistanceFunction switch
{
DistanceFunction.CosineSimilarity => "cosine",
DistanceFunction.DotProductSimilarity => "dotProduct",
DistanceFunction.EuclideanDistance => "euclidean",
_ => throw new InvalidOperationException($"Distance function '{distanceFunction}' for {nameof(VectorStoreRecordVectorProperty)} '{vectorPropertyName}' is not supported by the MongoDB VectorStore.")
};
}
}