Database Manual / Reference / Query Language / Aggregation Stages

$score (aggregation)(聚合)

New in version 8.2.在版本8.2中新增。

Definition定义

$score
$score computes and returns a new score as metadata. It also optionally normalizes the input scores, by default to a range between zero and one.计算并返回新的分数作为元数据。它还可以选择将输入分数标准化,默认情况下为0到1之间的范围。

Syntax语法

The stage has the following syntax:该阶段具有以下语法:

{
$score: {
score: <expression>,
normalization: "none|sigmoid|minMaxScaler",
weight: <expression>
}
}

Fields字段

$score takes the following fields:采用以下字段:

Field字段Type类型Description描述
scoreExpression表达式Computes a new value from the input scores and stores the value in the $meta keyword searchScore. 根据输入分数计算一个新值,并将该值存储在$metasearchScore中。All other scores are stored in the $meta keyword searchScoreDetails. Returns an error for non-numeric inputs.所有其他分数都存储在$metasearchScoreDetails中。对于非数字输入返回错误。
normalizationString字符串

Optional. 可选。Normalizes the score to the range of 0 to 1. Value can be:将分数标准化为01的范围。值可以是:

  • none - Doesn't normalize.不正常。
  • sigmoid - Applies the following $sigmoid expression:应用以下$sigmoid表达式:

    1 / (1 + e^-x)

    Here, x is the score to normalize.这里,x是要归一化的分数。

    If omitted, defaults to none.如果省略,则默认为none

  • minMaxScaler - to apply the $minMaxScaler window function:要应用$minMaxScaler窗口函数:

     (s - min_s) / (max_s - min_s)

    Where:其中:

    • s is the score.是分数。
    • min_s is the minimum score.是最低分数。
    • max_s is the maximum score.是最高分数。
weightDouble双精度浮点数Optional. 可选。Number to multiply the score expression by after normalization.归一化后将score表达式乘以的数字。

Behavior行为

The output documents from a $score are the same as the input documents, but includes additional computed score as metadata.$score的输出文档与输入文档相同,但包含额外的计算得分作为元数据。

If you specify multiple $score stages in the pipeline, the last $score stage in the pipeline overrides the score metadata from previous $score stages.如果在管道中指定了多个$score阶段,管道中的最后一个$score步骤将覆盖之前$score阶段的分数元数据。

Example示例

Consider the following documents in a collection named articles:考虑名为articles的集合中的以下文档:

{ "_id" : ObjectId("512bc95fe835e68f199c8686"), "author" : "dave", "score" : 80, "views" : 100 },
{ "_id" : ObjectId("512bc962e835e68f199c8687"), "author" : "dave", "score" : 85, "views" : 521 }
{ "_id" : ObjectId("55f5a192d4bede9ac365b257"), "author" : "ahn", "score" : 60, "views" : 1000 }
{ "_id" : ObjectId("55f5a192d4bede9ac365b258"), "author" : "li", "score" : 55, "views" : 5000 }
{ "_id" : ObjectId("55f5a1d3d4bede9ac365b259"), "author" : "annT", "score" : 60, "views" : 50 }
{ "_id" : ObjectId("55f5a1d3d4bede9ac365b25a"), "author" : "li", "score" : 94, "views" : 999 }
{ "_id" : ObjectId("55f5a1d3d4bede9ac365b25b"), "author" : "ty", "score" : 95, "views" : 1000 }

The following query uses the:以下查询使用:

  • $match stage to filter the documents where the views field value is greater than or equal to 1000.阶段用于筛选views字段值大于或等于1000的文档。
  • $score stage to compute a new score that is normalized using $sigmoid expression and then multiplied by the specified weight, 1.计算一个新的分数,该分数使用$sigmoid表达式进行归一化,然后乘以指定的权重1
db.articles.aggregate([
{
$match: {
views: { $gte: 1000 }
}
},
{
$score: {
score: "$score",
normalization: "sigmoid",
weight: 1
}
},
{
"$project": {
"_id": 0,
"author": 1,
"views": 1,
"score": 1,
"calculatedScore": { $meta: "score" }
}
}
])
[
{ author: 'ahn', score: 60, views: 1000, calculatedScore: 1 },
{ author: 'li', score: 55, views: 5000, calculatedScore: 1 },
{ author: 'ty', score: 95, views: 1000, calculatedScore: 1 }
]