You can customize the score of the documents in the results. By adjusting how scores are calculated, you can ensure that the most pertinent documents are ranked higher in the search results. To learn more about the different ways in which you can customize the score, see Score the Documents in the Results. This page demonstrates how to:
Modify the score of the documents in the results to boost or bury the results.
Normalize your
$searchquery score in the range from0to1in the subsequent stages of your aggregation pipeline.
Modify the Score of the Documents in the Results
A MongoDB Search query assigns every returned document a score based on its relevance. The documents included in a result set return in order from highest to lowest score. To learn more, see Score the Documents in the Results.
You can use the following options with all operators to modify the default scoring behavior. For details and examples, click any of the following options:
This section demonstrates how to add weights to your search fields to boost or bury the documents in the results or a category of results. Specifically, it demonstrates how to assign one or more values to a field to return results with an increased or decreased score.
Example Index
You can set up an index with dynamic mappings enabled to index all the fields in the collection. Alternatively, use static mappings on the fields that you want to query and sort the results by. To learn more about creating MongoDB Search indexes, see Manage MongoDB Search Indexes.
Example Queries
The sample queries demonstrate how to boost or bury the documents in the results. They use the compound operator to combine two or more operators into a single query.
Normalize the Score
You can normalize your $search query score in the range from 0 to 1 in the subsequent stages of your aggregation pipeline. You can use the following stages after your $search stage in the following order to normalize the score:
- { "$addFields": { "score": { "$meta": "searchScore" } }}
- { "$setWindowFields": { "output": { "maxScore": { "$max": "$score" } } }}
- { "$addFields": { "normalizedScore": { "$divide": [ "$score", "$maxScore" ] } }}
db.movies.aggregate([{ "$search": { "text": { "query": "Helsinki", "path": "plot" } } }, { "$limit": 5 }, { "$project": { "_id": 0, "title": 1, "score": 1, "maxScore": 1, "normalizedScore": 1 } }, { "$addFields": { "score": { "$meta": "searchScore" } } }, { "$setWindowFields": { "output": { "maxScore": { "$max": "$score" } } } }, { "$addFields": { "normalizedScore": { "$divide": [ "$score", "$maxScore" ] } }}])[ { title: 'Drifting Clouds', score: 4.5660295486450195, maxScore: 4.5660295486450195, normalizedScore: 1 }, { title: 'Sairaan kaunis maailma', score: 4.041563034057617, maxScore: 4.5660295486450195, normalizedScore: 0.8851372929150143 }, { title: 'Bad Luck Love', score: 3.6251673698425293, maxScore: 4.5660295486450195, normalizedScore: 0.79394303764817 }, { title: 'Bad Luck Love', score: 3.6251673698425293, maxScore: 4.5660295486450195, normalizedScore: 0.79394303764817 }, { title: 'Forbidden Fruit', score: 3.6251673698425293, maxScore: 4.5660295486450195, normalizedScore: 0.79394303764817 }]db.movies.aggregate([{ "$search": { "text": { "path": "title", "query": "men", "score": { "function":{ "multiply":[ { "path": { "value": "imdb.rating", "undefined": 2 } }, { "score": "relevance" } ] } } } } }, { "$limit": 5 }, { "$addFields": { "score": { "$meta": "searchScore" } } }, { "$setWindowFields": { "output": { "maxScore": { "$max": "$score" } } } }, { "$addFields": { "normalizedScore": { "$divide": [ "$score", "$maxScore" ] } } }, { "$project": { "_id": 0, "title": 1, "score": 1, "maxScore": 1, "normalizedScore": 1 }}])[ { title: 'Men...', score: 23.431293487548828, maxScore: 23.431293487548828, normalizedScore: 1 }, { title: '12 Angry Men', score: 22.080968856811523, maxScore: 23.431293487548828, normalizedScore: 0.9423708882544255 }, { title: 'X-Men', score: 21.34803581237793, maxScore: 23.431293487548828, normalizedScore: 0.911090795039637 }, { title: 'X-Men', score: 21.34803581237793, maxScore: 23.431293487548828, normalizedScore: 0.911090795039637 }, { title: 'Matchstick Men', score: 21.05954933166504, maxScore: 23.431293487548828, normalizedScore: 0.8987787781692841 }]db.movies.aggregate([{ "$search": { "text": { "path": "title", "query": "shop", "score": { "function":{ "gauss": { "path": { "value": "imdb.rating", "undefined": 4.6 }, "origin": 9.5, "scale": 5, "offset": 0, "decay": 0.5 } } } } } }, { "$limit": 5 }, { "$addFields": { "score": { "$meta": "searchScore" } } }, { "$setWindowFields": { "output": { "maxScore": { "$max": "$score" } } } }, { "$addFields": { "normalizedScore": { "$divide": [ "$score", "$maxScore" ] } } }, { "$project": { "_id": 0, "title": 1, "score": 1, "maxScore": 1, "normalizedScore": 1 }}])[ { title: 'The Shop Around the Corner', score: 0.9471074342727661, maxScore: 0.9471074342727661, normalizedScore: 1 }, { title: 'Exit Through the Gift Shop', score: 0.9471074342727661, maxScore: 0.9471074342727661, normalizedScore: 1 }, { title: 'The Shop on Main Street', score: 0.9395227432250977, maxScore: 0.9471074342727661, normalizedScore: 0.9919917310611205 }, { title: 'Chop Shop', score: 0.8849083781242371, maxScore: 0.9471074342727661, normalizedScore: 0.9343273488331464 }, { title: 'Little Shop of Horrors', score: 0.8290896415710449, maxScore: 0.9471074342727661, normalizedScore: 0.8753913353110349 }]db.movies.aggregate([{ "$search": { "text": { "path": "title", "query": "men", "score": { "function":{ "path": { "value": "imdb.rating", "undefined": 4.6 } } } } } }, { "$limit": 5 }, { "$addFields": { "score": { "$meta": "searchScore" } } }, { "$setWindowFields": { "output": { "maxScore": { "$max": "$score" } } } }, { "$addFields": { "normalizedScore": { "$divide": [ "$score", "$maxScore" ] } } }, { "$project": { "_id": 0, "title": 1, "score": 1, "maxScore": 1, "normalizedScore": 1 }}])[ { title: '12 Angry Men', score: 8.899999618530273, maxScore: 8.899999618530273, normalizedScore: 1 }, { title: 'The Men Who Built America', score: 8.600000381469727, maxScore: 8.899999618530273, normalizedScore: 0.9662922191102197 }, { title: 'No Country for Old Men', score: 8.100000381469727, maxScore: 8.899999618530273, normalizedScore: 0.9101124414213563 }, { title: 'X-Men: Days of Future Past', score: 8.100000381469727, maxScore: 8.899999618530273, normalizedScore: 0.9101124414213563 }, { title: 'The Best of Men', score: 8.100000381469727, maxScore: 8.899999618530273, normalizedScore: 0.9101124414213563 }]db.movies.aggregate([{ "$search": { "text": { "path": "title", "query": "men", "score": { "function": { "log": { "path": { "value": "imdb.rating", "undefined": 10 } } } } } } }, { "$limit": 5 }, { "$addFields": { "score": { "$meta": "searchScore" } } }, { "$setWindowFields": { "output": { "maxScore": { "$max": "$score" } } } }, { "$addFields": { "normalizedScore": { "$divide": [ "$score", "$maxScore" ] } } }, { "$project": { "_id": 0, "title": 1, "score": 1, "maxScore": 1, "normalizedScore": 1 } }])[ { title: '12 Angry Men', score: 0.9493899941444397, maxScore: 0.9493899941444397, normalizedScore: 1 }, { title: 'The Men Who Built America', score: 0.9344984292984009, maxScore: 0.9493899941444397, normalizedScore: 0.9843145968064908 }, { title: 'No Country for Old Men', score: 0.9084849953651428, maxScore: 0.9493899941444397, normalizedScore: 0.9569144408182233 }, { title: 'X-Men: Days of Future Past', score: 0.9084849953651428, maxScore: 0.9493899941444397, normalizedScore: 0.9569144408182233 }, { title: 'The Best of Men', score: 0.9084849953651428, maxScore: 0.9493899941444397, normalizedScore: 0.9569144408182233 }]The MongoDB Search results contain the following scores:
The modified score for the
$searchquery in thescorefield from the$addFieldsstage.The maximum score assigned to the documents in the results in the
maxScorefield from the$setWindowFieldsstage.The normalized score in the
normalizedScorefield from the$addFieldsstage. MongoDB computes this score by dividing the modified score in$scoreby the maximum score in$maxScoreusing$divide.
Continue Learning
To learn more about compound queries using MongoDB Search, take Unit 9 of the Intro To MongoDB Course on MongoDB University. The 1.5-hour unit includes an overview of MongoDB Search. The unit also covers creating MongoDB Search indexes, running $search queries using compound operators, and grouping results using facet (MongoDB Search Operator).