Skip to main content

馃摌 Scoring

You might have noticed that most of the operators have a score property, which we haven't really talked about so far.

Before we jump into scoring, it is important to understand what the document score is. 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.

Some factors that can influence the score include:

  • 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.

To see the scores, you will need to project a new metadata field, using $meta. To see the document scores in a simple phrase search for Alice in Wonderland, try the following aggregation pipeline.

You can run this in the aggregation builders from the Atlas UI or in Compass.

[
{
$search: {
index: "fulltextsearch",
phrase: {
query: "Alice in Wonderland",
path: "title",
},
},
},
{
$project: {
title: 1,
authors: 1,
pages: 1,
year: 1,
score: {
$meta: "searchScore",
},
},
},
]

Your first result should look like this:

{
"_id": "0831702877",
"title": "Alice in Wonderland",
"authors": [
{
"_id": "64cc2db4830ba29148da7bbc",
"name": "Walt Disney Productions"
},
{
"_id": "64cc2db4830ba29148da8db5",
"name": "Mouse Works"
}
],
"pages": 95,
"year": 1997,
"score": 10.441896438598633
}

It got scored much higher than the next one because it's an exact match.