Docs 菜单
Docs 主页
/ /

返回得分详情

您可以在 $search 阶段中使用 scoreDetails 布尔值选项,详细了解查询结果中每个文档的得分明细。

当您在$search阶段设立scoreDetails: true时, MongoDB 搜索 会返回每个匹配文档的详细评分信息。此信息解释了文档与查询匹配并在搜索结果中获得相关性分数的原因。

默认下,评分基于 bm25 公式:

  • 词语频率:搜索术语在文档中出现的频率

  • 反向文档频率:搜索术语在所有文档中的常见程度

  • 字段长度:与查询匹配的字段的长度

scoreDetails 选项对这些因素进行了分解,以帮助您分析文档与查询匹配并获得其分数的原因。

要查看元数据,必须在 阶段使用 $meta $project表达式。

{
"$search": {
"<operator>": {
<operator-specification>
},
"scoreDetails": true | false
}
},
{
"$project": {
"scoreDetails": {"$meta": "searchScoreDetails"}
}
}

在 $search 阶段scoreDetails布尔选项采用以下值之一:

  • true — 在结果中包含文档分数的详细信息。如果设立为 true, MongoDB 搜索 将返回结果中每个文档的分数明细。这提供了有关某些文档与MongoDB搜索查询匹配的原因的信息。要学习;了解更多信息,请参阅输出。

  • false - 排除结果的分数细分详细信息。 (默认)

如果省略,则scoreDetails选项默认为false

在 $project 阶段scoreDetails 字段采用 $meta 表达式,该表达式需要以下值:

searchScoreDetails

返回结果中每个文档的分数明细。

scoreDetails选项会在结果中每个文档的scoreDetails对象内的details数组中返回以下字段:

字段
类型
说明

value

float

评分公式的子集对分数的贡献。顶层 value 显示结果文档的全部分数,等于 $searchScore 的值。

评分公式根据查询中使用的操作符而有所不同。例如,MongoDB Search 使用距离衰减函数来计算 near 操作符的分数。

description

字符串

评分公式的子集,包括文件评分方式和计算分数时考虑的因素的详细信息。顶层的 description 显示了用于为文档评分的整个评分公式。

要学习;了解更多信息,请参阅影响分数的因素。

details

对象数组

基于评分公式的子集对文档中每个匹配项的分数进行细分。 该值是分数详细信息对象的数组,具有递归结构。

不同的查询操作符使用不同的算法来计算结果中每个文档的 searchScore。以下各节描述了常用查询操作符如何处理评分:

默认情况下,textphrasequeryStringautocomplete 操作符使用bm25 相似性算法对文档进行评分。

当您需要在多个查询中获得一致的结果时,我们建议使用 stableTflboolean 算法,尤其是在以下两个条件均为真的情况下:

  • 您的应用程序按 searchScore 对结果进行排序,并对结果进行分页,这依赖于确定性评分来防止重复或跳过文档。

  • 您的部署使用专用 MongoDB Search 节点或将读取偏好设置为 secondarynearest,这增加了初始和后续查询被路由到不同 MongoDB Search 节点的可能性。

bm25 后续查询之间的分数可能不一致。每个 MongoDB Search 节点都会构建 MongoDB Search 索引并独立执行更新和删除操作,从而生成的文档语料库可能因不同的 MongoDB Search 节点而异。由于 bm25 计算取决于文档语料库,因此路由到不同 MongoDB Search 节点的后续查询可能会为相同的文档计算出不同的 bm25 分数。

要使用其他相似性算法,请在 MongoDB Search 索引定义中为您索引为 MongoDB Search stringautocomplete 类型的字段指定 similarity.type 属性。要了解如何为这些类型配置 MongoDB Search 索引,请参阅如何索引 String 字段如何为自动完成字段编制索引

在 MongoDB Search 索引定义中指定 similarity.type 属性时,您可以从以下相似度算法中进行选择:

bm25 是一种流行的排名算法,它根据以下标准对文档进行排名:

  • 词语频率:搜索术语在文档中出现的频率

  • 反向文档频率:搜索术语在所有文档中的常见程度

  • 字段长度:与查询匹配的字段的长度

bm25 计算分数为 boost * idf * tf,其中每个因素定义如下:

因子
说明

boost

在查询时使用查询操作符的 score.boost 选项指定的因素。您可以将 boost 设置为正数或文档中数字字段的值。要了解详情,请参阅boost。

idf

查询的反向文档频率。MongoDB Search 使用以下公式计算频率:

idf = log(1 + (N - n + 0.5) / (n + 0.5))

其中:

  • N 是包含查询字段的文档总数。

  • n 是包含查询术语的文档数量。

tf

词语频率。MongoDB Search 使用以下公式计算频率:

tf = freq / (freq + k1 * (1 - b + b * dl / avgdl))

其中:

  • freq 是该词语在文档中出现的次数。

  • k1 是内部指定的词语饱和度参数。它会影响每次重复出现该词语时分数的增加幅度。

  • b 是内部指定的长度归一化参数。b 乘以 dlavgdl 的比率。如果 b 增加,则dlavgdl 之比的影响也会放大。

  • dl 是文档中字段的长度。

  • avgdl 是所有文档中字段的平均长度。

boolean 是一种评分算法,用于检查文档中是否存在每个查询术语,并统计找到了多少术语。所有匹配的术语都得到同等对待,不根据术语的重要性或频率进行调整。

对于boolean,得分计算为文档中所有存在的查询术语的总和,其中每个术语如果存在于文档中,则对得分贡献1

stableTfl 是一种自定义的 MongoDB Search 排名算法,它使用术语的长度来推导术语的稀有度。这基于 Zipf 定律,即较长的单词出现频率较低。

stableTfl 计算分数为 boost * tr * tf,其中每个因素定义如下:

因子
说明

boost

