Skip to content

Commit 6b8c389

Browse files
committed
DOCSP-48420: bson v2.14 vector support (#169)
(cherry picked from commit aeb0adc)
1 parent 0822969 commit 6b8c389

File tree

11 files changed

+394
-36
lines changed

11 files changed

+394
-36
lines changed

snooty.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ toc_landing_pages = [
1313
"/fundamentals/crud",
1414
"/usage-examples",
1515
"/fundamentals/indexes",
16+
"/fundamentals/aggregation",
1617
]
1718

1819
sharedinclude_root = "https://raw.githubusercontent.com/10gen/docs-shared/main/"
@@ -25,7 +26,7 @@ docs-branch = "master" # always set this
2526
version = "3.1.1" # always set this to the driver version (i.e. 2.6.0, 2.5.0, etc.)
2627
min-rust-version = "1.60" # always set this to the minimum supported Rust version
2728
api = "https://docs.rs/mongodb/{+version+}/mongodb"
28-
bson-version = "2.13.0"
29+
bson-version = "2.14.0"
2930
bson-api = "https://docs.rs/bson/{+bson-version+}/bson"
3031
stable-api = "Stable API"
3132
tracing-version = "0.1.37"

source/api.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=================
2+
API Documentation
3+
=================
4+
5+
.. toctree::
6+
:titlesonly:
7+
:maxdepth: 1
8+
9+
{+driver-long+} <{+api+}/index.html>
10+
BSON Crate <{+bson-api+}/index.html>
11+
12+
- `{+driver-long+} <{+api+}/index.html>`__
13+
- `BSON Crate <{+bson-api+}/index.html>`__

source/fundamentals/aggregation.txt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ Aggregation
1717
:depth: 2
1818
:class: singlecol
1919

20+
.. toctree::
21+
:titlesonly:
22+
:maxdepth: 1
23+
24+
Atlas Vector Search </fundamentals/aggregation/vector-search>
25+
2026
Overview
2127
--------
2228

@@ -152,7 +158,7 @@ The aggregation pipeline contains the following stages:
152158

153159
.. io-code-block::
154160

155-
.. input:: /includes/fundamentals/code-snippets/aggregation.rs
161+
.. input:: /includes/fundamentals/code-snippets/aggregation/aggregation.rs
156162
:start-after: begin-age-agg
157163
:end-before: end-age-agg
158164
:language: rust
@@ -191,7 +197,7 @@ The aggregation pipeline contains the following stages:
191197

192198
.. io-code-block::
193199

194-
.. input:: /includes/fundamentals/code-snippets/aggregation.rs
200+
.. input:: /includes/fundamentals/code-snippets/aggregation/aggregation.rs
195201
:start-after: begin-lastactive-agg
196202
:end-before: end-lastactive-agg
197203
:language: rust
@@ -225,7 +231,7 @@ The aggregation pipeline contains the following stages:
225231

226232
.. io-code-block::
227233

228-
.. input:: /includes/fundamentals/code-snippets/aggregation.rs
234+
.. input:: /includes/fundamentals/code-snippets/aggregation/aggregation.rs
229235
:start-after: begin-popular-agg
230236
:end-before: end-popular-agg
231237
:language: rust
@@ -260,6 +266,12 @@ Retrieve Data guide.
260266
To learn more about sorting results within an aggregation pipeline, see the
261267
:ref:`rust-sort-guide` guide.
262268

269+
Atlas Vector Search
270+
~~~~~~~~~~~~~~~~~~~
271+
272+
You can perform similarity searches on vector embeddings by using the
273+
Atlas Vector Search feature. To learn more, see the :ref:`rust-vector-search` guide.
274+
263275
API Documentation
264276
~~~~~~~~~~~~~~~~~
265277

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
.. _rust-vector-search:
2+
3+
===================
4+
Atlas Vector Search
5+
===================
6+
7+
.. facet::
8+
:name: genre
9+
:values: reference
10+
11+
.. meta::
12+
:keywords: code example, semantic, text, embeddings
13+
14+
.. contents:: On this page
15+
:local:
16+
:backlinks: none
17+
:depth: 2
18+
:class: singlecol
19+
20+
Overview
21+
--------
22+
23+
In this guide, you can learn how to perform searches on your documents
24+
by using the Atlas Vector Search feature. The {+driver-short+} allows you to
25+
perform Atlas Vector Search queries by using the aggregation framework.
26+
To learn more about performing aggregations, see the
27+
:ref:`rust-aggregation` guide.
28+
29+
.. note:: Deployment Compatibility
30+
31+
You can use the Atlas Vector Search feature only when
32+
you connect to MongoDB Atlas clusters. This feature is not
33+
available for self-managed deployments.
34+
35+
To learn more about Atlas Vector Search, see the :atlas:`Atlas Vector
36+
Search Overview </atlas-vector-search/vector-search-overview/>`.
37+
38+
.. note:: Atlas Search
39+
40+
To perform advanced full-text search on your documents, you can use the
41+
Atlas Search feature. To learn about this feature, see the
42+
:atlas:`Atlas Search Overview </atlas-search/atlas-search-overview/>`.
43+
44+
.. TODO update when we add VS type updates to the linked page
45+
46+
.. Atlas Vector Search Index
47+
.. ~~~~~~~~~~~~~~~~~~~~~~~~~
48+
..
49+
.. Before you can perform Atlas Vector Search queries, you must create an
50+
.. Atlas Vector Search index on your collection. To learn more about
51+
.. creating this index type, see the :ref:`rust-atlas-search-indexes` guide.
52+
53+
Vector Search Aggregation Stage
54+
-------------------------------
55+
56+
To create a ``$vectorSearch`` stage in your aggregation pipeline, perform the
57+
following actions:
58+
59+
1. Create a vector to store the pipeline stages.
60+
61+
#. Specify the ``$vectorSearch`` operator and provide details about the
62+
vector search query.
63+
64+
You must define the following fields in your ``$vectorSearch`` stage:
65+
66+
.. list-table::
67+
:header-rows: 1
68+
69+
* - Parameter
70+
- Type
71+
- Description
72+
73+
* - ``index``
74+
- string
75+
- Name of the vector search index
76+
77+
* - ``path``
78+
- string
79+
- Field that contains vector embeddings
80+
81+
* - ``queryVector``
82+
- array of numbers
83+
- Vector representation of your query
84+
85+
* - ``limit``
86+
- number
87+
- Number of results to return
88+
89+
Atlas Search Query Examples
90+
---------------------------
91+
92+
In this section, you can learn how to perform Atlas Vector
93+
Search queries. The examples in this section use sample data from the
94+
``sample_mflix.embedded_movies`` collection. To learn how to load this
95+
sample data, see the :atlas:`Load Data into Atlas </sample-data/>`
96+
tutorial in the Atlas documentation.
97+
98+
Basic Vector Search Query
99+
~~~~~~~~~~~~~~~~~~~~~~~~~
100+
101+
The following code performs an Atlas Vector Search query on the
102+
``plot_embedding`` vector field by using a query vector that represents
103+
the term *time travel*:
104+
105+
.. io-code-block::
106+
:copyable: true
107+
108+
.. input:: /includes/fundamentals/code-snippets/aggregation/vector-search.rs
109+
:language: rust
110+
:dedent:
111+
:start-after: start-basic-query
112+
:end-before: end-basic-query
113+
114+
.. output::
115+
:language: json
116+
:visible: false
117+
118+
{ "plot": "A reporter, learning of time travelers visiting 20th century disasters, tries to change the history they know by averting upcoming disasters.", "title": "Thrill Seekers" }
119+
{ "plot": "At the age of 21, Tim discovers he can travel in time and change what happens and has happened in his own life. His decision to make his world a better place by getting a girlfriend turns out not to be as easy as you might think.", "title": "About Time" }
120+
{ "plot": "Hoping to alter the events of the past, a 19th century inventor instead travels 800,000 years into the future, where he finds humankind divided into two warring races.", "title": "The Time Machine" }
121+
{ "plot": "After using his mother's newly built time machine, Dolf gets stuck involuntary in the year 1212. He ends up in a children's crusade where he confronts his new friends with modern techniques...", "title": "Crusade in Jeans" }
122+
{ "plot": "An officer for a security agency that regulates time travel, must fend for his life against a shady politician who has a tie to his past.", "title": "Timecop" }
123+
124+
.. tip:: Query Vector Type
125+
126+
The preceding example creates an instance of
127+
``bson::binary::Vector``, introduced in ``bson`` v2.14,
128+
to serve as the query vector. You can also use a ``vec`` of
129+
numbers as a query vector, but we recommend that you use the
130+
``Vector`` type to improve storage efficiency.
131+
132+
Vector Search Score
133+
~~~~~~~~~~~~~~~~~~~
134+
135+
The following code performs the same query as in the preceding example,
136+
but outputs only the ``title`` field and ``vectorSearchScore`` meta
137+
field, which describes how well the document matches the query vector:
138+
139+
.. io-code-block::
140+
:copyable: true
141+
142+
.. input:: /includes/fundamentals/code-snippets/aggregation/vector-search.rs
143+
:language: rust
144+
:dedent:
145+
:start-after: start-score-query
146+
:end-before: end-score-query
147+
:emphasize-lines: 15
148+
149+
.. output::
150+
:language: json
151+
:visible: false
152+
153+
{ "title": "Thrill Seekers", "score": 0.9332504272460938 }
154+
{ "title": "About Time", "score": 0.9312690496444702 }
155+
{ "title": "The Time Machine", "score": 0.9295310378074646 }
156+
{ "title": "Crusade in Jeans", "score": 0.9290415048599243 }
157+
{ "title": "Timecop", "score": 0.9283164739608765 }
158+
159+
Vector Search Options
160+
---------------------
161+
162+
You can use a ``$vectorSearch`` stage to perform many types of Atlas
163+
Vector Search queries. Depending on your desired query, you can specify the
164+
following options in the stage definition:
165+
166+
.. list-table::
167+
:widths: 20 20 40 20
168+
:header-rows: 1
169+
170+
* - Optional Parameter
171+
- Type
172+
- Description
173+
- Default Value
174+
175+
* - ``exact``
176+
- boolean
177+
- Specifies whether to run an Exact Nearest Neighbor (``true``) or
178+
Approximate Nearest Neighbor (``false``) search
179+
- ``false``
180+
181+
* - ``filter``
182+
- document
183+
- Specifies a pre-filter for documents to search on
184+
- No filtering
185+
186+
* - ``numCandidates``
187+
- number
188+
- Specifies the number of nearest neighbors to use during the
189+
search
190+
- No limit
191+
192+
To learn more about these options, see the :atlas:`Fields
193+
</atlas-vector-search/vector-search-stage/#fields>` section of the
194+
``$vectorSearch`` operator reference in the Atlas documentation.
195+
196+
.. _rust-avs-addtl-info:
197+
198+
Additional Information
199+
----------------------
200+
201+
To learn more about the concepts mentioned in this guide, see the
202+
following Server manual entries:
203+
204+
- :atlas:`Run Vector Search Queries </atlas-vector-search/vector-search-stage/>`
205+
- :manual:`Aggregation Pipeline </core/aggregation-pipeline/>`
206+
- :manual:`Aggregation Stages </meta/aggregation-quick-reference/#stages>`
207+
208+
To learn more about the behavior of the ``aggregate()`` method, see the
209+
:ref:`Aggregation Operations <rust-retrieve-aggregation>` section of the
210+
Retrieve Data guide.
211+
212+
API Documentation
213+
~~~~~~~~~~~~~~~~~
214+
215+
To learn more about the methods and types mentioned in this
216+
guide, see the following API documentation:
217+
218+
- `aggregate() <{+api+}/struct.Collection.html#method.aggregate>`__
219+
- `Vector <{+bson-api+}/binary/enum.Vector.html>`__

source/fundamentals/crud/read-operations/retrieve.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ following documentation:
394394
- :ref:`rust-sort-guide` guide
395395
- :ref:`rust-skip-guide` guide
396396
- :ref:`rust-limit-guide` guide
397+
397398
.. - :ref:`rust-project-guide`
398399
.. - :ref:`rust-collations-guide`
399400

@@ -409,4 +410,4 @@ guide, see the following API documentation:
409410
- `FindOneOptions <{+api+}/options/struct.FindOneOptions.html>`__
410411
- `Cursor <{+api+}/struct.Cursor.html>`__
411412
- `aggregate() <{+api+}/struct.Collection.html#method.aggregate>`__
412-
- `AggregateOptions <{+api+}/options/struct.AggregateOptions.html>`__
413+
- `AggregateOptions <{+api+}/options/struct.AggregateOptions.html>`__

source/fundamentals/serialization.txt

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@ BSON is called **serialization**, while the reverse process is called
2020

2121
The Rust language uses a static type system, but BSON has a dynamic
2222
schema. To handle conversions between Rust types and BSON, the driver and the
23-
``bson`` library integrate functionality from the Serde framework. To
24-
learn how to install the ``serde`` crate, see `serde
25-
<https://crates.io/crates/serde>`__ at the ``crates.io`` crate registry.
23+
`bson <https://crates.io/crates/bson>`__ crate integrate functionality
24+
from the **Serde framework**.
25+
26+
.. tip::
27+
28+
The ``bson`` crate is exported with the ``mongodb`` crate. To learn how
29+
to install the ``serde`` crate, see `serde <https://crates.io/crates/serde>`__
30+
at the ``crates.io`` crate registry.
2631

2732
By implementing functionality from the ``serde`` crate into your
2833
application, you can use custom Rust types such as structs and enums
@@ -76,17 +81,27 @@ attribute before defining a Rust type:
7681
Custom Struct Example
7782
~~~~~~~~~~~~~~~~~~~~~
7883

79-
The following code defines a sample ``Vegetable`` struct that implements
84+
The following code defines a sample ``Article`` struct that implements
8085
the ``serde`` serialization traits:
8186

8287
.. literalinclude:: /includes/fundamentals/code-snippets/serialization.rs
8388
:language: rust
8489
:dedent:
85-
:start-after: begin-veg-struct
86-
:end-before: end-veg-struct
90+
:start-after: begin-struct
91+
:end-before: end-struct
92+
93+
.. tip:: Vector Type
94+
95+
Starting in ``bson`` library v2.14, you can use the
96+
`bson::binary::Vector <{+bson-api+}/binary/enum.Vector.html>`__ type
97+
to represent vectors of numeric values.
98+
99+
Since this type is serialized as a BSON binary vector, usage of
100+
``Vector`` can improve storage efficiency. To learn more, see the
101+
`BSON specification <https://bsonspec.org/spec.html>`__.
87102

88-
The following code accesses the ``vegetables`` collection with
89-
``Vegetable`` as its generic type parameter:
103+
The following code accesses the ``articles`` collection with
104+
the ``Article`` struct as its generic type parameter:
90105

91106
.. literalinclude:: /includes/fundamentals/code-snippets/serialization.rs
92107
:language: rust
@@ -95,14 +110,14 @@ The following code accesses the ``vegetables`` collection with
95110
:end-before: end-access-coll
96111

97112
Because the ``Collection`` instance is parameterized with the
98-
``Vegetable`` struct, you can perform CRUD operations with this type.
99-
The following code inserts a ``Vegetable`` instance into the collection:
113+
``Article`` struct, you can perform CRUD operations with this type.
114+
The following code inserts an ``Article`` instance into the collection:
100115

101116
.. literalinclude:: /includes/fundamentals/code-snippets/serialization.rs
102117
:language: rust
103118
:dedent:
104-
:start-after: begin-insert-veg
105-
:end-before: end-insert-veg
119+
:start-after: begin-insert-struct
120+
:end-before: end-insert-struct
106121

107122
Multiple Parameterizations
108123
~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -326,6 +341,7 @@ To learn more about the methods and types mentioned in this
326341
guide, see the following API documentation:
327342

328343
- `collection() <{+api+}/struct.Database.html#method.collection>`__
344+
- `Vector <{+bson-api+}/binary/enum.Vector.html>`__
329345
- `clone_with_type() <{+api+}/struct.Collection.html#method.clone_with_type>`__
330346
- `serialize_with <https://serde.rs/field-attrs.html#serialize_with>`__
331347
Serde attribute

0 commit comments

Comments
 (0)