MongoDB Search またはMongoDB ベクトル検索クエリによって返されるすべてのドキュメントには関連性に基づいてスコアが割り当てられ、結果セットに含まれるドキュメントは最高スコアから最低スコアの順に返されます。
使用法
To include the score of each document in your search results, use the $project stage in your aggregation pipeline.
For the
$vectorSearchstage, thescorefield takes the$metaexpression, which requires thevectorSearchScorevalue to return the score of each document in your vector search results.
注意
次の URL を使用できます。
searchScore$searchクエリの場合のみ。vectorSearchScoreonly in$vectorSearchqueries
他のクエリでsearchScoreおよびvectorSearchScoreを使用すると、MongoDB は MongoDB v8.2 以降で警告をログに記録します。
例
The following query uses a $project stage to add a field named score to the returned documents:
1 db.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 ])
1 db.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 ])
詳しくは、「検索スコアの詳細を返す」を参照してください。
1 db.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 は次のオプションをサポートしています。
Considerations
結果内の複数のドキュメントのスコアが同じである場合、結果内のドキュメントの順序は非決定的になります。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 はより高いスコアを割り当てます。