在查询时使用查询操作符的 score.boost 选项指定的因素。您可以将 boost 设置为正数或文档中数字字段的值。要了解详情,请参阅boost。

tf

递减函数。MongoDB Search 使用以下公式计算递减函数:

tf = freq / (freq + k1)

其中:

  • freq 是文档中查询词语的频率。

  • k1 是内部指定的词语饱和度参数。它会影响每次重复出现该词语时分数的增加幅度。

tr

术语稀有度。MongoDB Search 使用以下公式计算术语稀有度:

tr = log(1 + (1 - p + 0.05)/(p + 0.05))

其中:

  • p 是查询术语出现在文档中的概率(基于术语长度)。这基于 Zipf 定律,即较长的单词出现频率较低。为了确定术语的稀有度,MongoDB 使用基于术语长度的衰减函数。

p

基于 Zipf 定律的概率函数。MongoDB Search 使用以下公式计算查询词出现在文档中的概率:

p = 1 - (1 - m * 2 ^ (-c * tl)) ^ dl

其中:

  • m 是内部指定的术语概率的乘性常量。

  • c 是内部指定的衰减常量。它控制 p 随着术语长度增加的递减速度。

  • tl 是查询术语的长度,以 Unicode 代码点为单位进行测量。

  • dl 是文档 d 的长度,以词元为单位。

near 操作符使用距离衰减函数来对文档进行评分。它测量 MongoDB Search 结果与您设置为origin 值的数字、日期或的接近程度。

距离衰减函数计算出的分数为 pivot / (pivot + distance),其中每个因子的定义如下:

因子
说明

pivot

指定为参考点的值,如果fieldValueorigin之间的距离等于0.5 ,则分数等于该值。 这定义了随着fieldValueorigin之间的距离增加,分数衰减的速度。 对于fieldValueorigin之间的给定距离,如果pivot减小,则分数也会减小。

distance

fieldValueorigin 之间的绝对距离。MongoDB Search 使用以下公式计算该值:

abs(fieldValue - origin)

其中:

  • fieldValue 是您在文档中查询的字段的值。

  • origin 是要在附近搜索的数字、日期或地理位置点。

以下示例展示如何在以下结果中检索分数的详细信息:

提示

要以递归方式查看对象数组中分数的详细信息,请通过运行以下命令来配置mongosh中的设置:

config.set('inspectDepth', Infinity)

以下示例演示如何使用$search scoreDetails选项检索textnear复合embeddedDocument操作符查询结果中的文档的分数明细。

以下示例使用文本操作符查询 sample_mflix.movies 集合中 title 字段的术语 autumn。该查询在 $search 阶段指定 scoreDetails 选项,以检索结果中每个文档得分的明细。该查询使用 $limit 阶段将结果限制为三个文档,并使用 $project 阶段进行以下操作:

  • 排除_id字段。

  • 仅包含title字段。

  • score字段添加到结果中以返回文档的分数,并将scoreDetails字段添加到结果中以返回文档分数的明细。

1db.movies.aggregate([
2 {
3 "$search": {
4 "text": {
5 "path": "title",
6 "query": "autumn"
7 },
8 "scoreDetails": true
9 }
10 },
11 {
12 "$limit": 3
13 },
14 {
15 "$project": {
16 "_id": 0,
17 "title": 1,
18 "score": { "$meta": "searchScore" },
19 "scoreDetails": { "$meta": "searchScoreDetails" }
20 }
21 }
22])
1[
2 {
3 title: 'Autumn Leaves',
4 score: 3.834893226623535,
5 scoreDetails: {
6 value: 3.834893226623535,
7 description: '$type:string/title:autumn [BM25Similarity], result of:',
8 details: [
9 {
10 value: 3.834893226623535,
11 description: 'score(freq=1.0), computed as boost * idf * tf from:',
12 details: [
13 {
14 value: 7.39188289642334,
15 description: 'idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:',
16 details: [
17 {
18 value: 14,
19 description: 'n, number of documents containing term',
20 details: []
21 },
22 {
23 value: 23529,
24 description: 'N, total number of documents with field',
25 details: []
26 }
27 ]
28 },
29 {
30 value: 0.5187978744506836,
31 description: 'tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:',
32 details: [
33 {
34 value: 1,
35 description: 'freq, occurrences of term within document',
36 details: []
37 },
38 {
39 value: 1.2000000476837158,
40 description: 'k1, term saturation parameter',
41 details: []
42 },
43 {
44 value: 0.75,
45 description: 'b, length normalization parameter',
46 details: []
47 },
48 {
49 value: 2,
50 description: 'dl, length of field',
51 details: []
52 },
53 {
54 value: 2.868375301361084,
55 description: 'avgdl, average length of field',
56 details: []
57 }
58 ]
59 }
60 ]
61 }
62 ]
63 }
64 },
65 {
66 title: 'Late Autumn',
67 score: 3.834893226623535,
68 scoreDetails: {
69 value: 3.834893226623535,
70 description: '$type:string/title:autumn [BM25Similarity], result of:',
71 details: [
72 {
73 value: 3.834893226623535,
74 description: 'score(freq=1.0), computed as boost * idf * tf from:',
75 details: [
76 {
77 value: 7.39188289642334,
78 description: 'idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:',
79 details: [
80 {
81 value: 14,
82 description: 'n, number of documents containing term',
83 details: []
84 },
85 {
86 value: 23529,
87 description: 'N, total number of documents with field',
88 details: []
89 }
90 ]
91 },
92 {
93 value: 0.5187978744506836,
94 description: 'tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:',
95 details: [
96 {
97 value: 1,
98 description: 'freq, occurrences of term within document',
99 details: []
100 },
101 {
102 value: 1.2000000476837158,
103 description: 'k1, term saturation parameter',
104 details: []
105 },
106 {
107 value: 0.75,
108 description: 'b, length normalization parameter',
109 details: []
110 },
111 {
112 value: 2,
113 description: 'dl, length of field',
114 details: []
115 },
116 {
117 value: 2.868375301361084,
118 description: 'avgdl, average length of field',
119 details: []
120 }
121 ]
122 }
123 ]
124 }
125 ]
126 }
127 },
128 {
129 title: 'Cheyenne Autumn',
130 score: 3.834893226623535,
131 scoreDetails: {
132 value: 3.834893226623535,
133 description: '$type:string/title:autumn [BM25Similarity], result of:',
134 details: [
135 {
136 value: 3.834893226623535,
137 description: 'score(freq=1.0), computed as boost * idf * tf from:',
138 details: [
139 {
140 value: 7.39188289642334,
141 description: 'idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:',
142 details: [
143 {
144 value: 14,
145 description: 'n, number of documents containing term',
146 details: []
147 },
148 {
149 value: 23529,
150 description: 'N, total number of documents with field',
151 details: []
152 }
153 ]
154 },
155 {
156 value: 0.5187978744506836,
157 description: 'tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:',
158 details: [
159 {
160 value: 1,
161 description: 'freq, occurrences of term within document',
162 details: []
163 },
164 {
165 value: 1.2000000476837158,
166 description: 'k1, term saturation parameter',
167 details: []
168 },
169 {
170 value: 0.75,
171 description: 'b, length normalization parameter',
172 details: []
173 },
174 {
175 value: 2,
176 description: 'dl, length of field',
177 details: []
178 },
179 {
180 value: 2.868375301361084,
181 description: 'avgdl, average length of field',
182 details: []
183 }
184 ]
185 }
186 ]
187 }
188 ]
189 }
190 }
191]

