Você pode normalizar sua pontuação de query $search no intervalo de 0 a 1 nos estágios subsequentes do seu pipeline de agregação. Você pode usar os seguintes estágios após seu estágio $search na seguinte ordem para normalizar a pontuação:
Os exemplos a seguir demonstram como normalizar a pontuação de algumas das queries de exemplo em relação à collection sample_mflix.movies usando os estágios $addFields e $setWindowFields após o estágio $search . Para saber mais sobre as queries, consulte functionexemplos de opção .
1
db.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
]
1
db.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
}
23
},
24
{
25
"$limit":5
26
},
27
{
28
"$addFields":{
29
"score":{
30
"$meta":"searchScore"
31
}
32
}
33
},
34
{
35
"$setWindowFields":{
36
"output":{
37
"maxScore":{
38
"$max":"$score"
39
}
40
}
41
}
42
},
43
{
44
"$addFields":{
45
"normalizedScore":{
46
"$divide":[
47
"$score","$maxScore"
48
]
49
}
50
}
51
},
52
{
53
"$project":{
54
"_id":0,
55
"title":1,
56
"score":1,
57
"maxScore":1,
58
"normalizedScore":1
59
}
60
}])
1
[
2
{
3
title: 'Men...',
4
score:23.431293487548828,
5
maxScore:23.431293487548828,
6
normalizedScore:1
7
},
8
{
9
title: '12 Angry Men',
10
score:22.080968856811523,
11
maxScore:23.431293487548828,
12
normalizedScore:0.9423708882544255
13
},
14
{
15
title: 'X-Men',
16
score:21.34803581237793,
17
maxScore:23.431293487548828,
18
normalizedScore:0.911090795039637
19
},
20
{
21
title: 'X-Men',
22
score:21.34803581237793,
23
maxScore:23.431293487548828,
24
normalizedScore:0.911090795039637
25
},
26
{
27
title: 'Matchstick Men',
28
score:21.05954933166504,
29
maxScore:23.431293487548828,
30
normalizedScore:0.8987787781692841
31
}
32
]
1
db.movies.aggregate([{
2
"$search":{
3
"text":{
4
"path":"title",
5
"query":"shop",
6
"score":{
7
"function":{
8
"gauss":{
9
"path":{
10
"value":"imdb.rating",
11
"undefined":4.6
12
},
13
"origin":9.5,
14
"scale":5,
15
"offset":0,
16
"decay":0.5
17
}
18
}
19
}
20
}
21
}
22
},
23
{
24
"$limit":5
25
},
26
{
27
"$addFields":{
28
"score":{
29
"$meta":"searchScore"
30
}
31
}
32
},
33
{
34
"$setWindowFields":{
35
"output":{
36
"maxScore":{
37
"$max":"$score"
38
}
39
}
40
}
41
},
42
{
43
"$addFields":{
44
"normalizedScore":{
45
"$divide":[
46
"$score","$maxScore"
47
]
48
}
49
}
50
},
51
{
52
"$project":{
53
"_id":0,
54
"title":1,
55
"score":1,
56
"maxScore":1,
57
"normalizedScore":1
58
}
59
}])
1
[
2
{
3
title: 'The Shop Around the Corner',
4
score:0.9471074342727661,
5
maxScore:0.9471074342727661,
6
normalizedScore:1
7
},
8
{
9
title: 'Exit Through the Gift Shop',
10
score:0.9471074342727661,
11
maxScore:0.9471074342727661,
12
normalizedScore:1
13
},
14
{
15
title: 'The Shop on Main Street',
16
score:0.9395227432250977,
17
maxScore:0.9471074342727661,
18
normalizedScore:0.9919917310611205
19
},
20
{
21
title: 'Chop Shop',
22
score:0.8849083781242371,
23
maxScore:0.9471074342727661,
24
normalizedScore:0.9343273488331464
25
},
26
{
27
title: 'Little Shop of Horrors',
28
score:0.8290896415710449,
29
maxScore:0.9471074342727661,
30
normalizedScore:0.8753913353110349
31
}
32
]
1
db.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
}
16
},
17
{
18
"$limit":5
19
},
20
{
21
"$addFields":{
22
"score":{
23
"$meta":"searchScore"
24
}
25
}
26
},
27
{
28
"$setWindowFields":{
29
"output":{
30
"maxScore":{
31
"$max":"$score"
32
}
33
}
34
}
35
},
36
{
37
"$addFields":{
38
"normalizedScore":{
39
"$divide":[
40
"$score","$maxScore"
41
]
42
}
43
}
44
},
45
{
46
"$project":{
47
"_id":0,
48
"title":1,
49
"score":1,
50
"maxScore":1,
51
"normalizedScore":1
52
}
53
}])
1
[
2
{
3
title: '12 Angry Men',
4
score:8.899999618530273,
5
maxScore:8.899999618530273,
6
normalizedScore:1
7
},
8
{
9
title: 'The Men Who Built America',
10
score:8.600000381469727,
11
maxScore:8.899999618530273,
12
normalizedScore:0.9662922191102197
13
},
14
{
15
title: 'No Country for Old Men',
16
score:8.100000381469727,
17
maxScore:8.899999618530273,
18
normalizedScore:0.9101124414213563
19
},
20
{
21
title: 'X-Men: Days of Future Past',
22
score:8.100000381469727,
23
maxScore:8.899999618530273,
24
normalizedScore:0.9101124414213563
25
},
26
{
27
title: 'The Best of Men',
28
score:8.100000381469727,
29
maxScore:8.899999618530273,
30
normalizedScore:0.9101124414213563
31
}
32
]
1
db.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
}
18
},
19
{
20
"$limit":5
21
},
22
{
23
"$addFields":{
24
"score":{
25
"$meta":"searchScore"
26
}
27
}
28
},
29
{
30
"$setWindowFields":{
31
"output":{
32
"maxScore":{
33
"$max":"$score"
34
}
35
}
36
}
37
},
38
{
39
"$addFields":{
40
"normalizedScore":{
41
"$divide":[
42
"$score","$maxScore"
43
]
44
}
45
}
46
},
47
{
48
"$project":{
49
"_id":0,
50
"title":1,
51
"score":1,
52
"maxScore":1,
53
"normalizedScore":1
54
}
55
}])
1
[
2
{
3
title: '12 Angry Men',
4
score:0.9493899941444397,
5
maxScore:0.9493899941444397,
6
normalizedScore:1
7
},
8
{
9
title: 'The Men Who Built America',
10
score:0.9344984292984009,
11
maxScore:0.9493899941444397,
12
normalizedScore:0.9843145968064908
13
},
14
{
15
title: 'No Country for Old Men',
16
score:0.9084849953651428,
17
maxScore:0.9493899941444397,
18
normalizedScore:0.9569144408182233
19
},
20
{
21
title: 'X-Men: Days of Future Past',
22
score:0.9084849953651428,
23
maxScore:0.9493899941444397,
24
normalizedScore:0.9569144408182233
25
},
26
{
27
title: 'The Best of Men',
28
score:0.9084849953651428,
29
maxScore:0.9493899941444397,
30
normalizedScore:0.9569144408182233
31
}
32
]
Os resultados do Atlas Search contêm as seguintes pontuações:
A pontuação modificada para a consulta $search no campo score do estágio $addFields.
A pontuação máxima atribuída aos documentos nos resultados do campo maxScore do estágio $setWindowFields .
A pontuação normalizada no campo normalizedScore do estágio $addFields , que é calculada dividindo a pontuação modificada em $score pela pontuação máxima em $maxScore usando $divide.