For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

How to Combine Multiple $vectorSearch Queries

The MongoDB $rankFusion aggregation stage supports $vectorSearch inside the input pipeline. You can use $rankFusion to combine multiple $vectorSearch queries against the same collection in the same pipeline. $rankFusion first executes all input pipelines independently, and then de-duplicates and combines the input pipeline results into a final ranked results set. To run multiple $vectorSearch queries against multiple collections in a single pipeline, use $unionWith.

You can run the following types of $vectorSearch queries by using the $rankFusion pipeline:

  • Run multiple $vectorSearch queries for similar terms against the same field.

    This allows you to perform a comprehensive search of your dataset for similar terms in the same query.

  • Run multiple $vectorSearch queries for the same term against multiple fields.

    This allows you to search multiple fields in your dataset to determine which fields return the best results for the query.

  • Run multiple $vectorSearch queries for the same term against embeddings from different embedding models.

    This allows you to search embeddings from different embedding models to determine the semantic interpretation differences between the different models.

This tutorial demonstrates how to run different $vectorSearch queries against the collections in the sample_mflix database. You can use the vector embeddings in the embedded_movies collection to try the sample queries or use Automated Embedding to generate embeddings for the sample data.

Before you begin, ensure that your cluster meets the requirements described in the Prerequisites. In addition, you must have the following installed:

In this section, you create the MongoDB Vector Search index on the sample_mflix.embedded_movies namespace. The section demonstrates the index definitions that can be used for running various queries.

In this section, you reorder the results of the query by using the $rerank stage.