MongoDB Search 在计算分数时会考虑以下 BM25Similarity 因素:

因子
说明

boost

在查询时使用查询操作符的 score.boost 选项指定的因素。您可以将 boost 设置为正数或文档中数字字段的值。要了解详情,请参阅boost。

idf

查询的反向文档频率。MongoDB Search 使用以下公式计算频率:

idf = log(1 + (N - n + 0.5) / (n + 0.5))

其中:

  • N 是包含查询字段的文档总数。

  • n 是包含查询术语的文档数量。

tf

词语频率。MongoDB Search 使用以下公式计算频率:

tf = freq / (freq + k1 * (1 - b + b * dl / avgdl))

其中:

  • freq 是该词语在文档中出现的次数。

  • k1 是内部指定的词语饱和度参数。它会影响每次重复出现该词语时分数的增加幅度。

  • b 是内部指定的长度归一化参数。b 乘以 dlavgdl 的比率。如果 b 增加,则dlavgdl 之比的影响也会放大。

  • dl 是文档中字段的长度。

  • avgdl 是所有文档中字段的平均长度。

以下示例使用 near 运算符查询 sample_mflix.movies 集合中的 released 字段,以查找接近 2010 年 1 月 01 日上映的电影。该查询在 $search 阶段指定 scoreDetails 选项,以检索结果中每个文档的得分明细。该查询使用 $limit 阶段将结果限制为三个文档,并使用 $project 阶段执行以下操作:

  • 排除_id字段。

  • 仅包含 titlereleased 字段。

  • score字段添加到结果中以返回文档的分数,并将scoreDetails字段添加到结果中以返回文档分数的明细。

1db.movies.aggregate([
2 {
3 "$search": {
4 "near": {
5 "path": "released",
6 "origin": ISODate("2010-01-01T00:00:00.000+00:00"),
7 "pivot": 7776000000
8 },
9 "scoreDetails": true
10 }
11 },
12 {
13 "$limit": 3
14 },
15 {
16 "$project": {
17 "_id": 0,
18 "title": 1,
19 "released": 1,
20 "score": { "$meta": "searchScore" },
21 "scoreDetails": { "$meta": "searchScoreDetails" }
22 }
23 }
24])
1[
2 {
3 title: 'Tony',
4 released: ISODate("2010-01-01T00:00:00.000Z"),
5 score: 1,
6 scoreDetails: {
7 value: 1,
8 description: 'Distance score, computed as weight * pivotDistance / (pivotDistance + abs(value - origin)) from:',
9 details: [
10 { value: 1, description: 'weight', details: [] },
11 {
12 value: 7776000000,
13 description: 'pivotDistance',
14 details: []
15 },
16 { value: 1262303969280, description: 'origin', details: [] },
17 {
18 value: 1262303969280,
19 description: 'current value',
20 details: []
21 }
22 ]
23 }
24 },
25 {
26 title: 'And Everything Is Going Fine',
27 released: ISODate("2010-01-01T00:00:00.000Z"),
28 score: 1,
29 scoreDetails: {
30 value: 1,
31 description: 'Distance score, computed as weight * pivotDistance / (pivotDistance + abs(value - origin)) from:',
32 details: [
33 { value: 1, description: 'weight', details: [] },
34 {
35 value: 7776000000,
36 description: 'pivotDistance',
37 details: []
38 },
39 { value: 1262303969280, description: 'origin', details: [] },
40 {
41 value: 1262303969280,
42 description: 'current value',
43 details: []
44 }
45 ]
46 }
47 },
48 {
49 title: 'A Film with Me in It',
50 released: ISODate("2010-01-01T00:00:00.000Z")
51 score: 1,
52 scoreDetails: {
53 value: 1,
54 description: 'Distance score, computed as weight * pivotDistance / (pivotDistance + abs(value - origin)) from:',
55 details: [
56 { value: 1, description: 'weight', details: [] },
57 {
58 value: 7776000000,
59 description: 'pivotDistance',
60 details: []
61 },
62 { value: 1262303969280, description: 'origin', details: [] },
63 {
64 value: 1262303969280,
65 description: 'current value',
66 details: []
67 }
68 ]
69 }
70 }
71]

