|
| 1 | +.. _csharp-atlas-vector-search: |
| 2 | + |
| 3 | +=================== |
| 4 | +{+vector-search+} |
| 5 | +=================== |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: .NET, search, semantic, AI, RAG |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +You can use {+vector-search+} to perform vector search on your data stored in |
| 24 | +Atlas. Vector search allows you to query your data based on semantic meaning |
| 25 | +rather than just keyword matches, which helps you retrieve more relevant search |
| 26 | +results. It enables your AI-powered applications to support use cases such as |
| 27 | +semantic search, hybrid search, and generative search, including |
| 28 | +Retrieval-Augmented Generation (RAG). |
| 29 | + |
| 30 | +By using Atlas as a vector database, you can seamlessly index vector data along |
| 31 | +with your other data in Atlas. This allows you to filter on fields in your |
| 32 | +collection and perform vector search queries against vector data. You can also |
| 33 | +combine vector search with full-text search queries to return the most relevant |
| 34 | +results for your use case. You can integrate {+vector-search+} with popular AI |
| 35 | +frameworks and services to easily implement vector search in your applications. |
| 36 | + |
| 37 | +To learn more about {+vector-search+}, see the :atlas:`{+vector-search+} |
| 38 | +</atlas-vector-search/vector-search-overview/>` guide in the MongoDB Atlas |
| 39 | +documentation. |
| 40 | + |
| 41 | +Supported Vector Embedding Types |
| 42 | +-------------------------------- |
| 43 | + |
| 44 | +:atlas:`Vector embeddings |
| 45 | +</atlas-vector-search/vector-search-overview/#std-term-vector-embeddings>` |
| 46 | +are vectors you use to represent your data. These embeddings |
| 47 | +capture meaningful relationships in your data and enable tasks like semantic |
| 48 | +search and retrieval. |
| 49 | + |
| 50 | +The {+driver-short+} supports vector embeddings of several types. The following |
| 51 | +sections describe the supported vector embedding types. |
| 52 | + |
| 53 | + |
| 54 | +.. _csharp-vector-array-representation: |
| 55 | + |
| 56 | +Array Representations |
| 57 | +~~~~~~~~~~~~~~~~~~~~~ |
| 58 | + |
| 59 | +The {+driver-short+} supports the following representations of the array |
| 60 | +type in vector embeddings: |
| 61 | + |
| 62 | +- ``BsonArray`` |
| 63 | +- ``Memory`` |
| 64 | +- ``ReadOnlyMemory`` |
| 65 | +- ``float[]`` and ``double[]`` |
| 66 | + |
| 67 | +The following example shows a class with properties of the preceding types: |
| 68 | + |
| 69 | +.. literalinclude:: /includes/fundamentals/code-examples/atlas-vector-search/VectorSearchExamples.cs |
| 70 | + :language: csharp |
| 71 | + :start-after: start-bson-arrays |
| 72 | + :end-before: end-bson-arrays |
| 73 | + |
| 74 | +.. _csharp-binary-vector-representation: |
| 75 | + |
| 76 | +Binary Vector Representations |
| 77 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 78 | + |
| 79 | +The {+driver-short+} supports the following binary vector representations in |
| 80 | +vector embeddings: |
| 81 | + |
| 82 | +- ``BinaryVectorFloat32`` (not supported on big-endian architectures) |
| 83 | +- ``BinaryVectorInt8`` |
| 84 | +- ``BinaryVectorPackedBit`` |
| 85 | +- ``Memory<float>``, ``Memory<byte>``, ``Memory<sbyte>`` |
| 86 | +- ``ReadOnlyMemory<float>``, ``ReadOnlyMemory<byte>``, ``ReadOnlyMemory<sbyte>`` |
| 87 | +- ``float[]``, ``byte[]``, ``sbyte[]`` |
| 88 | + |
| 89 | +.. note:: |
| 90 | + |
| 91 | + You must use the ``BinaryVector`` attribute when specifying binary vector |
| 92 | + representations of the ``Memory<T>``, ``ReadOnlyMemory<T>``, or array |
| 93 | + types. |
| 94 | + |
| 95 | +The following example shows a class with properties of the preceding types: |
| 96 | + |
| 97 | +.. literalinclude:: /includes/fundamentals/code-examples/atlas-vector-search/VectorSearchExamples.cs |
| 98 | + :language: csharp |
| 99 | + :start-after: start-binary-vectors |
| 100 | + :end-before: end-binary-vectors |
| 101 | + |
| 102 | +Binary Vector Data Serialization |
| 103 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 104 | + |
| 105 | +You can serialize ``Int8`` binary vector typed data as ``byte`` or ``sbyte``. |
| 106 | +You can also serialize ``Float32`` binary vector typed data as ``float``. The |
| 107 | +following example serializes ``Int8`` and ``Float32`` binary vector data: |
| 108 | + |
| 109 | +.. literalinclude:: /includes/fundamentals/code-examples/atlas-vector-search/VectorSearchExamples.cs |
| 110 | + :language: csharp |
| 111 | + :start-after: start-binary-int-float-serialize |
| 112 | + :end-before: end-binary-int-float-serialize |
| 113 | + |
| 114 | +You can deserialize ``PackedBit`` vector data to a :ref:`binary vector |
| 115 | +represented <csharp-binary-vector-representation>` ``byte`` data type only if the |
| 116 | +vector data has a padding value of ``0``. If the vector data has a padding value not |
| 117 | +equal to ``0``, you can deserialize it only to a ``BsonVectorPackedBit``. |
| 118 | + |
| 119 | +Vector Search Query Example |
| 120 | +--------------------------- |
| 121 | + |
| 122 | +You can perform a vector search query by calling the ``VectorSearch()`` method |
| 123 | +in an :ref:`aggregation pipeline <csharp-aggregation>`. To perform a vector |
| 124 | +search on a collection, you must first have a collection with a field that contains |
| 125 | +vector data and a vector search index that covers that field. |
| 126 | + |
| 127 | +.. tip:: |
| 128 | + |
| 129 | + To learn more about configuring a collection for vector search, see the :atlas:`{+vector-search+} |
| 130 | + </atlas-vector-search/vector-search-overview/>` guide in the MongoDB Atlas |
| 131 | + documentation. |
| 132 | + |
| 133 | +You can convert ``BinaryVectorFloat32``, ``BinaryVectorInt8``, and |
| 134 | +``BinaryVectorPackedBit`` data to the ``BsonBinaryData`` type to use in a vector |
| 135 | +search query by using the ``ToQueryVector()`` method. The following example |
| 136 | +converts ``BinaryVectorInt8`` into a ``BsonBinaryData`` object: |
| 137 | + |
| 138 | +.. literalinclude:: /includes/fundamentals/code-examples/atlas-vector-search/VectorSearchExamples.cs |
| 139 | + :language: csharp |
| 140 | + :start-after: start-to-query-vector |
| 141 | + :end-before: end-to-query-vector |
| 142 | + |
| 143 | +You can specify your :ref:`array-represented |
| 144 | +<csharp-vector-array-representation>` vector data as an instance of the |
| 145 | +``QueryVector`` class to use in a vector search query. The following example |
| 146 | +creates an array of ``ReadOnlyMemory<float>`` values as a ``QueryVector`` object |
| 147 | +to use in a vector search query: |
| 148 | + |
| 149 | +.. literalinclude:: /includes/fundamentals/code-examples/atlas-vector-search/VectorSearchExamples.cs |
| 150 | + :language: csharp |
| 151 | + :start-after: start-array-query-vector |
| 152 | + :end-before: end-array-query-vector |
| 153 | + |
| 154 | +Example |
| 155 | +~~~~~~~ |
| 156 | + |
| 157 | +This example performs the following steps to run an {+vector-search+} query on a collection that |
| 158 | +contains vector data and a vector search index on the ``PlotEmbedding`` field: |
| 159 | + |
| 160 | +1. Creates an array that contains the :ref:`array-represented |
| 161 | + <csharp-vector-array-representation>` vector data to search for |
| 162 | +#. Specifies a ``VectorSearchOptions`` object that contains the name of the index |
| 163 | + and the number of nearest neighbors to use during the search |
| 164 | +#. Creates an aggregation pipeline that uses the ``VectorSearch()`` stage to |
| 165 | + perform the vector search query and a ``Project()`` stage to filter the |
| 166 | + results |
| 167 | +#. Prints the results of the query |
| 168 | + |
| 169 | +.. literalinclude:: /includes/fundamentals/code-examples/atlas-vector-search/VectorSearchQueryExample.cs |
| 170 | + :language: csharp |
| 171 | + :start-after: start-search-example |
| 172 | + :end-before: end-search-example |
| 173 | + :emphasize-lines: 11 |
| 174 | + |
| 175 | +Additional Information |
| 176 | +---------------------- |
| 177 | + |
| 178 | +To learn more about {+vector-search+}, see the :atlas:`{+vector-search+} |
| 179 | +</atlas-vector-search/vector-search-overview/>` guide in the MongoDB Atlas |
| 180 | +documentation. |
| 181 | + |
| 182 | +API Documentation |
| 183 | +~~~~~~~~~~~~~~~~~ |
| 184 | + |
| 185 | +To learn more about any of the functions or types discussed in this |
| 186 | +guide, see the following API Documentation: |
| 187 | + |
| 188 | +- `BinaryVectorFloat32 <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.BinaryVectorFloat32.html>`__ |
| 189 | +- `BinaryVectorInt8 <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.BinaryVectorInt8.html>`__ |
| 190 | +- `BinaryVectorFloat32 <{+new-api-root+}/MongoDB.Bson/MongoDB.Bson.BinaryVectorPackedBit.html>`__ |
| 191 | +- `ToQueryVector() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.BinaryVectorDriverExtensions.ToQueryVector.html>`__ |
| 192 | +- `VectorSearch() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.AggregateFluentBase-1.VectorSearch.html>`__ |
| 193 | +- `Aggregate() <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.IMongoCollectionExtensions.Aggregate.html>`__ |
0 commit comments