Navigation

Scoring

On this page

Every document returned by a Full Text Search query is assigned a score based on relevance, and the documents included in a result set are returned in order from highest score to lowest.

Behavior

Many factors can influence a document’s score, including:

  • The position of the search term in the document,
  • The frequency of occurrence of the search term in the document,
  • The type of operator the query uses,
  • The type of analyzer the query uses.

The score assigned to a returned document is part of the document’s metadata. You can include each returned document’s score along with the result set by using a $project stage in your aggregation pipeline. The following example query uses a $project stage to add a field named score to the returned documents:

db.movies.aggregate([
  {
    $searchBeta: {
      "search": {
        "query": "Helsinki",
        "path": "plot"
      }
    }
  },
  {
    $project: {
      plot: 1,
      title: 1,
      score: { $meta: "searchScore" }
    }
  }
])

More information about the Lucene scoring algorithm can be found in the Lucene documentation.

Score Modifiers

Two score modifying options are available to the search and term operators.

boost

The boost option multiplies a result’s base score by a given number. In the following example, the search operator uses score with the boost option to multiply the score results by 3.

db.movies.aggregate([
  {
    $searchBeta: {
      "search": {
        "query": "Helsinki",
        "path": "title",
        "score": { "boost": { "value": 3 } }
      }
    }
  },
  {
    $project: {
      "_id": 0,
      "title": 1,
      "score": { "$meta": "searchScore" }
    }
  }
])

The above query returns the following results, in which the score is multiplied by 3 from its base value:

{ "title" : "Kites Over Helsinki", "score" : 12.250713348388672 }
{ "title" : "Helsinki-Naples All Night Long", "score" : 9.572236061096191 }

constant

The constant option replaces the base score with a given number. In the following example, the term operator uses score with the constant option to replace all score results with 5.

db.movies.aggregate([
  {
    $searchBeta: {
      "term": {
        "query": "tower",
        "path": "title",
        "score": { "constant": { "value": 5 } }
      }
    }
  },
  {
    $project: {
      "_id": 0,
      "title": 1,
      "score": { "$meta": "searchScore" }
    }
  }
])

The above query returns the following results, in which the score is replaced with the specified constant value:

{ "title" : "Tower Heist", "score" : 5 }
{ "title" : "Tokyo Tower", "score" : 5 }
{ "title" : "The Leaning Tower", "score" : 5 }
{ "title" : "Ivory Tower", "score" : 5 }

Note

The boost and constant options may not be used together.