|
| 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>`__ |
0 commit comments