Docs Menu

Docs HomeLaunch & Manage MongoDBMongoDB Atlas

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

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

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

    {
    "$addFields": {
    "normalizedScore": {
    "$divide": [
    "$score", "$maxScore"
    ]
    }
    }
    }

The following examples demonstrate how to normalize the score for some of the example queries against the sample_mflix.movies collection using the $addFields and $setWindowFields stages after the $search stage. To learn more about the queries, see function option examples.

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.

← Return the Score Details