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

为结果中的文档打分

MongoDB Search 或MongoDB Vector Search查询返回的每个文档都会根据相关性分配一个分数,并且结果设立包含的文档会按从最高分到最低分的顺序返回。

To include the score of each document in your search results, use the $project stage in your aggregation pipeline.

  • For the $search stage, the score field takes the $meta expression, which requires the searchScore value. You can also specify the searchScoreDetails value for the scoreDetails field $meta expression for a detailed breakdown of the score.

  • For the $vectorSearch stage, the score field takes the $meta expression, which requires the vectorSearchScore value to return the score of each document in your vector search results.

注意

您可以使用:

如果您在任何其他查询中使用 searchScorevectorSearchScore,MongoDB 会从 MongoDB v8.2 开始记录警告日志。

例子

The following query uses a $project stage to add a field named score to the returned documents:

1db.movies.aggregate([
2 {
3 "$search": {
4 "text": {
5 <operator-specification>
6 }
7 }
8 },
9 {
10 "$project": {
11 "<field-to-include>": 1,
12 "<field-to-exclude>": 0,
13 "score": { "$meta": "searchScore" }
14 }
15 }
16])
1db.movies.aggregate([
2 {
3 "$search": {
4 "text": {
5 <operator-specification>
6 },
7 "scoreDetails": true
8 }
9 },
10 {
11 "$project": {
12 "<field-to-include>": 1,
13 "<field-to-exclude>": 0,
14 "scoreDetails": { "$meta": "searchScoreDetails" }
15 }
16 }
17])

要了解更多信息,请参阅返回搜索分数详情。

1db.movies.aggregate([
2 {
3 "$vectorSearch": {
4 <query-syntax>
5 }
6 },
7 {
8 "$project": {
9 "<field-to-include>": 1,
10 "<field-to-exclude>": 0,
11 "score": { "$meta": "vectorSearchScore" }
12 }
13 }
14])

To learn more, see MongoDB Vector Search Scoring.

The score assigned to a returned document is part of the document's metadata. You can use a $project stage in your aggregation pipeline to include each returned document's score along with the result set. Documents return from highest score to lowest. Many factors can influence a document's score, including the following:

要学习;了解有关Lucene评分算法的更多信息,请参阅Lucene文档。

除了默认的评分行为外, MongoDB Search 还支持以下选项:

如果结果中的多个文档具有相同的分数,则结果中文档的顺序是不确定的。如果希望MongoDB Search 结果有确定的顺序,请在 阶段包含 排序 $search选项,以按唯一字段对结果进行排序。您还可以使用排序选项按分数返回对结果进行升序排序。要学习;了解更多信息,请参阅对MongoDB搜索结果进行排序按分数排序示例。

On separate Search Nodes, each node assigns documents different internal Lucene IDs used for sorting when scores are identical. When sorting and paginating results, the mongot process on the node that is processing the query might include documents from other nodes if their internal IDs have greater pagination order than the token. To prevent this, use $match after $search to exclude documents by their _id.

查询大量值时,如果大量中的更多值与查询匹配, MongoDB Search 会分配更高的分数。

有关如何在MongoDB搜索查询中使用附加 score 选项的示例,请参阅以下页面:

有关如何在一些常见MongoDB搜索查询中使用 score字段的示例,请参阅以下页面: