Vector Search Fundamentals / Perform a Vector Search

Code Summary: Configure a Vector Index

Create a Vector Search Index

To create a vector search index, use createSearchIndex() method, which expects the name, type, and definition of the index. In this example, we use the createSearchIndex() method to create an index named vectorPlotIndex, which is a vectorSearch index. The default number of dimensions for the voyage-3.5-lite embedding model is 1024. We have the option to use 256, 512, or 2048 dimensions with the voyage 3.5 models.

db.movies.createSearchIndex(
  "vectorPlotIndex",
  "vectorSearch",
  {
     "fields": [
        {
           "type": "vector",
           "path": "plot_embedding",
           "numDimensions": 1024,
           "similarity": "cosine"
        }
     ]
  }
);

Create a Vector Search Index with a Pre-filter Field

To create a vector search index, use createSearchIndex() method, which expects the name, type, and definition of the index. In this example, we use the type filter so that we can pre-filter on the year field when we use $vectorSearch.

db.movies.createSearchIndex(
  "vectorPlotIndex",
  "vectorSearch",
  {
     "fields": [
        {
           "type": "vector",
           "path": "plot_embedding",
           "numDimensions": 1024,
           "similarity": "cosine"
        },
        {
          "type": "filter",
          "path": "year"
        }
     ]
  }
);