|
| 1 | +.. _node-atlas-vector-search: |
| 2 | + |
| 3 | +================================ |
| 4 | +Run an Atlas Vector Search Query |
| 5 | +================================ |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: code example, semantic, nearest |
| 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 use the Atlas Vector Search feature |
| 24 | +in the {+driver-short+}. |
| 25 | + |
| 26 | +You can use {+vector-search+} to perform a vector search on your data stored in |
| 27 | +Atlas. Vector search allows you to query your data based on semantic meaning |
| 28 | +rather than just keyword matches, which helps you retrieve more relevant search |
| 29 | +results. It enables your AI-powered applications to support use cases such as |
| 30 | +semantic search, hybrid search, and generative search, including |
| 31 | +Retrieval-Augmented Generation (RAG). |
| 32 | + |
| 33 | +To learn more about {+vector-search+}, see the :atlas:`{+vector-search+} |
| 34 | +</atlas-vector-search/vector-search-overview/>` guides in the MongoDB Atlas |
| 35 | +documentation. |
| 36 | + |
| 37 | +.. important:: Feature Compatibility |
| 38 | + |
| 39 | + To learn what versions of MongoDB Atlas support this feature, see |
| 40 | + :atlas:`Limitations </atlas-vector-search/vector-search-stage/#limitations>` |
| 41 | + in the MongoDB Atlas documentation. |
| 42 | + |
| 43 | +Perform a Vector Search |
| 44 | +----------------------- |
| 45 | + |
| 46 | +To use this feature, you must create a vector search index and index your |
| 47 | +vector embeddings. To learn about how to programmatically create a |
| 48 | +vector search index, see the :atlas:`Create an Atlas Vector Search Index |
| 49 | +</atlas-vector-search/vector-search-type/#create-an-atlas-vector-search-index>` section in the |
| 50 | +Atlas documentation. To learn more about vector embeddings, see |
| 51 | +:atlas:`How to Create Vector Embeddings |
| 52 | +</atlas-vector-search/create-embeddings/>` in the Atlas documentation. |
| 53 | + |
| 54 | +After you create a vector search index on your vector embeddings, you |
| 55 | +can reference this index in your pipeline stage, as shown in the |
| 56 | +following example. |
| 57 | + |
| 58 | +Sample Data |
| 59 | +~~~~~~~~~~~ |
| 60 | + |
| 61 | +The example on this page shows how to build an aggregation pipeline that uses the |
| 62 | +``$vectorSearch`` stage to perform a vector search on the |
| 63 | +``sample_mflix.embedded_movies`` collection in the :atlas:`Atlas sample datasets |
| 64 | +</sample-data>`. |
| 65 | + |
| 66 | +To learn how to create a free MongoDB Atlas cluster and load the sample datasets, see the |
| 67 | +:atlas:`Get Started with Atlas </getting-started>` guide. |
| 68 | + |
| 69 | +Vector Search Example |
| 70 | +~~~~~~~~~~~~~~~~~~~~~ |
| 71 | + |
| 72 | +You can perform a vector search query by using the ``$vectorSearch`` stage |
| 73 | +in an :ref:`aggregation pipeline <node-aggregation>`. To perform a vector |
| 74 | +search on a collection, you must first have a collection with a field that contains |
| 75 | +vector embeddings and a vector search index that covers that field. |
| 76 | + |
| 77 | +In the following example, the aggregation pipeline searches the ``plot`` field of each |
| 78 | +document in the collection for text semantically related to the term "time travel". The |
| 79 | +``queryVector`` field in the ``$vectorSearch`` pipeline is the vector representation of |
| 80 | +your query. |
| 81 | + |
| 82 | +.. io-code-block:: |
| 83 | + :copyable: true |
| 84 | + |
| 85 | + .. input:: /includes/aggregation/atlas-vector-search-example.js |
| 86 | + :language: javascript |
| 87 | + :dedent: |
| 88 | + |
| 89 | + .. output:: |
| 90 | + :language: javascript |
| 91 | + :visible: false |
| 92 | + |
| 93 | + { |
| 94 | + plot: 'A reporter, learning of time travelers visiting 20th century disasters, tries to change the history they know by averting upcoming disasters.', |
| 95 | + title: 'Thrill Seekers', |
| 96 | + score: 0.9259490966796875 |
| 97 | + } |
| 98 | + { |
| 99 | + 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.', |
| 100 | + title: 'About Time', |
| 101 | + score: 0.9253997802734375 |
| 102 | + } |
| 103 | + { |
| 104 | + 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.', |
| 105 | + title: 'Timecop', |
| 106 | + score: 0.922332763671875 |
| 107 | + } |
| 108 | + { |
| 109 | + plot: "After using his mother's newly built time machine, Dolf gets stuck |
| 110 | + involuntary in the year 1212. He ends up in a children's crusade where he confronts |
| 111 | + his new friends with modern techniques...", |
| 112 | + title: 'Crusade in Jeans', |
| 113 | + score: 0.92205810546875 |
| 114 | + } |
| 115 | + { |
| 116 | + 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.', |
| 117 | + title: 'The Time Machine', |
| 118 | + score: 0.921875 |
| 119 | + } |
| 120 | + { |
| 121 | + 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...', |
| 122 | + title: 'A.P.E.X.', |
| 123 | + score: 0.9202728271484375 |
| 124 | + } |
| 125 | + { |
| 126 | + 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.", |
| 127 | + title: 'Men in Black 3', |
| 128 | + score: 0.9198150634765625 |
| 129 | + } |
| 130 | + { |
| 131 | + 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.', |
| 132 | + title: 'Tomorrowland', |
| 133 | + score: 0.91961669921875 |
| 134 | + } |
| 135 | + { |
| 136 | + plot: 'A romantic drama about a Chicago librarian with a gene that causes him to involuntarily time travel, and the complications it creates for his marriage.', |
| 137 | + title: "The Time Traveler's Wife", |
| 138 | + score: 0.9174346923828125 |
| 139 | + } |
| 140 | + { |
| 141 | + plot: 'With the help of his uncle, a man travels to the future to try and bring his girlfriend back to life.', |
| 142 | + title: 'Love Story 2050', |
| 143 | + score: 0.9165191650390625 |
| 144 | + } |
| 145 | + |
| 146 | +This query uses the ``$vectorSearch`` stage to: |
| 147 | + |
| 148 | +- Perform an Approximate Nearest Neighbor (ANN) vector search |
| 149 | + |
| 150 | +- Search for the specified term in the ``plot_embedding`` field |
| 151 | + |
| 152 | +- Set the number of nearest neighbors used in the search to 150 by using the |
| 153 | + ``numCandidates`` option |
| 154 | + |
| 155 | +- Return a maximum of 10 documents from the query using the ``limit`` option |
| 156 | + |
| 157 | +It uses the ``$project`` stage to: |
| 158 | + |
| 159 | +- Only include the movie ``plot`` and ``title`` fields in the results |
| 160 | + |
| 161 | +- Add a ``score`` field to show the relevance of each result to the search term |
| 162 | + |
| 163 | +Additional Information |
| 164 | +---------------------- |
| 165 | + |
| 166 | +To see more Atlas Vector Search tutorials for the {+driver-short+}, see the :atlas:`Atlas |
| 167 | +Vector Search tutorials </atlas-vector-search/tutorials/>` in the Atlas documentation. |
| 168 | + |
| 169 | +To learn more about the syntax of the ``$vectorSearch`` pipeline stage, |
| 170 | +see the Syntax and Fields sections of the |
| 171 | +:atlas:`Create and Run Queries </atlas-vector-search/vector-search-stage/#syntax>` |
| 172 | +guide in the Atlas Vector Search section of the Atlas documentation. |
| 173 | + |
| 174 | +API Documentation |
| 175 | +~~~~~~~~~~~~~~~~~ |
| 176 | + |
| 177 | +To learn more about the ``aggregate()`` method, see `aggregate() |
| 178 | +<{+api+}/classes/Collection.html#aggregate>`__ in the API documentation. |
0 commit comments