|
| 1 | +.. _java-atlas-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, 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:`Atlas Vector Search |
| 24 | +</atlas-vector-search/vector-search-overview/>` feature |
| 25 | +in the {+driver-short+}. The ``Aggregates`` builders class provides the |
| 26 | +the ``vectorSearch()`` helper method that you can use to |
| 27 | +create a :atlas:`$vectorSearch </atlas-vector-search/vector-search-stage/>` |
| 28 | +pipeline stage. This pipeline stage allows you to perform a **semantic |
| 29 | +search** on your documents. A semantic search is a type of search which |
| 30 | +locates information that is similar in meaning, but not necessarily |
| 31 | +identical, to your provided search term or phrase. |
| 32 | + |
| 33 | +.. important:: Feature Compatibility |
| 34 | + |
| 35 | + To learn what versions of MongoDB Atlas support this feature, see |
| 36 | + :atlas:`Limitations </atlas-vector-search/vector-search-stage/#limitations>` |
| 37 | + in the MongoDB Atlas documentation. |
| 38 | + |
| 39 | +Perform a Vector Search |
| 40 | +----------------------- |
| 41 | + |
| 42 | +To use this feature, you must create a vector search index and index your |
| 43 | +vector embeddings. To learn about how to programmatically create a |
| 44 | +vector search index, see the :ref:`java-search-indexes` section in the |
| 45 | +Indexes guide. To learn more about vector embeddings, see |
| 46 | +:atlas:`How to Index Vector Embeddings for Vector Search |
| 47 | +</atlas-search/field-types/knn-vector/>` in the Atlas documentation. |
| 48 | + |
| 49 | +After you create a vector search index on your vector embeddings, you |
| 50 | +can reference this index in your pipeline stage, as shown in the |
| 51 | +following section. |
| 52 | + |
| 53 | +Vector Search Example |
| 54 | +~~~~~~~~~~~~~~~~~~~~~ |
| 55 | + |
| 56 | +The following example shows how to build an aggregation pipeline that uses the |
| 57 | +``vectorSearch()`` and ``project()`` methods to compute a vector search score: |
| 58 | + |
| 59 | +.. literalinclude:: /includes/fundamentals/code-snippets/builders/AggBuilders.java |
| 60 | + :start-after: // begin vectorSearch |
| 61 | + :end-before: // end vectorSearch |
| 62 | + :language: java |
| 63 | + :dedent: |
| 64 | + |
| 65 | +.. tip:: Query Vector Type |
| 66 | + |
| 67 | + The preceding example creates an instance of ``BinaryVector`` to |
| 68 | + serve as the query vector, but you can also create a ``List`` of |
| 69 | + ``Double`` instances. However, we recommend that you use the |
| 70 | + ``BinaryVector`` type to improve storage efficiency. |
| 71 | + |
| 72 | +The following example shows how you can run the aggregation and print |
| 73 | +the vector search meta-score from the result of the preceding |
| 74 | +aggregation pipeline: |
| 75 | + |
| 76 | +.. literalinclude:: /includes/fundamentals/code-snippets/builders/AggBuilders.java |
| 77 | + :start-after: // begin vectorSearch-output |
| 78 | + :end-before: // end vectorSearch-output |
| 79 | + :language: java |
| 80 | + :dedent: |
| 81 | + |
| 82 | +.. tip:: Java Driver Vector Search Examples |
| 83 | + |
| 84 | + Visit the :atlas:`Atlas documentation </atlas-vector-search/tutorials/>` |
| 85 | + to find more tutorials on using the {+driver-short+} to perform Atlas |
| 86 | + Vector Searches. |
| 87 | + |
| 88 | +API Documentation |
| 89 | +----------------- |
| 90 | + |
| 91 | +To learn more about the methods and types mentioned in this |
| 92 | +guide, see the following API documentation: |
| 93 | + |
| 94 | +- `Aggregates.vectorSearch() |
| 95 | + <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Aggregates.html#vectorSearch(com.mongodb.client.model.search.FieldSearchPath,java.lang.Iterable,java.lang.String,long,com.mongodb.client.model.search.VectorSearchOptions)>`__ |
| 96 | + |
| 97 | +- `FieldSearchPath |
| 98 | + <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/search/FieldSearchPath.html>`__ |
| 99 | + |
| 100 | +- `VectorSearchOptions |
| 101 | + <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/search/VectorSearchOptions.html>`__ |
| 102 | + |
| 103 | +- `Projections.metaVectorSearchScore() |
| 104 | + <{+api+}/apidocs/mongodb-driver-core/com/mongodb/client/model/Projections.html#metaVectorSearchScore(java.lang.String)>`__ |
| 105 | + |
| 106 | +- `BinaryVector <{+api+}/apidocs/bson/org/bson/BinaryVector.html>`__ |
0 commit comments