Skip to content

DOCSP-48385 Make Atlas Vector Search page #1042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 26 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions snooty.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ driver-short = "Node.js driver"
mdb-server = "MongoDB Server"
min-node-version = "v16.20.1"
stable-api = "Stable API"
vector-search = "Atlas Vector Search"
136 changes: 136 additions & 0 deletions source/atlas-vector-search.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
.. _node-atlas-vector-search:

================================
Run an Atlas Vector Search Query
================================

.. facet::
:name: genre
:values: reference

.. meta::
:keywords: code example, semantic, nearest

.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol

Overview
--------

In this guide, you can learn how to use the Atlas Vector Search feature
in the {+driver-short+}.

You can use {+vector-search+} to perform a vector search on your data stored in
Atlas. Vector search allows you to query your data based on semantic meaning
rather than just keyword matches, which helps you retrieve more relevant search
results. It enables your AI-powered applications to support use cases such as
semantic search, hybrid search, and generative search, including
Retrieval-Augmented Generation (RAG).

To learn more about {+vector-search+}, see the :atlas:`{+vector-search+}
</atlas-vector-search/vector-search-overview/>` guides in the MongoDB Atlas
documentation.

.. important:: Feature Compatibility

To learn what versions of MongoDB Atlas support this feature, see
:atlas:`Limitations </atlas-vector-search/vector-search-stage/#limitations>`
in the MongoDB Atlas documentation.

Perform a Vector Search
-----------------------

To use this feature, you must create a vector search index and index your
vector embeddings. To learn about how to programmatically create a
vector search index, see the :atlas:`Create an Atlas Vector Search Index
</atlas-vector-search/vector-search-type/#create-an-atlas-vector-search-index>` section in the
Atlas documentation. To learn more about vector embeddings, see
:atlas:`How to Create Vector Embeddings
</atlas-vector-search/create-embeddings/>` in the Atlas documentation.

After you create a vector search index on your vector embeddings, you
can reference this index in your pipeline stage, as shown in the
following example.

Sample Data
~~~~~~~~~~~

The example on this page shows how to build an aggregation pipeline that uses the
``$vectorSearch`` stage to perform a vector search on the
``sample_mflix.embedded_movies`` collection in the :atlas:`Atlas sample datasets
</sample-data>`.

To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the
:atlas:`Get Started with Atlas </getting-started>` guide.

Vector Search Example
~~~~~~~~~~~~~~~~~~~~~

You can perform a vector search query by using the ``$vectorSearch`` stage
in an :ref:`aggregation pipeline <node-aggregation>`. To perform a vector
search on a collection, you must first have a collection with a field that contains
vector embeddings and a vector search index that covers that field.

In the following example, the aggregation pipeline searches the ``plot`` field of each
document in the collection for text semantically related to the term "time travel". The
``queryVector`` field in the ``$vectorSearch`` pipeline is the vector representation of
your query.

.. io-code-block::
:copyable: true

.. input:: /includes/aggregation/atlas-vector-search-example.js
:language: javascript
:dedent:

.. output::
:language: json
:visible: false

'{"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","score":0.7892671227455139}'
'{"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","score":0.7843604683876038}'
'{"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","score":0.7801066637039185}'
`{"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","score":0.7789170742034912}`
'{"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","score":0.7771612405776978}'
'{"plot":"A time-travel experiment in which a robot probe is sent from the year 2073 to the year 1973 goes terribly wrong thrusting one of the project scientists, a man named Nicholas Sinclair into a...","title":"A.P.E.X.","score":0.7730885744094849}'
`{"plot":"Agent J travels in time to M.I.B.'s early days in 1969 to stop an alien from assassinating his friend Agent K and changing history.","title":"Men in Black 3","score":0.7712380886077881}`
'{"plot":"Bound by a shared destiny, a teen bursting with scientific curiosity and a former boy-genius inventor embark on a mission to unearth the secrets of a place somewhere in time and space that exists in their collective memory.","title":"Tomorrowland","score":0.7669923901557922}'
'{"plot":"With the help of his uncle, a man travels to the future to try and bring his girlfriend back to life.","title":"Love Story 2050","score":0.7649372816085815}'
'{"plot":"A dimension-traveling wizard gets stuck in the 21st century because cell-phone radiation interferes with his magic. With his home world on the brink of war, he seeks help from a jaded ...","title":"The Portal","score":0.7640786170959473}'

This query uses the ``$vectorSearch`` stage to:

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Perform an Approximate Nearest Neighbor (ANN) vector search

Include the type of search

- Perform an Approximate Nearest Neighbor (ANN) vector search

- Search for the specified term in the ``plot_embedding`` field

- Set the number of nearest neighbors used in the search to 150 by using the
``numCandidates`` option

- Return a maximum of 10 documents from the query using the ``limit`` option

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Return 10 documents by using the ``limit`` option

S: Add a description of what limit does

It uses the ``$project`` stage to:

- Only include the movie ``plot`` and ``title`` fields in the results

- Add a ``score`` field to show the relevance of each result to the search term

Additional Information
----------------------

To see more Atlas Vector Search tutorials for the {+driver-short+}, see the :atlas:`Atlas
Vector Search tutorials </atlas-vector-search/tutorials/>` in the Atlas documentation.

To learn more about the syntax of the ``$vectorSearch`` pipeline stage,
see the Syntax and Fields sections of the
:atlas:`Create and Run Queries </atlas-vector-search/vector-search-stage/#syntax>`
guide in the Atlas Vector Search section of the Atlas documentation.

API Documentation
~~~~~~~~~~~~~~~~~

To learn more about the ``aggregate()`` method, see `aggregate()
<{+api+}/classes/Collection.html#aggregate>`__ in the API documentation.
Loading