Skip to content

Commit 91e93bc

Browse files
committed
DOCSP-49237: bson vector subtype
1 parent 21709e9 commit 91e93bc

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

snooty.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ patch-version-number = "{+version-number+}.0"
2727
version = "v{+version-number+}"
2828
stable-api = "Stable API"
2929
api-root = "https://www.mongodb.com/docs/ruby-driver/current/api/"
30-
bson-version = "5.0.2"
30+
bson-version = "5.1.0"
3131
avs = "Atlas Vector Search"

source/data-formats.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ application. To learn how to work with these data formats, see the following
3333
sections:
3434

3535
- :ref:`ruby-time-series`: Learn how to create a time series collection and interact with time series data.
36-
- :ref:`ruby-bson-tutorial`: Learn how to work with BSON data by using the {+driver-short+}'s BSON library.
36+
- :ref:`ruby-bson`: Learn how to work with BSON data by using the {+driver-short+}'s BSON library.

source/data-formats/bson.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,45 @@ You can use these methods to convert from one representation to another:
447447
BSON::Binary.from_uuid('77665544-3322-1100-ffeeddccbbaa9988',:java_legacy).to_uuid(:csharp_legacy)
448448
# => "33221100-5544-7766-8899aabbccddeeff"
449449

450+
.. _ruby-bson-vector:
451+
452+
BSON::Vector
453+
~~~~~~~~~~~~
454+
455+
Starting in ``bson`` v5.1, you can use the
456+
``BSON::Vector`` type to represent vectors of numeric values.
457+
458+
Because this type is serialized as a BSON binary vector (subtype 9), you
459+
can use the ``Vector`` type for improved storage efficiency. To learn
460+
more, see the `BSON specification <https://bsonspec.org/spec.html>`__.
461+
462+
You can create a ``Vector`` object to store values of the following
463+
types:
464+
465+
- ``int8``
466+
- ``float32``
467+
- ``packed_bit``
468+
469+
You can use the optional ``dtype`` and ``padding`` atrributes to specify
470+
the data type of the vector and the bit padding, respectively.
471+
472+
The following example demonstrates how to create a ``Vector`` object:
473+
474+
.. code-block:: ruby
475+
476+
BSON::Vector.new([ -0.0016261312, -0.028070757, -0.011342932 ], :float32)
477+
478+
The following list describes some methods that you can use
479+
to handle conversions between ``BSON::Binary`` and ``BSON::Vector``:
480+
481+
- ``Binary.as_vector``: Decodes binary data as a ``Vector`` object
482+
- ``Binary.from_vector``: Constructs a binary object from a ``Vector`` object
483+
484+
.. tip:: {+avs+}
485+
486+
To view an example that leverages the ``Vector`` type
487+
to perform efficient {+avs+} queries, see the :ref:`ruby-avs-examples`.
488+
450489
BSON::Code
451490
~~~~~~~~~~
452491

source/vector-search.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,15 @@ You must define the following fields in your ``$vectorSearch`` stage:
8282
- Field that contains vector embeddings
8383

8484
* - ``queryVector``
85-
- array of numbers
85+
- ``BSON::Vector`` instance or array of numbers
8686
- Vector representation of your query
8787

8888
* - ``limit``
8989
- number
9090
- Number of results to return
9191

92+
.. _ruby-avs-examples:
93+
9294
Atlas Search Query Examples
9395
---------------------------
9496

@@ -124,6 +126,15 @@ embedding of the phrase "time travel":
124126
{"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"}
125127
{"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"}
126128

129+
.. tip:: Query Vector Type
130+
131+
The preceding example creates an instance of
132+
``BSON::Vector``, introduced in ``bson`` gem v5.1,
133+
to serve as the query vector. You can also use an array of
134+
numbers as a query vector, but we recommend that you use the
135+
``Vector`` type to improve storage efficiency. To learn more about
136+
this type, see the :ref:`ruby-bson-vector` section of the BSON guide.
137+
127138
Vector Search Score
128139
~~~~~~~~~~~~~~~~~~~
129140

0 commit comments

Comments
 (0)