Docs Menu
Docs Home
/
Atlas
/ /

How to Customize the Score of the Documents in the Results

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 and how to normalize your $search query score in the range from 0 to 1 in the subsequent stages of your aggregation pipeline. .

Every document that an Atlas Search query returns is assigned 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 a higher or lower score.

You can set up an index with dynamic mappings enabled to index all the fields in the collection or with static mappings on the fields you want to query and sort the results by. To learn more about creating Atlas Search indexes, see Create an Atlas Search Index.

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.


Use the Select your language drop-down menu to set the client you want to use try the examples in this section.


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

    {
    "$addFields": {
    "score": {
    "$meta": "searchScore"
    }
    }
    }
  • $setWindowFields

    {
    "$setWindowFields": {
    "output": {
    "maxScore": {
    "$max": "$score"
    }
    }
    }
    }
  • $addFields

    {
    "$addFields": {
    "normalizedScore": {
    "$divide": [
    "$score", "$maxScore"
    ]
    }
    }
    }
1db.movies.aggregate([{
2 "$search": {
3 "text": {
4 "query": "Helsinki",
5 "path": "plot"
6 }
7 }
8 },
9 {
10 "$limit": 5
11 },
12 {
13 "$project": {
14 "_id": 0,
15 "title": 1,
16 "score": 1,
17 "maxScore": 1,
18 "normalizedScore": 1
19 }
20 },
21 {
22 "$addFields": {
23 "score": {
24 "$meta": "searchScore"
25 }
26 }
27 },
28 {
29 "$setWindowFields": {
30 "output": {
31 "maxScore": {
32 "$max": "$score"
33 }
34 }
35 }
36 },
37 {
38 "$addFields": {
39 "normalizedScore": {
40 "$divide": [
41 "$score", "$maxScore"
42 ]
43 }
44 }
45}])
1[
2 {
3 title: 'Drifting Clouds',
4 score: 4.5660295486450195,
5 maxScore: 4.5660295486450195,
6 normalizedScore: 1
7 },
8 {
9 title: 'Sairaan kaunis maailma',
10 score: 4.041563034057617,
11 maxScore: 4.5660295486450195,
12 normalizedScore: 0.8851372929150143
13 },
14 {
15 title: 'Bad Luck Love',
16 score: 3.6251673698425293,
17 maxScore: 4.5660295486450195,
18 normalizedScore: 0.79394303764817
19 },
20 {
21 title: 'Bad Luck Love',
22 score: 3.6251673698425293,
23 maxScore: 4.5660295486450195,
24 normalizedScore: 0.79394303764817
25 },
26 {
27 title: 'Forbidden Fruit',
28 score: 3.6251673698425293,
29 maxScore: 4.5660295486450195,
30 normalizedScore: 0.79394303764817
31 }
32]
1db.movies.aggregate([{
2 "$search": {
3 "text": {
4 "path": "title",
5 "query": "men",
6 "score": {
7 "function":{
8 "multiply":[
9 {
10 "path": {
11 "value": "imdb.rating",
12 "undefined": 2
13 }
14 },
15 {
16 "score": "relevance"
17 }
18 ]
19 }
20 }
21 }
22 }
23 },
24 {
25 "$limit": 5
26 },
27 {
28 "$addFields": {
29 "score": {
30 "$meta": "searchScore"
31 }
32 }
33 },
34 {
35 "$setWindowFields": {
36 "output": {
37 "maxScore": {
38 "$max": "$score"
39 }
40 }
41 }
42 },
43 {
44 "$addFields": {
45 "normalizedScore": {
46 "$divide": [
47 "$score", "$maxScore"
48 ]
49 }
50 }
51 },
52 {
53 "$project": {
54 "_id": 0,
55 "title": 1,
56 "score": 1,
57 "maxScore": 1,
58 "normalizedScore": 1
59 }
60}])
1[
2 {
3 title: 'Men...',
4 score: 23.431293487548828,
5 maxScore: 23.431293487548828,
6 normalizedScore: 1
7 },
8 {
9 title: '12 Angry Men',
10 score: 22.080968856811523,
11 maxScore: 23.431293487548828,
12 normalizedScore: 0.9423708882544255
13 },
14 {
15 title: 'X-Men',
16 score: 21.34803581237793,
17 maxScore: 23.431293487548828,
18 normalizedScore: 0.911090795039637
19 },
20 {
21 title: 'X-Men',
22 score: 21.34803581237793,
23 maxScore: 23.431293487548828,
24 normalizedScore: 0.911090795039637
25 },
26 {
27 title: 'Matchstick Men',
28 score: 21.05954933166504,
29 maxScore: 23.431293487548828,
30 normalizedScore: 0.8987787781692841
31 }
32]
1db.movies.aggregate([{
2 "$search": {
3 "text": {
4 "path": "title",
5 "query": "shop",
6 "score": {
7 "function":{
8 "gauss": {
9 "path": {
10 "value": "imdb.rating",
11 "undefined": 4.6
12 },
13 "origin": 9.5,
14 "scale": 5,
15 "offset": 0,
16 "decay": 0.5
17 }
18 }
19 }
20 }
21 }
22 },
23 {
24 "$limit": 5
25 },
26 {
27 "$addFields": {
28 "score": {
29 "$meta": "searchScore"
30 }
31 }
32 },
33 {
34 "$setWindowFields": {
35 "output": {
36 "maxScore": {
37 "$max": "$score"
38 }
39 }
40 }
41 },
42 {
43 "$addFields": {
44 "normalizedScore": {
45 "$divide": [
46 "$score", "$maxScore"
47 ]
48 }
49 }
50 },
51 {
52 "$project": {
53 "_id": 0,
54 "title": 1,
55 "score": 1,
56 "maxScore": 1,
57 "normalizedScore": 1
58 }
59}])
1[
2 {
3 title: 'The Shop Around the Corner',
4 score: 0.9471074342727661,
5 maxScore: 0.9471074342727661,
6 normalizedScore: 1
7 },
8 {
9 title: 'Exit Through the Gift Shop',
10 score: 0.9471074342727661,
11 maxScore: 0.9471074342727661,
12 normalizedScore: 1
13 },
14 {
15 title: 'The Shop on Main Street',
16 score: 0.9395227432250977,
17 maxScore: 0.9471074342727661,
18 normalizedScore: 0.9919917310611205
19 },
20 {
21 title: 'Chop Shop',
22 score: 0.8849083781242371,
23 maxScore: 0.9471074342727661,
24 normalizedScore: 0.9343273488331464
25 },
26 {
27 title: 'Little Shop of Horrors',
28 score: 0.8290896415710449,
29 maxScore: 0.9471074342727661,
30 normalizedScore: 0.8753913353110349
31 }
32]
1db.movies.aggregate([{
2 "$search": {
3 "text": {
4 "path": "title",
5 "query": "men",
6 "score": {
7 "function":{
8 "path": {
9 "value": "imdb.rating",
10 "undefined": 4.6
11 }
12 }
13 }
14 }
15 }
16 },
17 {
18 "$limit": 5
19 },
20 {
21 "$addFields": {
22 "score": {
23 "$meta": "searchScore"
24 }
25 }
26 },
27 {
28 "$setWindowFields": {
29 "output": {
30 "maxScore": {
31 "$max": "$score"
32 }
33 }
34 }
35 },
36 {
37 "$addFields": {
38 "normalizedScore": {
39 "$divide": [
40 "$score", "$maxScore"
41 ]
42 }
43 }
44 },
45 {
46 "$project": {
47 "_id": 0,
48 "title": 1,
49 "score": 1,
50 "maxScore": 1,
51 "normalizedScore": 1
52 }
53}])
1[
2 {
3 title: '12 Angry Men',
4 score: 8.899999618530273,
5 maxScore: 8.899999618530273,
6 normalizedScore: 1
7 },
8 {
9 title: 'The Men Who Built America',
10 score: 8.600000381469727,
11 maxScore: 8.899999618530273,
12 normalizedScore: 0.9662922191102197
13 },
14 {
15 title: 'No Country for Old Men',
16 score: 8.100000381469727,
17 maxScore: 8.899999618530273,
18 normalizedScore: 0.9101124414213563
19 },
20 {
21 title: 'X-Men: Days of Future Past',
22 score: 8.100000381469727,
23 maxScore: 8.899999618530273,
24 normalizedScore: 0.9101124414213563
25 },
26 {
27 title: 'The Best of Men',
28 score: 8.100000381469727,
29 maxScore: 8.899999618530273,
30 normalizedScore: 0.9101124414213563
31 }
32]
1db.movies.aggregate([{
2 "$search": {
3 "text": {
4 "path": "title",
5 "query": "men",
6 "score": {
7 "function": {
8 "log": {
9 "path": {
10 "value": "imdb.rating",
11 "undefined": 10
12 }
13 }
14 }
15 }
16 }
17 }
18 },
19 {
20 "$limit": 5
21 },
22 {
23 "$addFields": {
24 "score": {
25 "$meta": "searchScore"
26 }
27 }
28 },
29 {
30 "$setWindowFields": {
31 "output": {
32 "maxScore": {
33 "$max": "$score"
34 }
35 }
36 }
37 },
38 {
39 "$addFields": {
40 "normalizedScore": {
41 "$divide": [
42 "$score", "$maxScore"
43 ]
44 }
45 }
46 },
47 {
48 "$project": {
49 "_id": 0,
50 "title": 1,
51 "score": 1,
52 "maxScore": 1,
53 "normalizedScore": 1
54 }
55 }
56])
1[
2 {
3 title: '12 Angry Men',
4 score: 0.9493899941444397,
5 maxScore: 0.9493899941444397,
6 normalizedScore: 1
7 },
8 {
9 title: 'The Men Who Built America',
10 score: 0.9344984292984009,
11 maxScore: 0.9493899941444397,
12 normalizedScore: 0.9843145968064908
13 },
14 {
15 title: 'No Country for Old Men',
16 score: 0.9084849953651428,
17 maxScore: 0.9493899941444397,
18 normalizedScore: 0.9569144408182233
19 },
20 {
21 title: 'X-Men: Days of Future Past',
22 score: 0.9084849953651428,
23 maxScore: 0.9493899941444397,
24 normalizedScore: 0.9569144408182233
25 },
26 {
27 title: 'The Best of Men',
28 score: 0.9084849953651428,
29 maxScore: 0.9493899941444397,
30 normalizedScore: 0.9569144408182233
31 }
32]

The Atlas Search results contain the following scores:

  • The modified score for the $search query in the score field from the $addFields stage.

  • The maximum score assigned to the documents in the results in the maxScore field from the $setWindowFields stage.

  • The normalized score in the normalizedScore field from the $addFields stage, which is computed by dividing the modified score in $score by the maximum score in $maxScore using $divide.

To learn more about compound queries using Atlas Search, take Unit 9 of the Intro To MongoDB Course on MongoDB University. The 1.5 hour unit includes an overview of Atlas Search and lessons on creating Atlas Search indexes, running $search queries using compound operators, and grouping results using facet.

Back

Improve Accuracy