liveBook · Manning

Relevance

As explained in MongoDB 8.0 in Action, Third Edition

A relevance score is a numeric value representing how closely a search result matches the search criteria.

A relevance score is a number indicating how closely a search result matches a query. In Atlas Search, it helps rank matching documents: higher scores indicate closer matches. Scores are calculated dynamically and have no fixed upper limit, so read them in the context of the query rather than as percentages. section 11.5.1 section 11.8

Lucene assigns scores after matching searchable tokens. A query term appearing more often in a document can raise its score, while a term common across the collection contributes less. Token proximity and indexing settings can also affect the ranking. Boosting or decaying scores can tailor relevance to a search domain. section 11.2

To use scores in an Atlas Search aggregation:

  • Start with a $search stage targeting a search index. A compound query can require several text matches, such as a business name, inspection result, and sector. section 11.5.1
  • Add the score to each returned document with a subsequent stage that sets `score: { $meta: "searchScore" }`. section 11.5.1
  • Sort by `score: -1` and limit the output when an application needs the highest-scoring matches first. section 11.5.1 section 13.5.1

Compare matches, not percentages

A search requiring “food” in `business_name`, “PASS” in `result`, and “127” in `sector` returns a matching inspection with a score of about 2.94. That number describes its relevance to those search criteria; it is not a score out of a fixed maximum. section 11.5.1

Listing 13.2 Executing a full-text search locally
db.inspections.aggregate([
  {
    $search: { #1
      index: 'LocalSearchIndex', #2
      text: {
        query: 'No Violation Issued', #3
        path: ['result', 'business_name'], #4
        fuzzy: {
          maxEdits: 2 #5
        }
      }
    }
  },
  {
    $match: { #6
      sector: 'Cigarette Retail Dealer - 127', #7
      'address.city': 'RIDGEWOOD' #8
    }
  },
  {
    $addFields: { #9
      score: { $meta: "searchScore" }
    }
  },
  {
    $sort: { #10
      score: -1
    }
  },
  {
    $limit: 3 #11
  },
  {
    $project: { #12
      _id: 0, #13
      business_name: 1,
      certificate_number: 1,
      date: 1,
      result: 1,
      'address.street': 1,
      'address.number': 1,
      'address.zip': 1,
      score: 1 #14
    }
  }
])

This local Atlas Search pipeline combines fuzzy text matching with filters, exposes `searchScore`, sorts descending, and returns the top three results. section 13.5.1

For a quick view of an entire result set, aggregate the scores into minimum, maximum, and average values. One filtered inspection search reports 13 matches with scores ranging from about 3.27 to 7.45. These statistics summarize that query’s matches without listing every document. section 11.5.2

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains scoring factors and demonstrates score extraction, sorting, and summary statistics without deriving Lucene’s scoring formula.
Examples
Uses inspection searches with required text matches and a local fuzzy search that returns the three highest-scoring results.
What it assumes
Assumes a searchable MongoDB collection and index while showing connection, database selection, and local index-creation steps.
What you can do after
Apply it via Atlas Search to display scores and order matching documents by relevance.
How it connects
Connects relevance to inverted indexes, tokenization, compound queries, fuzzy search, and aggregation pipelines.

Also explained in

Compare how 11 books explain relevance →

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.