A similarity metric is the rule that scores how closely a query vector matches a document vector embedding. Configure it in a vector search index so semantic retrieval can rank candidate documents. section 12.2.3 section 12.3.1
Choose the metric according to what closeness means for the application:
- Cosine similarity measures the angle between vectors and suits text analysis and NLP tasks such as finding similar sentences or documents. section 12.2.3
- Euclidean distance measures straight-line distance and suits image recognition or clustering where spatial separation matters. section 12.2.3
- Dot product sums corresponding element products and is often used for recommendations based on user preferences or behavior. section 12.2.3
- Keep the configured metric aligned with the vector index: the query uses that index when comparing its queryVector with stored embeddings. section 12.3
Configure cosine similarity for movie plots
Define a vectorSearch index for the sample_mflix database and embedded_movies collection, map plot_embedding as a 1,536-dimensional vector, and set similarity to cosine. A query for “village life” can then return ranked movies with vectorSearchScore metadata; higher scores indicate closer matches, with the shown example ranging from 0.9523 to 0.9453. section 12.2.3 section 12.3.1
{
"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"
}
]
}
Inspect a complete Atlas Vector Search index definition showing the vector field, dimensions, cosine metric, and filter fields. section 12.2.3
use sample_mflix
db.embedded_movies.createSearchIndex({
"name": "vectorSearchIndex",
"mappings": {
"dynamic": true,
"fields": {
"plot_embedding": {
"type": "knnVector",
"dimensions": 1536,
"similarity": "euclidean"
}
}
}
})
Compare a local Atlas configuration that uses a 1,536-dimensional knnVector with Euclidean similarity. section 13.5.2









