liveBook · Manning

Similarity metric

As explained in MongoDB 8.0 in Action, Third Edition

A similarity score is a numeric measure of how closely a query vector matches a document vector embedding.

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

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Defines cosine, Euclidean, and dot product behavior with application guidance, but does not develop mathematical derivations.
Examples
Uses movie-plot embeddings with cosine scoring and a local knnVector index using Euclidean similarity.
What it assumes
Assumes vector embeddings and their storage paths, while introducing metric selection through index definitions and query parameters.
What you can do after
Apply it via Atlas CLI and vector search pipelines by selecting a metric, defining an index, and inspecting returned similarity scores.
How it connects
Connects vector embeddings with vector search indexes, aggregation pipelines, approximate nearest neighbor search, and exact nearest neighbor search.

Also explained in

Compare how 10 books explain similarity metric →

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.