对于距离分数, MongoDB Search 在计算分数时会考虑以下因素:

因子
说明

pivot

指定为参考点的值,如果fieldValueorigin之间的距离等于0.5 ,则分数等于该值。 这定义了随着fieldValueorigin之间的距离增加,分数衰减的速度。 对于fieldValueorigin之间的给定距离,如果pivot减小,则分数也会减小。

distance

fieldValueorigin 之间的绝对距离。MongoDB Search 使用以下公式计算该值:

abs(fieldValue - origin)

其中:

  • fieldValue 是您在文档中查询的字段的值。

  • origin 是要在附近搜索的数字、日期或地理位置点。

以下示例使用复合操作符,通过以下子句查询 sample_mflix.movies 集合中的电影:

  • filter 用于查找标题中包含 friend 一词的电影的子句。

  • must 子句查找 20002015 年之间上映的电影。

  • mustNot 子句查找不属于 ShortWesternBiography 类型的电影。

该查询在 $search 阶段指定 scoreDetails 选项,以检索结果中每个文档分数的详细明细。该查询使用 $limit 阶段将结果限制为三份文档,并使用 $project 阶段执行以下操作:

  • 排除_id字段。

  • 仅包含 titlereleasedgenres 字段。

  • score字段添加到结果中以返回文档的分数,并将scoreDetails字段添加到结果中以返回文档分数的明细。

1db.movies.aggregate([
2 {
3 "$search": {
4 "compound": {
5 "filter": [{
6 "text": {
7 "query": "friend",
8 "path": "title"
9 }
10 }],
11 "must": [{
12 "range": {
13 "path": "year",
14 "gte": 2000,
15 "lte": 2015
16 }
17 }],
18 "mustNot": [{
19 "text": {
20 "query": ["Short, Western", "Biography"],
21 "path": "genres"
22 }
23 }]
24 },
25 "scoreDetails": true
26 }
27 },
28 {
29 "$limit": 3
30 },
31 {
32 "$project": {
33 "_id": 0,
34 "title": 1,
35 "released": 1,
36 "genres": 1,
37 "score": { "$meta": "searchScore" },
38 "scoreDetails": { "$meta": "searchScoreDetails" }
39 }
40 }
41])
1[
2 {
3 genres: [ 'Comedy', 'Drama', 'Mystery' ],
4 title: 'With a Friend Like Harry...',
5 released: ISODate("2001-06-15T00:00:00.000Z"),
6 score: 1,
7 scoreDetails: {
8 value: 1,
9 description: 'sum of:',
10 details: [
11 {
12 value: 0,
13 description: 'match on required clause, product of:',
14 details: [
15 { value: 0, description: '# clause', details: [] },
16 {
17 value: 1,
18 description: '$type:string/title:friend',
19 details: []
20 }
21 ]
22 },
23 {
24 value: 1,
25 description: 'sum of:',
26 details: [
27 {
28 value: 1,
29 description: 'sum of:',
30 details: [
31 {
32 value: 1,
33 description: '$type:double/year:[4656510908468559872 TO 4656576879166226432]',
34 details: []
35 }
36 ]
37 }
38 ]
39 }
40 ]
41 }
42 },
43 {
44 genres: [ 'Drama' ],
45 title: 'My Friend Henry',
46 released: ISODate("2004-08-20T00:00:00.000Z"),
47 score: 1,
48 scoreDetails: {
49 value: 1,
50 description: 'sum of:',
51 details: [
52 {
53 value: 0,
54 description: 'match on required clause, product of:',
55 details: [
56 { value: 0, description: '# clause', details: [] },
57 {
58 value: 1,
59 description: '$type:string/title:friend',
60 details: []
61 }
62 ]
63 },
64 {
65 value: 1,
66 description: 'sum of:',
67 details: [
68 {
69 value: 1,
70 description: 'sum of:',
71 details: [
72 {
73 value: 1,
74 description: '$type:double/year:[4656510908468559872 TO 4656576879166226432]',
75 details: []
76 }
77 ]
78 }
79 ]
80 }
81 ]
82 }
83 },
84 {
85 genres: [ 'Comedy', 'Drama' ],
86 title: 'A Friend of Mine',
87 released: ISODate("2006-10-26T00:00:00.000Z"),
88 score: 1,
89 scoreDetails: {
90 value: 1,
91 description: 'sum of:',
92 details: [
93 {
94 value: 0,
95 description: 'match on required clause, product of:',
96 details: [
97 { value: 0, description: '# clause', details: [] },
98 {
99 value: 1,
100 description: '$type:string/title:friend',
101 details: []
102 }
103 ]
104 },
105 {
106 value: 1,
107 description: 'sum of:',
108 details: [
109 {
110 value: 1,
111 description: 'sum of:',
112 details: [
113 {
114 value: 1,
115 description: '$type:double/year:[4656510908468559872 TO 4656576879166226432]',
116 details: []
117 }
118 ]
119 }
120 ]
121 }
122 ]
123 }
124 }
125]

注意

15、56 和 97 行结果中的 # clause 代表复合操作符查询 filter 子句,它对文档的得分没有任何贡献。

下面的示例使用 embeddedDocument 操作符在 sample_training.companies 集合中的 products.name 字段中查询包含 Basic 字段(前面有任意数量的其他字符)的产品。查询在 embeddedDocument 操作符中指定返回的 score 必须是所有匹配的嵌入式文档的总和。该查询还在 $search 阶段指定 scoreDetails 选项,以检索结果中每个文档分数的详细明细。该查询使用 $limit 阶段将结果限制为三个文档,并使用 $project 阶段执行以下操作:

  • 排除_id字段。

  • 仅包含products.name字段。

  • score字段添加到结果中以返回文档的分数,并将scoreDetails字段添加到结果中以返回文档分数的明细。

