Skip to content

Commit 403d10a

Browse files
committed
wip
1 parent 9163555 commit 403d10a

File tree

2 files changed

+136
-2
lines changed

2 files changed

+136
-2
lines changed

docs/fundamentals/as-avs.txt

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ following optional parameters to ``search()``:
118118
- ``concurrent``: ``bool``
119119
- ``count``: ``string``
120120
- ``searchAfter``: ``string``
121-
- ``searchBefore`: ``string```
121+
- ``searchBefore`` ``string``
122122
- ``scoreDetails``: ``bool``
123123
- ``sort``: ``array``
124124
- ``returnStoredSource``: ``bool``
@@ -184,3 +184,79 @@ method to customize the query:
184184
To learn more about these parameters, see the :atlas:`Options
185185
</atlas-search/autocomplete/#options>` section of the
186186
``autocomplete`` operator reference in the Atlas documentation.
187+
188+
Atlas Vector Search
189+
-------------------
190+
191+
In this section, you can learn how to create Atlas Vector Search indexes
192+
and perform searches in the {+odm-short+}. The Atlas Vector Search API
193+
internally uses the ``$vectorSearch`` aggregation operator to perform queries.
194+
To learn more about this operator, see the :atlas:`$vectorSearch
195+
</atlas-vector-search/vector-search-stage/#mongodb-pipeline-pipe.-vectorSearch>`
196+
reference in the Atlas documentation.
197+
198+
.. _laravel-avs-index:
199+
200+
Create an Atlas Vector Search Index
201+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
202+
203+
.. TODO in DOCSP-46230
204+
205+
Perform Queries
206+
~~~~~~~~~~~~~~~
207+
208+
In this section, you can learn how to use the Atlas Vector Search API in
209+
the {+odm-short+}. The {+odm-short+} provides the ``vectorSearch()``
210+
method as a query builder method and as an Eloquent model method. You
211+
can use the ``vectorSearch()`` method to run Atlas Vector Search queries
212+
on documents in your collections.
213+
214+
You must pass the following parameters to the ``search()`` method:
215+
216+
- ``index``: Name of the vector search index (type: ``string``)
217+
- ``path``: Field that stores vector embeddings (type: ``array`` or ``string``)
218+
- ``queryVector``: Vector representation of your query (type: ``array``)
219+
- ``limit``: Number of results to return (type: ``int``)
220+
221+
The following code uses the index created in the preceding
222+
:ref:`laravel-avs-index` section to perform an Atlas Vector Search query on the
223+
``movies`` collection:
224+
225+
.. io-code-block::
226+
:copyable: true
227+
228+
.. input:: /includes/fundamentals/as-avs/AtlasSearchTest.php
229+
:language: php
230+
:dedent:
231+
:start-after: start-vs-query
232+
:end-before: end-vs-query
233+
234+
.. output::
235+
:language: json
236+
:visible: false
237+
238+
[
239+
{ "title": "Sunrising",
240+
"plot": "A shy teenager discovers confidence and new friendships during a transformative summer camp experience."
241+
},
242+
{ "title": "Last Semester",
243+
"plot": "High school friends navigate love, identity, and unexpected challenges before graduating together."
244+
}
245+
]
246+
247+
You can use the ``vector()`` method to perform many types of Atlas
248+
Search queries. Depending on your desired query, you can pass the
249+
following optional parameters to ``search()``:
250+
251+
- ``exact``: ``bool`` (default: ``false``)
252+
- ``filter``: ``QueryInterface`` or ``array``
253+
- ``numCandidates``: ``int`` or ``null`` (default: ``null``)
254+
255+
.. tip::
256+
257+
To construct a ``QueryInterface`` instance, you must import the
258+
``MongoDB\Builder\Query`` class into your application.
259+
260+
To learn more about these parameters, see the :atlas:`Fields
261+
</atlas-vector-search/vector-search-stage/#fields>` section of the
262+
``$vectorSearch`` operator reference in the Atlas documentation.

docs/includes/fundamentals/as-avs/AtlasSearchTest.php

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,20 @@
66

77
use App\Models\Movie;
88
use Illuminate\Support\Facades\DB;
9-
use MongoDB\Laravel\Tests\TestCase;
9+
use MongoDB\Builder\Query;
1010
use MongoDB\Builder\Search;
11+
use MongoDB\Laravel\Tests\TestCase;
12+
13+
use function array_map;
14+
use function mt_getrandmax;
15+
use function rand;
16+
use function range;
17+
use function srand;
1118

1219
class AtlasSearchTest extends TestCase
1320
{
21+
private array $vectors;
22+
1423
protected function setUp(): void
1524
{
1625
require_once __DIR__ . '/Movie.php';
@@ -30,6 +39,13 @@ protected function setUp(): void
3039
['title' => 'Jakob the Liar', 'year' => 1999],
3140
['title' => 'Emily Calling Jake', 'year' => 2001],
3241
]);
42+
43+
Movie::insert($this->addVector([
44+
['title' => 'A', 'plot' => 'A shy teenager discovers confidence and new friendships during a transformative summer camp experience.'],
45+
['title' => 'B', 'plot' => 'A detective teams up with a hacker to unravel a global conspiracy threatening personal freedoms.'],
46+
['title' => 'C', 'plot' => 'High school friends navigate love, identity, and unexpected challenges before graduating together.'],
47+
['title' => 'D', 'plot' => 'Stranded on a distant planet, astronauts must repair their ship before supplies run out.'],
48+
]));
3349
}
3450

3551
/**
@@ -63,4 +79,46 @@ public function autocompleteSearchTest(): void
6379
$this->assertNotNull($movies);
6480
$this->assertCount(3, $movies);
6581
}
82+
83+
/**
84+
* @runInSeparateProcess
85+
* @preserveGlobalState disabled
86+
*/
87+
public function vectorSearchTest(): void
88+
{
89+
// start-vs-query
90+
$movies = Book::vectorSearch(
91+
index: 'vector',
92+
path: 'vector_embeddings',
93+
// Vector representation of the query "coming of age"
94+
queryVector: [-0.0016261312, -0.028070757, ...],
95+
limit: 3,
96+
);
97+
// end-vs-query
98+
99+
$results = Book::vectorSearch(
100+
index: 'vector',
101+
path: 'vector4',
102+
queryVector: $this->vectors[0],
103+
limit: 5,
104+
numCandidates: 15,
105+
filter: Query::query(
106+
title: Query::ne('A'),
107+
),
108+
);
109+
110+
$this->assertNotNull($results);
111+
$this->assertSame('C', $results->first()->title);
112+
}
113+
114+
/** Generate random vectors using fixed seed to make tests deterministic */
115+
private function addVector(array $items): array
116+
{
117+
srand(1);
118+
foreach ($items as &$item) {
119+
$this->vectors[] = $item['vector4'] = array_map(fn () => rand() / mt_getrandmax(), range(0, 3));
120+
}
121+
122+
return $items;
123+
}
66124
}

0 commit comments

Comments
 (0)