liveBook · Manning

Vector index

As explained in MongoDB 8.0 in Action, Third Edition

A vector index is a specialized index structure that organizes vector embeddings for efficient similarity search.

A vector index organizes stored embeddings so a query vector can quickly retrieve similar documents. In Atlas Vector Search, the index is separate from the MongoDB collection it searches. It supports semantic search and recommendation workloads without comparing every result by hand. section 12.8 section 12.2.1

To define and use an Atlas vector index:

  • Store embeddings in a collection, then define a vectorSearch index with the database, collection, embedding field path, vector dimensions, and similarity metric. The configured dimensions must describe the stored vectors; Atlas supports up to 4,096. section 12.2.3 section 12.2.1
  • Choose a metric for the application: cosine similarity for text comparisons, Euclidean distance where spatial distance matters, or dot product for preference-based recommendations. section 12.2.3
  • Create the index from its JSON definition with the Atlas CLI and check that it is ready before querying. A $vectorSearch aggregation stage then names the index, embedding path, and query vector. section 12.2.4 section 12.3
{
  "name": "MongoDB-in-Action-VectorSearchIndex",
  "type": "vectorSearch",
  "collectionName": "embedded_movies",
  "database": "sample_mflix",
  "fields": [
    {
      "type": "vector",
      "path": "plot_embedding",
      "numDimensions": 1536,
      "similarity": "cosine"
    },
   {
      "type": "filter",
      "path": "genres"
    },
   {
      "type": "filter",
      "path": "languages"
    },
   {
      "type": "filter",
      "path": "year"
    }
  ]
}

This movie-search definition indexes 1,536-dimensional plot_embedding vectors with cosine similarity. The genres, languages, and year fields are separately declared as filter fields so queries can narrow results before vector search. section 12.2.3 section 12.3.2

atlas cluster search index create \
--clusterName MongoDB-in-Action \
--file vector-definition.json

Pass the saved definition to the Atlas CLI to create the cluster’s search index; afterward, list its status and wait until it is ready to query. section 12.2.4

Query choices affect the speed–precision trade-off. Approximate nearest-neighbor search (ANN) is the default; exact nearest-neighbor search (ENN) can be more precise but slower. For ANN, increasing numCandidates may improve precision at additional processing cost. Prefilters require fields declared with type filter in the index definition. section 12.3 section 12.3.2

Keep dimensions only as high as accuracy needs justify, and avoid indexing or reindexing during searches. If the embedding model changes, create a new index for the new vectors. Sufficient memory and a warmed filesystem cache also help avoid search latency; exclude large vector fields from returned results when they are unnecessary. section 12.7

Indexing chatbot embeddings

A chatbot setup indexes the embedding field in a langchain collection as 1,536-dimensional vectors using cosine similarity. Its local Atlas deployment verifies the resulting index with getSearchIndexes(), checking for READY and queryable status. section 14.5.3

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Works through index configuration, CLI creation, query use, and performance choices without implementing an indexing algorithm.
Examples
Uses a movie-plot embedding index with filter fields and a chatbot embedding index verified as ready.
What it assumes
Assumes embeddings are stored in a MongoDB collection while defining index settings and similarity metrics explicitly.
What you can do after
Operate/configure it with the Atlas CLI, then query the index through $vectorSearch.
How it connects
Connects vector embeddings and similarity metrics to approximate nearest-neighbor search, prefiltering, and MongoDB aggregation pipelines.

Also explained in

Compare how 8 books explain vector index →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.