Every document returned by a MongoDB Search or MongoDB 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.
Usage
To include the score of each document in your search results,
use the $project stage in your aggregation pipeline.
For the
$searchstage, thescorefield takes the $meta expression, which requires thesearchScorevalue. You can also specify thesearchScoreDetailsvalue for thescoreDetailsfield $meta expression for a detailed breakdown of the score.Note
If you deployed Search Nodes, consider the following:
Avoid sorting the results by
searchScorebecause 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 insearchScoredepending on which host the query is routed to.
To support pagination when sorting by
searchScoreon Search Nodes, upvote this request in the MongoDB Feedback Engine.For the
$vectorSearchstage, thescorefield takes the $meta expression, which requires thevectorSearchScorevalue 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:
1 db.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 ])
1 db.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.
1 db.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 MongoDB Vector Search Score.
Behavior
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.
To learn more about the Lucene scoring algorithm, see Lucene documentation.
Additional Options
In addition to the default scoring behavior, MongoDB Search supports the following options:
Modifying the score assigned to certain documents.
Returning a detailed breakdown of the score by using the score details option.
Normalizing the search score.
Considerations
If multiple documents in the results have identical scores,
the ordering of the documents in the results is non-deterministic.
If you want your MongoDB 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 MongoDB 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, MongoDB Search assigns the same score regardless of how many values in the array match the query.
Tutorials
For examples on how to use additional
| |
For examples on how to use the |