You can customize the score of the documents in the results. By adjusting how scores are calculated, you can ensure that the most pertinent documents are ranked higher in the search results. To learn more about the different ways in which you can customize the score, see Score the Documents in the Results. This page demonstrates how to:
Modify the score of the documents in the results to boost or bury the results.
Normalize your
$searchquery score in the range from0to1in the subsequent stages of your aggregation pipeline.
결과에서 문서 점수를 수정합니다
A MongoDB Search query assigns every returned document a score based on its relevance. The documents included in a result set return in order from highest to lowest score. To learn more, see Score the Documents in the Results.
모든 연산자에서 다음 옵션을 사용하여 기본 채점 동작을 수정할 수 있습니다. 자세한 내용과 예시를 보려면 다음 옵션 중 하나를 클릭하세요.
This section demonstrates how to add weights to your search fields to boost or bury the documents in the results or a category of results. Specifically, it demonstrates how to assign one or more values to a field to return results with an increased or decreased score.
예시 인덱스
You can set up an index with dynamic mappings enabled to index all the fields in the collection. Alternatively, use static mappings on the fields that you want to query and sort the results by. To learn more about creating MongoDB Search indexes, see Manage MongoDB Search Indexes.
쿼리 예시
샘플 쿼리는 결과에서 문서의 순위를 올리거나 내리는 방법을 보여줍니다. 이러한 연산자는 복합 연산자를 사용하여 두 개 이상의 연산자를 하나의 쿼리로 결합합니다.
Use the title and year fields in the sample_mflix.movies namespace to boost the relevance score that MongoDB Search returns for movie titles that contain the term snow. If you set up the index on the movies collection, you can run the following queries.
다음 샘플 쿼리는 sample_mflix.movies 네임스페이스에서 title, plot, genres 필드를 사용하여 다음 검색을 수행합니다.
Search for all movies containing the word
ghost, but reduce the score of comedy movies to 50%.Search for all movies containing the word
ghost, but reduces the score of movies with specifiedObjectIdsby 50%.
점수 정규화
집계 파이프라인의 후속 단계에서 0 에서 1 범위의 $search 쿼리 점수를 정규화할 수 있습니다. $search 단계 이후 다음 단계를 사용하여 다음 순서로 점수를 정규화할 수 있습니다.
$addFields{ "$addFields": { "score": { "$meta": "searchScore" } } } $setWindowFields{ "$setWindowFields": { "output": { "maxScore": { "$max": "$score" } } } } $addFields{ "$addFields": { "normalizedScore": { "$divide": [ "$score", "$maxScore" ] } } }
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 } 56 ])
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 ]
MongoDB Search 결과에는 다음 점수가 포함됩니다.
$addFields단계의score필드에서$search쿼리에 대한 수정된 점수.$setWindowFields단계의maxScore필드에 있는 결과의 문서에 할당된 최대 점수입니다.The normalized score in the
normalizedScorefield from the$addFieldsstage. MongoDB computes this score by dividing the modified score in$scoreby the maximum score in$maxScoreusing$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).