|
| 1 | +The ``$vectorSearch`` aggregation stage performs an *approximate nearest neighbor* search |
| 2 | +on a vector in the specified field. Your collection *must* have a |
| 3 | +defined Atlas Vector Search index before you can perform a vector search on your data. |
| 4 | + |
| 5 | +.. tip:: |
| 6 | + |
| 7 | + To obtain the sample dataset used in the following example, see :ref:`csharp-quickstart`. |
| 8 | + To create the sample Atlas Vector Search index used in the following example, see |
| 9 | + :atlas:`Create an Atlas Vector Search Index </atlas-vector-search/create-index>` in the |
| 10 | + Atlas manual. |
| 11 | + |
| 12 | +To create a ``$vectorSearch`` pipeline stage, call the ``VectorSearch()`` method on a |
| 13 | +``PipelineStageDefinitionBuilder`` object. The ``VectorSearch()`` method accepts the |
| 14 | +following parameters: |
| 15 | + |
| 16 | +.. list-table:: |
| 17 | + :header-rows: 1 |
| 18 | + :widths: 20 80 |
| 19 | + |
| 20 | + * - Parameter |
| 21 | + - Description |
| 22 | + |
| 23 | + * - ``field`` |
| 24 | + - The field to perform the vector search on. |
| 25 | + |
| 26 | + **Data type**: ``Expression<Func<TInput, TField>>`` |
| 27 | + |
| 28 | + * - ``queryVector`` |
| 29 | + - The encoded vector that will be matched with values from the database. |
| 30 | + Although the data type of this parameter is ``QueryVector``, you can also pass an |
| 31 | + array of ``float`` values. |
| 32 | + |
| 33 | + **Data type**: `QueryVector <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.QueryVector.html>`__ |
| 34 | + |
| 35 | + * - ``limit`` |
| 36 | + - The maximum number of documents to return. |
| 37 | + |
| 38 | + **Data type**: {+int-data-type+} |
| 39 | + |
| 40 | + * - ``options`` |
| 41 | + - Configuration options for the vector search operation. |
| 42 | + |
| 43 | + **Data type**: `VectorSearchOptions<TDocument> <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.VectorSearchOptions-1.html>`__ |
| 44 | + |
| 45 | +You can use the ``options`` parameter to configure your vector search operation. The |
| 46 | +``VectorSearchOptions`` class contains the following properties: |
| 47 | + |
| 48 | +.. list-table:: |
| 49 | + :header-rows: 1 |
| 50 | + :widths: 30 70 |
| 51 | + |
| 52 | + * - Property |
| 53 | + - Description |
| 54 | + |
| 55 | + * - ``Exact`` |
| 56 | + - Whether the vector search uses the exact nearest neighbor (ENN) algorithm. |
| 57 | + If this property is set to ``false``, the vector search uses the approximate nearest |
| 58 | + neighbor (ANN) algorithm. If this property is set to ``true``, the |
| 59 | + ``NumberOfCandidates`` property must be ``null``. |
| 60 | + |
| 61 | + | **Data type**: {+bool-data-type+} |
| 62 | + | **Default**: ``false`` |
| 63 | +
|
| 64 | + * - ``Filter`` |
| 65 | + - Additional search criteria that the found documents must match. |
| 66 | + |
| 67 | + | **Data Type:** `FilterDefinition<TDocument> <{+new-api-root+}/MongoDB.Driver/MongoDB.Driver.FilterDefinition-1.html>`__ |
| 68 | + | **Default**: ``null`` |
| 69 | + |
| 70 | + * - ``IndexName`` |
| 71 | + - The index to perform the vector search on. |
| 72 | + |
| 73 | + | **Data type**: {+string-data-type+} |
| 74 | + | **Default**: ``null`` |
| 75 | +
|
| 76 | + * - ``NumberOfCandidates`` |
| 77 | + - The number of neighbors to search in the index. |
| 78 | + |
| 79 | + | **Data type**: ``int?`` |
| 80 | + | **Default**: ``null`` |
| 81 | +
|
| 82 | +Consider the ``embedded_movies`` collection in the ``sample_mflix`` database. |
| 83 | +The following ``EmbeddedMovie`` class represents a document in this database: |
| 84 | + |
| 85 | +.. code-block:: csharp |
| 86 | +
|
| 87 | + public class EmbeddedMovie |
| 88 | + { |
| 89 | + [BsonElement("title")] |
| 90 | + public string Title { get; set; } |
| 91 | +
|
| 92 | + [BsonElement("plot_embedding")] |
| 93 | + public double[] Embedding { get; set; } |
| 94 | +
|
| 95 | + [BsonElement("score")] |
| 96 | + public double Score { get; set; } |
| 97 | + } |
| 98 | +
|
| 99 | +You can use a ``$vectorSearch`` stage to perform a semantic search on the ``plot_embedding`` |
| 100 | +field of the documents in the collection. |
| 101 | +The following example shows how to use |mechanism| to generate an aggregation pipeline to |
| 102 | +perform the following operations: |
| 103 | + |
| 104 | +- Perform a vector search on the Atlas Vector Search index of the ``plot_embedding`` |
| 105 | + field by using vector embeddings for the string ``"time travel"`` |
| 106 | +- Fetch the ``Title`` and ``Plot`` fields from documents found in the vector search |
0 commit comments