Docs Menu
Docs Home
/
MongoDB Atlas
/ / /

Score the Documents in the Results

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.

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.

Note

When you query values in arrays, Atlas Search doesn't alter the score of the matching results based on the number of values inside the array that matched the query. The score would be the same as a single match regardless of the number of matches inside an array.

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.

After the $search stage, in the $project stage, the score field takes the $meta expression, which requires the searchScore value. You can also specify the searchScoreDetails value for the scoreDetails field $meta expression for a detailed breakdown of the score.

After the $vectorSearch stage, in the $project stage, the score field takes the $meta expression, which requires the vectorSearchScore value to return the score of each document in your vector search results.

Example

The following query uses a $project stage to add a field named score to the returned documents:

After a $project stage, you don't need to include a descending $sort because Atlas Search returns the documents from highest score to lowest. However, if multiple documents in the results have identical scores, the ordering of the documents in the results is non-deterministic. If you want the results to have a determined order, we recommend that you include the sort option in your $search stage to sort the results by a unique field. If you don't specify a unique field, Atlas Search defaults to sorting the results arbitrarily when the results have an identical score. You can use the sort option to also return an ascending sort of the results by score. To learn more, see Sort Atlas Search Results and Sort by Score Examples.

Note

On separate search nodes, each node has its own copy of a document with a different internal ID that Lucene uses to sort the results when multiple documents have identical scores. If the internal ID of the document on a node that isn't processing the query has a greater pagination order than the pagination token, the mongot on the node that is processing the query might include it in the results if you sort and paginate the results. To mitigate this, use $match after $search to exclude the document by its _id.

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

← 3. Construct Query Path