1db.companies.aggregate({
2 "$search": {
3 "embeddedDocument": {
4 "path": "products",
5 "operator": {
6 "wildcard": {
7 "path": "products.name",
8 "query": "*Basic",
9 "allowAnalyzedField": true
10 }
11 },
12 "score": {
13 "embedded": {
14 "aggregate": "sum"
15 }
16 }
17 },
18 "scoreDetails": true
19 }
20},
21{
22 "$limit": 3
23},
24{
25 "$project": {
26 "_id": 0,
27 "name": 1,
28 "products.name": 1,
29 "score": { "$meta": "searchScore" },
30 "scoreDetails": { "$meta": "searchScoreDetails" }
31 }
32})
1[
2 {
3 name: 'Plaxo',
4 products: [
5 { name: 'Plaxo Basic' },
6 { name: 'Plaxo Pulse' },
7 { name: 'Plaxo Personal Assistant' }
8 ],
9 score: 1,
10 scoreDetails: {
11 value: 1,
12 description: 'Score based on 1 child docs in range from 27 to 29, best match:',
13 details: [
14 {
15 value: 1,
16 description: '$embedded:8/products/$type:string/products.name:*Basic',
17 details: []
18 }
19 ]
20 }
21 },
22 {
23 name: 'The Game Creators',
24 products: [
25 { name: 'Dark Basic Professional' },
26 { name: 'FPS Creator' },
27 { name: 'FPS Creator X10' }
28 ],
29 score: 1,
30 scoreDetails: {
31 value: 1,
32 description: 'Score based on 1 child docs in range from 7474 to 7476, best match:',
33 details: [
34 {
35 value: 1,
36 description: '$embedded:8/products/$type:string/products.name:*basic',
37 details: []
38 }
39 ]
40 }
41 },
42 {
43 name: 'Load Impact',
44 products: [
45 { name: 'Load Impact LIGHT' },
46 { name: 'Load Impact BASIC' },
47 { name: 'Load Impact PROFESSIONAL' },
48 { name: 'Load Impact ADVANCED' }
49 ],
50 score: 1,
51 scoreDetails: {
52 value: 1,
53 description: 'Score based on 1 child docs in range from 11545 to 11548, best match:',
54 details: [
55 {
56 value: 1,
57 description: '$embedded:8/products/$type:string/products.name:*basic',
58 details: []
59 }
60 ]
61 }
62 }
63]

注意

对于基于范围内子文档的分数,范围内的数字表示 Lucene 在后台索引的父文档和子文档的 ID。子文档中的 description (在第 16 行、 36 行和 57 行)显示路径的内部表示形式。

