|
2 | 2 |
|
3 | 3 | ================================
|
4 | 4 | Run an Atlas Vector Search Query
|
5 |
| -================================ |
| 5 | +================================ |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: full text, text analyzer, meta, pipeline, scoring, Lucene, AI, artificial intelligence, code example, semantic, nearest |
| 13 | + :description: Learn about how to use Atlas Vector Search in the {+driver-short+}. |
| 14 | + |
| 15 | +.. contents:: On this page |
| 16 | + :local: |
| 17 | + :backlinks: none |
| 18 | + :depth: 2 |
| 19 | + :class: singlecol |
| 20 | + |
| 21 | +Overview |
| 22 | +-------- |
| 23 | + |
| 24 | +In this guide, you can learn how to use the {+driver-short+} to perform |
| 25 | +:atlas:`Atlas Vector Search </atlas-vector-search/vector-search-overview/>` |
| 26 | +queries. The ``Aggregates`` builders class provides the |
| 27 | +the ``vectorSearch()`` helper method that you can use to |
| 28 | +create a :atlas:`$vectorSearch </atlas-vector-search/vector-search-stage/>` |
| 29 | +pipeline stage. |
| 30 | + |
| 31 | +.. important:: Feature Compatibility |
| 32 | + |
| 33 | + To learn which versions of MongoDB Atlas support this feature, see |
| 34 | + :atlas:`Limitations </atlas-vector-search/vector-search-stage/#limitations>` |
| 35 | + in the MongoDB Atlas documentation. |
| 36 | + |
| 37 | +Perform a Vector Search |
| 38 | +----------------------- |
| 39 | + |
| 40 | +Before you can perform Atlas Vector Search queries, you must create an Atlas Vector Search |
| 41 | +index on your collection. To learn about how to programmatically create a |
| 42 | +vector search index, see the :ref:`kotlin-sync-search-avs-indexes` guide. |
| 43 | + |
| 44 | +Then, you can run an Atlas Vector Search query by using the |
| 45 | +``vectorSearch()`` method in an aggregation pipeline. This |
| 46 | +method accepts the following parameters: |
| 47 | + |
| 48 | +- ``path``: The field to search |
| 49 | +- ``queryVector``: The vector embedding that represents your search query |
| 50 | +- ``indexName``: The name of the Atlas Vector Search index to use |
| 51 | +- ``limit``: The maximum number of results to return |
| 52 | +- ``options``: *(Optional)* A set of options that you can use to configure the |
| 53 | + vector search query |
| 54 | + |
| 55 | +Vector Search Example |
| 56 | +~~~~~~~~~~~~~~~~~~~~~ |
| 57 | + |
| 58 | +The following example performs an Atlas Vector Search query on the ``plot_embedding`` |
| 59 | +vector field and prints the ``title`` value of the first five results: |
| 60 | + |
| 61 | +.. io-code-block:: |
| 62 | + |
| 63 | + .. input:: /includes/vector-search.kt |
| 64 | + :start-after: start-vs |
| 65 | + :end-before: end-vs |
| 66 | + :language: kotlin |
| 67 | + :dedent: |
| 68 | + |
| 69 | + .. output:: |
| 70 | + :visible: false |
| 71 | + |
| 72 | + {"title": "Berserk: The Golden Age Arc I - The Egg of the King"} |
| 73 | + {"title": "Rollerball"} |
| 74 | + {"title": "After Life"} |
| 75 | + {"title": "What Women Want"} |
| 76 | + {"title": "Truth About Demons"} |
| 77 | + |
| 78 | +.. tip:: Query Vector Type |
| 79 | + |
| 80 | + The preceding example creates an instance of ``BinaryVector`` to |
| 81 | + serve as the query vector, but you can also create a ``List`` of |
| 82 | + ``Double`` instances. However, we recommend that you use the |
| 83 | + ``BinaryVector`` type to improve storage efficiency. |
| 84 | + |
| 85 | +The following example shows how to run the aggregation and print |
| 86 | +the vector search meta-score from the result of the preceding |
| 87 | +aggregation pipeline: |
| 88 | + |
| 89 | +.. io-code-block:: |
| 90 | + |
| 91 | + .. input:: /includes/vector-search.kt |
| 92 | + :start-after: start-vs-score |
| 93 | + :end-before: end-vs-score |
| 94 | + :language: kotlin |
| 95 | + :dedent: |
| 96 | + |
| 97 | + .. output:: |
| 98 | + :visible: false |
| 99 | + |
| 100 | + Title: Berserk: The Golden Age Arc I - The Egg of the King, Score: 0.49899211525917053 |
| 101 | + Title: Rollerball, Score: 0.4976102113723755 |
| 102 | + Title: After Life, Score: 0.4965665936470032 |
| 103 | + Title: What Women Want, Score: 0.49622756242752075 |
| 104 | + Title: Truth About Demons, Score: 0.49614521861076355 |
| 105 | + |
| 106 | +.. tip:: Vector Search Tutorials |
| 107 | + |
| 108 | + To view more tutorials that show how to run Atlas Vector Search queries, |
| 109 | + see the :atlas:`Atlas Vector Search Tutorials </atlas-vector-search/tutorials>` |
| 110 | + in the MongoDB Atlas documentation. |
| 111 | + |
| 112 | +API Documentation |
| 113 | +----------------- |
| 114 | + |
| 115 | +To learn more about the methods and types mentioned in this |
| 116 | +guide, see the following API documentation: |
| 117 | + |
| 118 | +- `Aggregates.vectorSearch() |
| 119 | + <{+core-api+}/client/model/Aggregates.html#vectorSearch(com.mongodb.client.model.search.FieldSearchPath,java.lang.Iterable,java.lang.String,long,com.mongodb.client.model.search.VectorSearchOptions)>`__ |
| 120 | + |
| 121 | +- `FieldSearchPath |
| 122 | + <{+core-api+}/client/model/search/FieldSearchPath.html>`__ |
| 123 | + |
| 124 | +- `VectorSearchOptions |
| 125 | + <{+core-api+}/client/model/search/VectorSearchOptions.html>`__ |
| 126 | + |
| 127 | +- `Projections.metaVectorSearchScore() |
| 128 | + <{+core-api+}/client/model/Projections.html#metaVectorSearchScore(java.lang.String)>`__ |
0 commit comments