Skip to content

Commit ff3b660

Browse files
committed
Updates following code review.
1 parent 90d097d commit ff3b660

3 files changed

Lines changed: 39 additions & 6 deletions

File tree

src/MongoDB.Driver/CreateVectorSearchIndexModelBase.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System.Collections.Generic;
1717
using System.Linq;
1818
using MongoDB.Bson;
19+
using MongoDB.Driver.Core.Misc;
1920

2021
namespace MongoDB.Driver;
2122

@@ -86,6 +87,8 @@ protected CreateVectorSearchIndexModelBase(
8687
params FieldDefinition<TDocument>[] filterFields)
8788
: base(name, SearchIndexType.VectorSearch)
8889
{
90+
Ensure.IsNotNull(field, nameof(field));
91+
8992
Field = field;
9093
FilterFields = filterFields?.ToList() ?? [];
9194
}
@@ -152,11 +155,12 @@ private protected void RenderCommonElements(RenderArgs<TDocument> renderArgs, Bs
152155
/// <param name="fieldDocument">The field document into which the elements will go.</param>
153156
private protected void RenderCommonFieldElements(RenderArgs<TDocument> renderArgs, BsonDocument fieldDocument)
154157
{
155-
var quantizationValue = Quantization == VectorQuantization.BinaryNoRescore
156-
? "binaryNoRescore"
157-
: Quantization?.ToString().ToLowerInvariant();
158-
159-
fieldDocument.Add("quantization", quantizationValue, quantizationValue != null);
158+
if (Quantization != null && Quantization != VectorQuantization.None)
159+
{
160+
fieldDocument.Add("quantization", Quantization == VectorQuantization.BinaryNoRescore
161+
? "binaryNoRescore"
162+
: Quantization.ToString().ToLowerInvariant());
163+
}
160164

161165
if (HnswMaxEdges != null || HnswNumEdgeCandidates != null)
162166
{

src/MongoDB.Driver/PipelineStageDefinitionBuilder.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2183,8 +2183,14 @@ public static PipelineStageDefinition<TInput, TInput> VectorSearch<TInput>(
21832183
{ "returnStoredSource", true, options?.ReturnStoredSource == true },
21842184
};
21852185

2186-
if (nestedRoot != null && options?.NestedFilter != null)
2186+
if (options?.NestedFilter != null)
21872187
{
2188+
if (nestedRoot == null)
2189+
{
2190+
throw new InvalidOperationException(
2191+
$"A nested filter was specified for the search against field '{path}', but the field is not nested. Nested filters can only be used when searching against vectors in nested (embedded) documents.");
2192+
}
2193+
21882194
vectorSearchOperator.Add("filter", options.NestedFilter.Render(
21892195
(args with { SubPathRoot = nestedRoot }) with { RenderDollarForm = true }));
21902196
}

tests/MongoDB.Driver.Tests/PipelineDefinitionBuilderTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ private class MovieWithPlot
707707
{
708708
public int Year { get; set; }
709709
public NestedPlot Plot { get; set; }
710+
public float[] NonNestedEmbedding { get; set; }
710711
}
711712

712713
private class NestedPlot
@@ -738,6 +739,28 @@ public void VectorSearch_should_throw_when_filter_is_at_wrong_level([Values(fals
738739
.Which.Message.Should().Contain("The field 'Year' is not part of the nested document 'Plot', which is the root of this search.");
739740
}
740741

742+
[Theory]
743+
[ParameterAttributeData]
744+
public void VectorSearch_should_throw_when_nested_filter_is_used_without_nesting([Values(false, true)] bool expressions)
745+
{
746+
var pipeline = new EmptyPipelineDefinition<MovieWithPlot>();
747+
var options = new VectorSearchOptions<MovieWithPlot>()
748+
{
749+
IndexName = "index_name",
750+
};
751+
752+
options.NestedFilter = expressions
753+
? Builders<MovieWithPlot>.Filter.Lt(m => m.Plot.Rating, 1900)
754+
: Builders<MovieWithPlot>.Filter.Lt("Plot.Rating", 1900);
755+
756+
var result = pipeline.VectorSearch(m => m.NonNestedEmbedding, new[] { 1.0, 2.0, 3.0 }, 1, options);
757+
758+
var exception = Record.Exception(() => RenderStages(result, BsonSerializer.SerializerRegistry.GetSerializer<MovieWithPlot>()));
759+
760+
exception.Should().BeOfType<InvalidOperationException>()
761+
.Which.Message.Should().Contain("A nested filter was specified for the search against field 'NonNestedEmbedding',");
762+
}
763+
741764
[Fact]
742765
public void VectorSearch_should_throw_when_pipeline_is_null()
743766
{

0 commit comments

Comments
 (0)