Skip to content

Commit 5a3ee32

Browse files
authored
Support nested fields in vectorSearch (#1912)
1 parent 33f4ac4 commit 5a3ee32

7 files changed

Lines changed: 166 additions & 8 deletions

File tree

psalm-baseline.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190
<file src="src/Builder/Stage/VectorSearchStage.php">
191191
<MixedArgumentTypeCoercion>
192192
<code><![CDATA[$filter]]></code>
193+
<code><![CDATA[$parentFilter]]></code>
193194
</MixedArgumentTypeCoercion>
194195
</file>
195196
<file src="src/Builder/Type/CombinedFieldQuery.php">

src/Builder/Stage/FactoryTrait.php

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Stage/FluentFactoryTrait.php

Lines changed: 6 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Builder/Stage/VectorSearchStage.php

Lines changed: 23 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/Builder/Stage/Pipelines.php

Lines changed: 94 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/Builder/Stage/VectorSearchStageTest.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace MongoDB\Tests\Builder\Stage;
66

7+
use DateTimeImmutable;
8+
use DateTimeZone;
79
use MongoDB\Builder\Pipeline;
810
use MongoDB\Builder\Query;
911
use MongoDB\Builder\Stage;
@@ -83,11 +85,44 @@ public function testENN(): void
8385
$this->assertSamePipeline(Pipelines::VectorSearchENN, $pipeline);
8486
}
8587

88+
public function testNestedField(): void
89+
{
90+
$pipeline = new Pipeline(
91+
Stage::vectorSearch(
92+
index: 'vector_index',
93+
limit: 5,
94+
path: 'reviews.comments_embedding',
95+
queryVector: [0.010745884850621223, -0.035673413425683975, -0.09298479557037354],
96+
filter: Query::query(...['reviews.date' => Query::gte(new DateTimeImmutable('2000-01-01', new DateTimeZone('UTC')))]),
97+
numCandidates: 100,
98+
nestedOptions: ['scoreMode' => 'avg'],
99+
parentFilter: Query::query(
100+
...['address.country' => Query::in(['United States'])],
101+
bedrooms: [Query::gte(2), Query::lte(3)],
102+
property_type: Query::in(['House', 'Apartment']),
103+
),
104+
),
105+
Stage::project(
106+
...['reviews.comments' => 1],
107+
_id: 0,
108+
name: 1,
109+
address: 1,
110+
neighborhood_overview: 1,
111+
bedrooms: 1,
112+
property_type: 1,
113+
score: ['$meta' => 'vectorSearchScore'],
114+
),
115+
);
116+
117+
$this->assertSamePipeline(Pipelines::VectorSearchNestedField, $pipeline);
118+
}
119+
86120
public function testStoredSource(): void
87121
{
88122
$pipeline = new Pipeline(
89123
Stage::vectorSearch(
90124
index: 'vector_index',
125+
limit: 10,
91126
path: 'plot_embedding',
92127
queryVector: [-0.03994801267981529, -0.016522614285349846, -0.008775344118475914],
93128
filter: Query::and(
@@ -101,7 +136,6 @@ public function testStoredSource(): void
101136
genres: Query::in(['Action', 'Drama', 'Comedy']),
102137
),
103138
),
104-
limit: 10,
105139
numCandidates: 1000,
106140
returnStoredSource: true,
107141
),

0 commit comments

Comments
 (0)