以下示例演示如何使用$searchscoreDetails sample_mflix.movies选项检索针对collection的 函数表达式示例 查询结果中的文档的分数明细。

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 "scoreDetails": true
23 }
24},
25{
26 $limit: 5
27},
28{
29 $project: {
30 "_id": 0,
31 "title": 1,
32 "score": { "$meta": "searchScore" },
33 "scoreDetails": {"$meta": "searchScoreDetails"}
34 }
35}])
[
{
title: 'Men...',
score: 23.431293487548828,
scoreDetails: {
value: 23.431293487548828,
description: 'FunctionScoreQuery($type:string/title:men, scored by (imdb.rating * scores)) [BM25Similarity], result of:',
details: [
{
value: 23.431293487548828,
description: '(imdb.rating * scores)',
details: []
}
]
}
},
{
title: '12 Angry Men',
score: 22.080968856811523,
scoreDetails: {
value: 22.080968856811523,
description: 'FunctionScoreQuery($type:string/title:men, scored by (imdb.rating * scores)) [BM25Similarity], result of:',
details: [
{
value: 22.080968856811523,
description: '(imdb.rating * scores)',
details: []
}
]
}
},
{
title: 'X-Men',
score: 21.34803581237793,
scoreDetails: {
value: 21.34803581237793,
description: 'FunctionScoreQuery($type:string/title:men, scored by (imdb.rating * scores)) [BM25Similarity], result of:',
details: [
{
value: 21.34803581237793,
description: '(imdb.rating * scores)',
details: []
}
]
}
},
{
title: 'X-Men',
score: 21.34803581237793,
scoreDetails: {
value: 21.34803581237793,
description: 'FunctionScoreQuery($type:string/title:men, scored by (imdb.rating * scores)) [BM25Similarity], result of:',
details: [
{
value: 21.34803581237793,
description: '(imdb.rating * scores)',
details: []
}
]
}
},
{
title: 'Matchstick Men',
score: 21.05954933166504,
scoreDetails: {
value: 21.05954933166504,
description: 'FunctionScoreQuery($type:string/title:men, scored by (imdb.rating * scores)) [BM25Similarity], result of:',
details: [
{
value: 21.05954933166504,
description: '(imdb.rating * scores)',
details: []
}
]
}
}
]
1db.movies.aggregate([
2 {
3 "$search": {
4 "text": {
5 "path": "title",
6 "query": "men",
7 "score": {
8 "function":{
9 "constant": 3
10 }
11 }
12 },
13 "scoreDetails": true
14 }
15 },
16 {
17 $limit: 5
18 },
19 {
20 $project: {
21 "_id": 0,
22 "title": 1,
23 "score": { "$meta": "searchScore" },
24 "scoreDetails": {"$meta": "searchScoreDetails"}
25 }
26 }
27])
[
{
title: 'Men Without Women',
score: 3,
scoreDetails: {
value: 3,
description: 'FunctionScoreQuery($type:string/title:men, scored by constant(3.0)) [BM25Similarity], result of:',
details: [ { value: 3, description: 'constant(3.0)', details: [] } ]
}
},
{
title: 'One Hundred Men and a Girl',
score: 3,
scoreDetails: {
value: 3,
description: 'FunctionScoreQuery($type:string/title:men, scored by constant(3.0)) [BM25Similarity], result of:',
details: [ { value: 3, description: 'constant(3.0)', details: [] } ]
}
},
{
title: 'Of Mice and Men',
score: 3,
scoreDetails: {
value: 3,
description: 'FunctionScoreQuery($type:string/title:men, scored by constant(3.0)) [BM25Similarity], result of:',
details: [ { value: 3, description: 'constant(3.0)', details: [] } ]
}
},
{
title: "All the King's Men",
score: 3,
scoreDetails: {
value: 3,
description: 'FunctionScoreQuery($type:string/title:men, scored by constant(3.0)) [BM25Similarity], result of:',
details: [ { value: 3, description: 'constant(3.0)', details: [] } ]
}
},
{
title: 'The Men',
score: 3,
scoreDetails: {
value: 3,
description: 'FunctionScoreQuery($type:string/title:men, scored by constant(3.0)) [BM25Similarity], result of:',
details: [ { value: 3, description: 'constant(3.0)', details: [] } ]
}
}
]
1db.movies.aggregate([
2 {
3 "$search": {
4 "text": {
5 "path": "title",
6 "query": "shop",
7 "score": {
8 "function":{
9 "gauss": {
10 "path": {
11 "value": "imdb.rating",
12 "undefined": 4.6
13 },
14 "origin": 9.5,
15 "scale": 5,
16 "offset": 0,
17 "decay": 0.5
18 }
19 }
20 }
21 },
22 "scoreDetails": true
23 }
24 },
25 {
26 "$limit": 10
27 },
28 {
29 "$project": {
30 "_id": 0,
31 "title": 1,
32 "score": { "$meta": "searchScore" },
33 "scoreDetails": {"$meta": "searchScoreDetails"}
34 }
35 }
36])
[
{
title: 'The Shop Around the Corner',
score: 0.9471074342727661,
scoreDetails: {
value: 0.9471074342727661,
description: 'FunctionScoreQuery($type:string/title:shop, scored by exp((max(0, |imdb.rating - 9.5| - 0.0)^2) / 2 * (5.0^2 / 2 * ln(0.5)))) [BM25Similarity], result of:',
details: [
{
value: 0.9471074342727661,
description: 'exp((max(0, |imdb.rating - 9.5| - 0.0)^2) / 2 * (5.0^2 / 2 * ln(0.5)))',
details: []
}
]
}
},
{
title: 'Exit Through the Gift Shop',
score: 0.9471074342727661,
scoreDetails: {
value: 0.9471074342727661,
description: 'FunctionScoreQuery($type:string/title:shop, scored by exp((max(0, |imdb.rating - 9.5| - 0.0)^2) / 2 * (5.0^2 / 2 * ln(0.5)))) [BM25Similarity], result of:',
details: [
{
value: 0.9471074342727661,
description: 'exp((max(0, |imdb.rating - 9.5| - 0.0)^2) / 2 * (5.0^2 / 2 * ln(0.5)))',
details: []
}
]
}
},
{
title: 'The Shop on Main Street',
score: 0.9395227432250977,
scoreDetails: {
value: 0.9395227432250977,
description: 'FunctionScoreQuery($type:string/title:shop, scored by exp((max(0, |imdb.rating - 9.5| - 0.0)^2) / 2 * (5.0^2 / 2 * ln(0.5)))) [BM25Similarity], result of:',
details: [
{
value: 0.9395227432250977,
description: 'exp((max(0, |imdb.rating - 9.5| - 0.0)^2) / 2 * (5.0^2 / 2 * ln(0.5)))',
details: []
}
]
}
},
{
title: 'Chop Shop',
score: 0.8849083781242371,
scoreDetails: {
value: 0.8849083781242371,
description: 'FunctionScoreQuery($type:string/title:shop, scored by exp((max(0, |imdb.rating - 9.5| - 0.0)^2) / 2 * (5.0^2 / 2 * ln(0.5)))) [BM25Similarity], result of:',
details: [
{
value: 0.8849083781242371,
description: 'exp((max(0, |imdb.rating - 9.5| - 0.0)^2) / 2 * (5.0^2 / 2 * ln(0.5)))',
details: []
}
]
}
},
{
title: 'Little Shop of Horrors',
score: 0.8290896415710449,
scoreDetails: {
value: 0.8290896415710449,
description: 'FunctionScoreQuery($type:string/title:shop, scored by exp((max(0, |imdb.rating - 9.5| - 0.0)^2) / 2 * (5.0^2 / 2 * ln(0.5)))) [BM25Similarity], result of:',
details: [
{
value: 0.8290896415710449,
description: 'exp((max(0, |imdb.rating - 9.5| - 0.0)^2) / 2 * (5.0^2 / 2 * ln(0.5)))',
details: []
}
]
}
},
{
title: 'The Suicide Shop',
score: 0.7257778644561768,
scoreDetails: {
value: 0.7257778644561768,
description: 'FunctionScoreQuery($type:string/title:shop, scored by exp((max(0, |imdb.rating - 9.5| - 0.0)^2) / 2 * (5.0^2 / 2 * ln(0.5)))) [BM25Similarity], result of:',
details: [
{
value: 0.7257778644561768,
description: 'exp((max(0, |imdb.rating - 9.5| - 0.0)^2) / 2 * (5.0^2 / 2 * ln(0.5)))',
details: []
}
]
}
},
{
title: 'A Woman, a Gun and a Noodle Shop',
score: 0.6559237241744995,
scoreDetails: {
value: 0.6559237241744995,
description: 'FunctionScoreQuery($type:string/title:shop, scored by exp((max(0, |imdb.rating - 9.5| - 0.0)^2) / 2 * (5.0^2 / 2 * ln(0.5)))) [BM25Similarity], result of:',
details: [
{
value: 0.6559237241744995,
description: 'exp((max(0, |imdb.rating - 9.5| - 0.0)^2) / 2 * (5.0^2 / 2 * ln(0.5)))',
details: []
}
]
}
},
{
title: 'Beauty Shop',
score: 0.6274620294570923,
scoreDetails: {
value: 0.6274620294570923,
description: 'FunctionScoreQuery($type:string/title:shop, scored by exp((max(0, |imdb.rating - 9.5| - 0.0)^2) / 2 * (5.0^2 / 2 * ln(0.5)))) [BM25Similarity], result of:',
details: [
{
value: 0.6274620294570923,
description: 'exp((max(0, |imdb.rating - 9.5| - 0.0)^2) / 2 * (5.0^2 / 2 * ln(0.5)))',
details: []
}
]
}
}
]
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 "scoreDetails": true
16 }
17},
18{
19 $limit: 5
20},
21{
22 $project: {
23 "_id": 0,
24 "title": 1,
25 "score": { "$meta": "searchScore" },
26 "scoreDetails": {"$meta": "searchScoreDetails"}
27 }
28}])
[
{
title: '12 Angry Men',
score: 8.899999618530273,
scoreDetails: {
value: 8.899999618530273,
description: 'FunctionScoreQuery($type:string/title:men, scored by imdb.rating) [BM25Similarity], result of:',
details: [
{
value: 8.899999618530273,
description: 'imdb.rating',
details: []
}
]
}
},
{
title: 'The Men Who Built America',
score: 8.600000381469727,
scoreDetails: {
value: 8.600000381469727,
description: 'FunctionScoreQuery($type:string/title:men, scored by imdb.rating) [BM25Similarity], result of:',
details: [
{
value: 8.600000381469727,
description: 'imdb.rating',
details: []
}
]
}
},
{
title: 'No Country for Old Men',
score: 8.100000381469727,
scoreDetails: {
value: 8.100000381469727,
description: 'FunctionScoreQuery($type:string/title:men, scored by imdb.rating) [BM25Similarity], result of:',
details: [
{
value: 8.100000381469727,
description: 'imdb.rating',
details: []
}
]
}
},
{
title: 'X-Men: Days of Future Past',
score: 8.100000381469727,
scoreDetails: {
value: 8.100000381469727,
description: 'FunctionScoreQuery($type:string/title:men, scored by imdb.rating) [BM25Similarity], result of:',
details: [
{
value: 8.100000381469727,
description: 'imdb.rating',
details: []
}
]
}
},
{
title: 'The Best of Men',
score: 8.100000381469727,
scoreDetails: {
value: 8.100000381469727,
description: 'FunctionScoreQuery($type:string/title:men, scored by imdb.rating) [BM25Similarity], result of:',
details: [
{
value: 8.100000381469727,
description: 'imdb.rating',
details: []
}
]
}
}
]
1db.movies.aggregate([{
2 "$search": {
3 "text": {
4 "path": "title",
5 "query": "men",
6 "score": {
7 "function":{
8 "score": "relevance"
9 }
10 }
11 },
12 "scoreDetails": true
13 }
14},
15{
16 $limit: 5
17},
18{
19 $project: {
20 "_id": 0,
21 "title": 1,
22 "score": { "$meta": "searchScore" },
23 "scoreDetails": {"$meta": "searchScoreDetails"}
24 }
25}])
[
{
title: 'Men...',
score: 3.4457783699035645,
scoreDetails: {
value: 3.4457783699035645,
description: 'FunctionScoreQuery($type:string/title:men, scored by scores) [BM25Similarity], result of:',
details: [
{
value: 3.4457783699035645,
description: 'weight($type:string/title:men in 4705) [BM25Similarity], result of:',
details: [
{
value: 3.4457783699035645,
description: 'score(freq=1.0), computed as boost * idf * tf from:',
details: [
{
value: 5.5606818199157715,
description: 'idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:',
details: [
{
value: 90,
description: 'n, number of documents containing term',
details: []
},
{
value: 23529,
description: 'N, total number of documents with field',
details: []
}
]
},
{
value: 0.6196683645248413,
description: 'tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:',
details: [
{
value: 1,
description: 'freq, occurrences of term within document',
details: []
},
{
value: 1.2000000476837158,
description: 'k1, term saturation parameter',
details: []
},
{
value: 0.75,
description: 'b, length normalization parameter',
details: []
},
{
value: 1,
description: 'dl, length of field',
details: []
},
{
value: 2.868375301361084,
description: 'avgdl, average length of field',
details: []
}
]
}
]
}
]
}
]
}
},
{
title: 'The Men',
score: 2.8848698139190674,
scoreDetails: {
value: 2.8848698139190674,
description: 'FunctionScoreQuery($type:string/title:men, scored by scores) [BM25Similarity], result of:',
details: [
{
value: 2.8848698139190674,
description: 'weight($type:string/title:men in 870) [BM25Similarity], result of:',
details: [
{
value: 2.8848698139190674,
description: 'score(freq=1.0), computed as boost * idf * tf from:',
details: [
{
value: 5.5606818199157715,
description: 'idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:',
details: [
{
value: 90,
description: 'n, number of documents containing term',
details: []
},
{
value: 23529,
description: 'N, total number of documents with field',
details: []
}
]
},
{
value: 0.5187978744506836,
description: 'tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:',
details: [
{
value: 1,
description: 'freq, occurrences of term within document',
details: []
},
{
value: 1.2000000476837158,
description: 'k1, term saturation parameter',
details: []
},
{
value: 0.75,
description: 'b, length normalization parameter',
details: []
},
{
value: 2,
description: 'dl, length of field',
details: []
},
{
value: 2.868375301361084,
description: 'avgdl, average length of field',
details: []
}
]
}
]
}
]
}
]
}
},
{
title: 'Simple Men',
score: 2.8848698139190674,
scoreDetails: {
value: 2.8848698139190674,
description: 'FunctionScoreQuery($type:string/title:men, scored by scores) [BM25Similarity], result of:',
details: [
{
value: 2.8848698139190674,
description: 'weight($type:string/title:men in 6371) [BM25Similarity], result of:',
details: [
{
value: 2.8848698139190674,
description: 'score(freq=1.0), computed as boost * idf * tf from:',
details: [
{
value: 5.5606818199157715,
description: 'idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:',
details: [
{
value: 90,
description: 'n, number of documents containing term',
details: []
},
{
value: 23529,
description: 'N, total number of documents with field',
details: []
}
]
},
{
value: 0.5187978744506836,
description: 'tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:',
details: [
{
value: 1,
description: 'freq, occurrences of term within document',
details: []
},
{
value: 1.2000000476837158,
description: 'k1, term saturation parameter',
details: []
},
{
value: 0.75,
description: 'b, length normalization parameter',
details: []
},
{
value: 2,
description: 'dl, length of field',
details: []
},
{
value: 2.868375301361084,
description: 'avgdl, average length of field',
details: []
}
]
}
]
}
]
}
]
}
},
{
title: 'X-Men',
score: 2.8848698139190674,
scoreDetails: {
value: 2.8848698139190674,
description: 'FunctionScoreQuery($type:string/title:men, scored by scores) [BM25Similarity], result of:',
details: [
{
value: 2.8848698139190674,
description: 'weight($type:string/title:men in 8368) [BM25Similarity], result of:',
details: [
{
value: 2.8848698139190674,
description: 'score(freq=1.0), computed as boost * idf * tf from:',
details: [
{
value: 5.5606818199157715,
description: 'idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:',
details: [
{
value: 90,
description: 'n, number of documents containing term',
details: []
},
{
value: 23529,
description: 'N, total number of documents with field',
details: []
}
]
},
{
value: 0.5187978744506836,
description: 'tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:',
details: [
{
value: 1,
description: 'freq, occurrences of term within document',
details: []
},
{
value: 1.2000000476837158,
description: 'k1, term saturation parameter',
details: []
},
{
value: 0.75,
description: 'b, length normalization parameter',
details: []
},
{
value: 2,
description: 'dl, length of field',
details: []
},
{
value: 2.868375301361084,
description: 'avgdl, average length of field',
details: []
}
]
}
]
}
]
}
]
}
},
{
title: 'Mystery Men',
score: 2.8848698139190674,
scoreDetails: {
value: 2.8848698139190674,
description: 'FunctionScoreQuery($type:string/title:men, scored by scores) [BM25Similarity], result of:',
details: [
{
value: 2.8848698139190674,
description: 'weight($type:string/title:men in 8601) [BM25Similarity], result of:',
details: [
{
value: 2.8848698139190674,
description: 'score(freq=1.0), computed as boost * idf * tf from:',
details: [
{
value: 5.5606818199157715,
description: 'idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:',
details: [
{
value: 90,
description: 'n, number of documents containing term',
details: []
},
{
value: 23529,
description: 'N, total number of documents with field',
details: []
}
]
},
{
value: 0.5187978744506836,
description: 'tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:',
details: [
{
value: 1,
description: 'freq, occurrences of term within document',
details: []
},
{
value: 1.2000000476837158,
description: 'k1, term saturation parameter',
details: []
},
{
value: 0.75,
description: 'b, length normalization parameter',
details: []
},
{
value: 2,
description: 'dl, length of field',
details: []
},
{
value: 2.868375301361084,
description: 'avgdl, average length of field',
details: []
}
]
}
]
}
]
}
]
}
}
]
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 "scoreDetails": true
18 }
19},
20{
21 $limit: 5
22},
23{
24 $project: {
25 "_id": 0,
26 "title": 1,
27 "score": { "$meta": "searchScore" },
28 "scoreDetails": {"$meta": "searchScoreDetails"}
29 }
30}])
[
{
title: '12 Angry Men',
score: 0.9493899941444397,
scoreDetails: {
value: 0.9493899941444397,
description: 'FunctionScoreQuery($type:string/title:men, scored by log(imdb.rating)) [BM25Similarity], result of:',
details: [
{
value: 0.9493899941444397,
description: 'log(imdb.rating)',
details: []
}
]
}
},
{
title: 'The Men Who Built America',
score: 0.9344984292984009,
scoreDetails: {
value: 0.9344984292984009,
description: 'FunctionScoreQuery($type:string/title:men, scored by log(imdb.rating)) [BM25Similarity], result of:',
details: [
{
value: 0.9344984292984009,
description: 'log(imdb.rating)',
details: []
}
]
}
},
{
title: 'No Country for Old Men',
score: 0.9084849953651428,
scoreDetails: {
value: 0.9084849953651428,
description: 'FunctionScoreQuery($type:string/title:men, scored by log(imdb.rating)) [BM25Similarity], result of:',
details: [
{
value: 0.9084849953651428,
description: 'log(imdb.rating)',
details: []
}
]
}
},
{
title: 'X-Men: Days of Future Past',
score: 0.9084849953651428,
scoreDetails: {
value: 0.9084849953651428,
description: 'FunctionScoreQuery($type:string/title:men, scored by log(imdb.rating)) [BM25Similarity], result of:',
details: [
{
value: 0.9084849953651428,
description: 'log(imdb.rating)',
details: []
}
]
}
},
{
title: 'The Best of Men',
score: 0.9084849953651428,
scoreDetails: {
value: 0.9084849953651428,
description: 'FunctionScoreQuery($type:string/title:men, scored by log(imdb.rating)) [BM25Similarity], result of:',
details: [
{
value: 0.9084849953651428,
description: 'log(imdb.rating)',
details: []
}
]
}
}
]

后退

评分选项

在此页面上