对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

自定义结果中文档的分数

您可以自定义结果中各个文档的 分数。通过调整分数的计算方式,您可以确保最相关的文档在搜索结果中排名更高。要了解有关自定义分数的不同方法,请参阅对结果中的文档进行评分。此页面演示如何:

  • 修改结果中文档的分数,以提升或隐藏结果。

  • 在聚合管道的后续阶段中,将 $search 查询分数标准化为 0 至 1 范围。

MongoDB Search 查询会根据每个返回文档的相关性为其分配分数。结果集中包含的文档会按从最高分到最低分的顺序返回。要了解更多信息,请参阅对结果中的文档进行评分。

您可以使用以下选项与所有操作符修改默认评分行为。有关详细信息和示例,请单击以下任一选项:

本节展示了如何为搜索字段添加权重,以提升或隐藏结果或结果类别中的文档。具体来说,它演示了如何将一个或多个值分配给字段,以返回得分增加或减少的结果。

您可以设置一个启用了 动态映射的索引,以为集合中的所有字段建立索引。或者,在要查询的字段上使用静态映射,并按此字段对结果进行排序。要了解有关创建 MongoDB Search 索引的更多信息,请参阅 管理 MongoDB Search 索引。

这些示例查询演示了如何在结果中提升或隐藏文档。它们使用复合操作符将两个或多个操作符组合成一个查询。

您可以在聚合管道的后续阶段将$search查询分数标准化为0到1的范围。 您可以在$search阶段之后按以下顺序使用以下阶段来标准化分数:

  • $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]

MongoDB搜索结果包含以下分数:

  • 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. MongoDB computes this score by dividing the modified score in $score by the maximum score in $maxScore using $divide.

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).