Docs Menu

Score the Documents in the Results

Every document returned by an Atlas Search or Atlas Vector 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.

To include the score of each document in your search results, use the $project stage in your aggregation pipeline.

  • For the $search 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.

    Note

    If you deployed Search Nodes, consider the following:

    • Avoid sorting the results by searchScore because it can be different across Search Nodes.

    • To calculate searchScore, a host takes into consideration all the documents that exist on it, including deleted documents that have not yet been removed from the index. Since the deletion occurs independently on each host, this might cause changes in searchScore depending on which host the query is routed to.

    To support pagination when sorting by searchScore on Search Nodes, upvote this request in the MongoDB Feedback Engine.

  • For the $vectorSearch 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:

1db.movies.aggregate([
2 {
3 "$search": {
4 "text": {
5 <operator-specification>
6 }
7 }
8 },
9 {
10 "$project": {
11 "<field-to-include>": 1,
12 "<field-to-exclude>": 0,
13 "score": { "$meta": "searchScore" }
14 }
15 }
16])
1db.movies.aggregate([
2 {
3 "$search": {
4 "text": {
5 <operator-specification>
6 },
7 "scoreDetails": true
8 }
9 },
10 {
11 "$project": {
12 "<field-to-include>": 1,
13 "<field-to-exclude>": 0,
14 "scoreDetails": { "$meta": "searchScoreDetails" }
15 }
16 }
17])

To learn more, see return the search score details.

1db.movies.aggregate([
2 {
3 "$vectorSearch": {
4 <query-syntax>
5 }
6 },
7 {
8 "$project": {
9 "<field-to-include>": 1,
10 "<field-to-exclude>": 0,
11 "score": { "$meta": "vectorSearchScore" }
12 }
13 }
14])

To learn more, see Atlas Vector Search Score.

The score assigned to a returned document is part of the document's metadata. You can use a $project stage in your aggregation pipeline to include each returned document's score along with the result set. Documents return from highest score to lowest. Many factors can influence a document's score, including the following:

  • Position of the search term in the document.

  • Frequency of occurrence of the search term in the document.

  • Type of operator or analyzer an Atlas Search query uses.

To learn more about the Lucene scoring algorithm, see Lucene documentation.

In addition to the default scoring behavior, Atlas Search supports the following options:

If multiple documents in the results have identical scores, the ordering of the documents in the results is non-deterministic. If you want your Atlas Search results to have a determined order, include the sort option in your $search stage to sort the results by a unique field. 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.

On separate Search Nodes, each node assigns documents different internal Lucene IDs used for sorting when scores are identical. When sorting and paginating results, the mongot process on the node that is processing the query might include documents from other nodes if their internal IDs have greater pagination order than the token. To prevent this, use $match after $search to exclude documents by their _id.

When querying array values, Atlas Search assigns the same score regardless of how many values in the array match the query.

For examples on how to use additional score options in your Atlas Search queries, see the following pages:

For examples on how to use the score field in some common Atlas Search queries, see the following pages: