Scoring¶
Every document returned by an Atlas 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
in your aggregation
pipeline.
The following example query uses a $project
stage to add a
field named score
to the returned documents:
1 db.movies.aggregate([ 2 { 3 $search: { 4 "text": { 5 "query": "Helsinki", 6 "path": "plot" 7 } 8 } 9 }, 10 { 11 $project: { 12 plot: 1, 13 title: 1, 14 score: { $meta: "searchScore" } 15 } 16 } 17 ])
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 text
operator uses score
with the boost
option to multiply the score
results by 3
.
1 db.movies.aggregate([ 2 { 3 $search: { 4 "text": { 5 "query": "Helsinki", 6 "path": "title", 7 "score": { "boost": { "value": 3 } } 8 } 9 } 10 }, 11 { 12 $project: { 13 "_id": 0, 14 "title": 1, 15 "score": { "$meta": "searchScore" } 16 } 17 } 18 ])
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.247001647949219 } { "title" : "Helsinki-Naples All Night Long", "score" : 9.568056106567383 }
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
.
1 db.movies.aggregate([ 2 { 3 $search: { 4 "text": { 5 "query": "tower", 6 "path": "title", 7 "score": { "constant": { "value": 5 } } 8 } 9 } 10 }, 11 { 12 $project: { 13 "_id": 0, 14 "title": 1, 15 "score": { "$meta": "searchScore" } 16 } 17 } 18 ])
The above query returns the following results, in which the score is
replaced with the specified constant
value:
1 { "title": "Tower Heist", "score": 5 } 2 { "title": "Tokyo Tower", "score": 5 } 3 { "title": "The Leaning Tower", "score": 5 } 4 { "title" : "Tower Block", "score" : 5 } 5 { "title": "Ivory Tower", "score": 5 }
The boost
and constant
options may not be used together.