forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCosmosMongoCollectionSearchMapping.cs
More file actions
113 lines (101 loc) · 3.85 KB
/
Copy pathCosmosMongoCollectionSearchMapping.cs
File metadata and controls
113 lines (101 loc) · 3.85 KB
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
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.Extensions.VectorData;
using Microsoft.SemanticKernel.Connectors.MongoDB;
using MongoDB.Bson;
namespace Microsoft.SemanticKernel.Connectors.CosmosMongoDB;
/// <summary>
/// Contains mapping helpers to use when searching for documents using Azure CosmosDB MongoDB.
/// </summary>
internal static class CosmosMongoCollectionSearchMapping
{
/// <summary>Returns index kind specified on vector property or default <see cref="MongoConstants.DefaultIndexKind"/>.</summary>
public static string GetVectorPropertyIndexKind(string? indexKind) => !string.IsNullOrWhiteSpace(indexKind) ? indexKind! : MongoConstants.DefaultIndexKind;
/// <summary>Returns distance function specified on vector property or default <see cref="MongoConstants.DefaultDistanceFunction"/>.</summary>
public static string GetVectorPropertyDistanceFunction(string? distanceFunction) => !string.IsNullOrWhiteSpace(distanceFunction) ? distanceFunction! : MongoConstants.DefaultDistanceFunction;
/// <summary>Returns search part of the search query for <see cref="IndexKind.Hnsw"/> index kind.</summary>
public static BsonDocument GetSearchQueryForHnswIndex<TVector>(
TVector vector,
string vectorPropertyName,
int limit,
int efSearch,
BsonDocument? filter)
{
var searchQuery = new BsonDocument
{
{ "vector", BsonArray.Create(vector) },
{ "path", vectorPropertyName },
{ "k", limit },
{ "efSearch", efSearch }
};
if (filter is not null)
{
searchQuery["filter"] = filter;
}
return new BsonDocument
{
{ "$search",
new BsonDocument
{
{ "cosmosSearch", searchQuery }
}
}
};
}
/// <summary>Returns search part of the search query for <see cref="IndexKind.IvfFlat"/> index kind.</summary>
public static BsonDocument GetSearchQueryForIvfIndex<TVector>(
TVector vector,
string vectorPropertyName,
int limit,
BsonDocument? filter)
{
var searchQuery = new BsonDocument
{
{ "vector", BsonArray.Create(vector) },
{ "path", vectorPropertyName },
{ "k", limit },
};
if (filter is not null)
{
searchQuery["filter"] = filter;
}
return new BsonDocument
{
{ "$search",
new BsonDocument
{
{ "cosmosSearch", searchQuery },
{ "returnStoredSource", true }
}
}
};
}
/// <summary>Returns projection part of the search query to return similarity score together with document.</summary>
public static BsonDocument GetProjectionQuery(string scorePropertyName, string documentPropertyName)
{
return new BsonDocument
{
{ "$project",
new BsonDocument
{
{ scorePropertyName, new BsonDocument { { "$meta", "searchScore" } } },
{ documentPropertyName, "$$ROOT" }
}
}
};
}
/// <summary>Returns a $match stage to filter results by score threshold.</summary>
/// <remarks>
/// Cosmos MongoDB returns a similarity score where higher values mean more similar,
/// so we filter with $gte to keep results at or above the threshold.
/// </remarks>
public static BsonDocument GetScoreThresholdMatchQuery(string scorePropertyName, double scoreThreshold)
=> new()
{
{
"$match", new BsonDocument
{
{ scorePropertyName, new BsonDocument { { "$gte", scoreThreshold } } }
}
}
};
}