Skip to content

Commit 7609432

Browse files
authored
CSHARP-5212: Exact Vector Search (#1521)
1 parent 2313984 commit 7609432

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

src/MongoDB.Driver/PipelineStageDefinitionBuilder.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1984,6 +1984,7 @@ public static PipelineStageDefinition<TInput, TInput> VectorSearch<TInput>(
19841984
Ensure.IsNotNull(field, nameof(field));
19851985
Ensure.IsNotNull(queryVector, nameof(queryVector));
19861986
Ensure.IsGreaterThanZero(limit, nameof(limit));
1987+
Ensure.That(options?.NumberOfCandidates is null || options.Exact == false, "Number of candidates must be omitted for exact nearest neighbour search (ENN).");
19871988

19881989
const string operatorName = "$vectorSearch";
19891990
var stage = new DelegatedPipelineStageDefinition<TInput, TInput>(
@@ -1996,9 +1997,10 @@ public static PipelineStageDefinition<TInput, TInput> VectorSearch<TInput>(
19961997
{ "queryVector", queryVector.Array },
19971998
{ "path", field.Render(args).FieldName },
19981999
{ "limit", limit },
1999-
{ "numCandidates", options?.NumberOfCandidates ?? limit * 10 },
2000+
{ "numCandidates", options?.NumberOfCandidates ?? limit * 10, options?.Exact != true },
20002001
{ "index", options?.IndexName ?? "default" },
2001-
{ "filter", () => options.Filter.Render(args with { RenderDollarForm = true }), options?.Filter != null },
2002+
{ "filter", () => options?.Filter.Render(args with { RenderDollarForm = true }), options?.Filter != null },
2003+
{ "exact", true, options?.Exact == true }
20022004
};
20032005

20042006
var document = new BsonDocument(operatorName, vectorSearchOperator);

src/MongoDB.Driver/VectorSearchOptions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,11 @@ public sealed class VectorSearchOptions<TDocument>
3535
/// Gets or sets the number of candidates.
3636
/// </summary>
3737
public int? NumberOfCandidates { get; set; }
38+
39+
/// <summary>
40+
/// Get or sets a value indicating if exact nearest neighbor (ENN) is to be used, false by default.
41+
/// If false, approximate nearest neighbor (ANN) is used.
42+
/// </summary>
43+
public bool Exact { get; set; }
3844
}
3945
}

0 commit comments

Comments
 (0)