vector field type and vectorSearch operator are available as Preview features. The feature and the corresponding documentation might change at any time during the Preview period. To learn more, see Preview Features.Definition
The vectorSearch operator performs an ANN or ENN search on
vector embeddings in the specified field. Use this operator to add
analyzed text capabilities such a fuzzy search, phrase matching,
location filtering, wildcard pattern matching, and so on alongside
semantic search.
Syntax
vectorSearch has the following syntax:
{ "$search": { "index": "<index name>", // optional, defaults to "default" "vectorSearch": { "exact": true | false, "filter": {<operator-specification>}, "limit": <number-of-results>, "numCandidates": <number-of-candidates>, "path": "<field-to-search>", "queryVector": [<array-of-numbers>], "score": {<options>} } } }
Options
vectorSearch uses the following fields to construct a query:
Field | Type | Necessity | Description |
|---|---|---|---|
| Boolean | Optional | This is required if Flag that specifies whether to run ENN or ANN search. Value can be one of the following:
If omitted, defaults to MongoDB Vector Search supports ANN search on clusters running MongoDB v6.0.11, v7.0.2, or later and ENN search on clusters running MongoDB v6.0.16, v7.0.10, v7.3.2, or later. |
| Object | Optional | MongoDB Search operator to use to pre-filter documents based on metadata or specific search criteria. To learn more, see Pre-Filter. |
| Int | Required | |
| Int | Optional | This field is required if Number of nearest neighbors to use during the search. Value must
be less than or equal to ( We recommend that you specify a number at least 20 times higher than the
number of documents to return ( This overrequest pattern is the recommended way to trade off latency and recall in your ANN searches, and we recommend tuning this parameter based on your specific dataset size and query requirements. |
| String | Required | Indexed vector type field to search. |
| Array of Integers or Floats | Required | Array of numbers of To learn more about generating BSON The array size must match the number of vector dimensions
( You must embed your query with the same model that you used to embed the data. You can query your embeddings with full-fidelity vectors,
as long as the vector subtype is the same. This is only possible with
|
| Object | Optional | Score assigned to matching search term results. Use one of the following options to modify the score:
For information on using |
Behavior
MongoDB Search Index
You must index the fields to search using the vectorSearch operator.
You can index the following types of fields in the MongoDB Search index definition:
Field that contains vector embeddings. This is the field you specify in the query
pathoption.Fields to pre-filter the documents. These are the fields you specify in the query
filteroption.
Pre-Filter
You can pre-filter the documents to narrow the scope of your semantic
search and ensure that not all vectors are considered for comparison.
You can use any supported MongoDB Search operator in the filter field to
query and filter the documents.
You must index the fields that you want to filter your data in the
index definition for the vector type.
Score
You can include the score of each document in your search results.
Specify the $meta
expression with the searchScore value in the $project
stage. You can also specify the searchScoreDetails value for the
scoreDetails field $meta
expression for a detailed breakdown of the score.
To learn more, see Score the Documents in the Results and Return the Score Details.
vectorSearch Operator Limitations
The vectorSearch operator must be the top-level operator in your
queries. Therefore, you can't use the vectorSearch operator
inside the following MongoDB Search operators:
You can't use the vectorSearch operator to query fields indexed
using a vectorSearch type index.
You can't use the following $search options with the
vectorSearch operator:
tracking (deprecated)
You can't run $search queries with the vectorSearch
operator using the Search Tester.
Examples
The following examples use the sample_mflix.embedded_movies
collection from the sample data. If you load the sample data and create the sample
index on the collection, you can run the following ANN and ENN queries
against the collection after replacing the <connection-string> and
<index-name> placeholders in the queries.