AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

スコアを修正する

次のスコア変更オプションはすべての演算子で使用できます。 詳細と例については、次のいずれかのオプションをクリックしてください。

boostオプションは、結果の基本スコアに特定の数値またはドキュメント内の数値フィールドの値を乗算します。 たとえば、 boostを使用すると、結果内の一致する特定のドキュメントの重要性を高めることができます。

注意

  • boostとconstantオプションは併用できません。

  • The boost option with path is the same as multiplying using the function option with a path expression.

boostオプションは次のフィールドを取ります。

フィールド
タイプ
必要性
説明

value

float

条件付き

デフォルトの基本スコアに乗じる数値。 値は正の数である必要があります。 valueとpathのいずれかが必要ですが、両方を指定することはできません。

path

string

条件付き

デフォルトの基本スコアに乗じる値を持つ数値フィールドの名前。 pathとvalueのいずれかが必要ですが、両方を指定することはできません。

undefined

float

任意

pathを通じて指定された数値フィールドがドキュメント内に見つからない場合に、 pathに置き換える数値。 省略された場合、デフォルトは0となります。 これは、 pathを指定した場合にのみ指定できます。

The following examples use the movies collection in the sample_mflix database. If you have the sample dataset on your cluster, you can create the MongoDB Search default index and run the example queries on your cluster.

サンプル複合クエリは、ある検索条件の重要性を別の検索条件よりも高める方法を示しています。 クエリには、 titleとscoreを除くすべてのフィールドを除外する$projectステージが含まれています。

次の例では、 複合 演算子は テキスト 演算子を使用して、 フィールドとHelsinki フィールドで用語plot titleを検索しています。titleフィールドのクエリでは、 scoreとboostオプションを併用して、 valueフィールドに指定されているようにスコア結果に 3 を掛けます。

1db.movies.aggregate([
2 {
3 "$search": {
4 "compound": {
5 "should": [{
6 "text": {
7 "query": "Helsinki",
8 "path": "plot"
9 }
10 },
11 {
12 "text": {
13 "query": "Helsinki",
14 "path": "title",
15 "score": { "boost": { "value": 3 } }
16 }
17 }]
18 }
19 }
20 },
21 {
22 "$limit": 5
23 },
24 {
25 "$project": {
26 "_id": 0,
27 "title": 1,
28 "plot": 1,
29 "score": { "$meta": "searchScore" }
30 }
31 }
32])

このクエリは次の結果を返します。ここでは、 titleがクエリタームに一致するドキュメントのスコアが、その基本値から3を掛けて計算されます。

[
{
plot: 'Epic tale about two generations of men in a wealthy Finnish family, spanning from the 1960s all the way through the early 1990s. The father has achieved his position as director of the ...',
title: 'Kites Over Helsinki',
score: 12.2470121383667
},
{
plot: 'Alex is Finlander married to an Italian who works as a taxi driver in Berlin. One night in his taxi come two men with a briefcase full of money. Unluckily for Alex, they are being chased by...',
title: 'Helsinki-Naples All Night Long',
score: 9.56808090209961
},
{
plot: 'The recession hits a couple in Helsinki.',
title: 'Drifting Clouds',
score: 4.5660295486450195
},
{
plot: 'Two teenagers from Helsinki are sent on a mission by their drug dealer.',
title: 'Sairaan kaunis maailma',
score: 4.041563034057617
},
{
plot: 'A murderer tries to leave his criminal past in East Helsinki and make a new life for his family',
title: 'Bad Luck Love',
score: 3.6251673698425293
}
]

constantオプションは、基本スコアを指定された数値に置き換えます。

注意

constantとboostオプションは併用しないでください。

次の例では、sample_mflix.movies コレクションのデフォルト インデックスを使用して、plot title複合 演算子を使用して フィールドと フィールドをクエリします。クエリでは、テキスト演算子はscoreをconstantオプションと併用して、クエリがtitleフィールドのみに一致する結果のすべてのスコア結果を5に置き換えます。

1db.movies.aggregate([
2 {
3 "$search": {
4 "compound": {
5 "should": [{
6 "text": {
7 "query": "tower",
8 "path": "plot"
9 }
10 },
11 {
12 "text": {
13 "query": "tower",
14 "path": "title",
15 "score": { "constant": { "value": 5 } }
16 }
17 }]
18 }
19 }
20 },
21 {
22 "$limit": 5
23 },
24 {
25 "$project": {
26 "_id": 0,
27 "title": 1,
28 "plot": 1,
29 "score": { "$meta": "searchScore" }
30 }
31 }
32])

上記のクエリは次の結果を返します。ここでは、 titleフィールドに対してクエリに一致するドキュメントのスコアは、指定されたconstant値のみに置き換えられます。

1[
2 {
3 plot: 'Several months after witnessing a murder, residents of Tower Block 31 find themselves being picked off by a sniper, pitting those lucky enough to be alive into a battle for survival.',
4 title: 'Tower Block',
5 score: 8.15460205078125
6 },
7 {
8 plot: "When a group of hard-working guys find out they've fallen victim to their wealthy employer's Ponzi scheme, they conspire to rob his high-rise residence.",
9 title: 'Tower Heist',
10 score: 5
11 },
12 {
13 plot: 'Toru Kojima and his friend Koji are young student boys with one thing in common - they both love to date older women. Koji is a playboy with several women, young and older, whereas Toru is a romantic with his heart set on on certain lady.',
14 title: 'Tokyo Tower',
15 score: 5
16 },
17 {
18 plot: 'A middle-aged mental patient suffering from a split personality travels to Italy where his two personalities set off all kinds of confusing developments.',
19 title: 'The Leaning Tower',
20 score: 5
21 },
22 {
23 plot: 'A documentary that questions the cost -- and value -- of higher education in the United States.',
24 title: 'Ivory Tower',
25 score: 5
26 }
27]

注意

このオプションはembeddedDocument演算子でのみ使用できます。

embeddedオプションを使用すると、次の方法を構成できます。

  • 一致する複数の埋め込みドキュメントのスコアを集計します。

  • 一致する埋め込みドキュメントのスコアを集計した後、 embeddedDocument演算子のスコアを変更します。

embeddedオプションは次のフィールドを取ります。

フィールド
タイプ
必要性
説明

aggregate

string

任意

一致する埋め込みドキュメントのスコアを組み合わせる方法を設定します。 値は、次の集計戦略のいずれかである必要があります。

  • sum -(デフォルト)一致するすべての埋め込みドキュメントのスコアを合計します。

  • maximum - 一致するすべての埋め込みドキュメントの最大スコアを選択します。

  • minimum - 一致するすべての埋め込みドキュメントの最も低いスコアを選択します。

  • mean - 一致するすべての埋め込みドキュメントの平均(算術平均)スコアを選択します。MongoDB Search には、平均を計算するときにのみ一致する埋め込みドキュメントのスコアが含まれます。MongoDB Search では、クエリ述語を満たさない埋め込みドキュメントは、スコアが 0 のドキュメントとしてカウントされません。

省略した場合、このフィールドのデフォルトはsumになります。

outerScore

任意

集計戦略を適用した後に適用するスコアの変更を指定します。

次のサンプル クエリでは、 sample_analytics.transactionsコレクションでdefaultという名前のインデックスを使用します。 このインデックスは、embeddedDocuments transactions配列内のドキュメントに タイプを構成し、 フィールドとstring フィールドにはtransactions.symbol transactions.transaction_codeタイプを構成します。

例

サンプル インデックス
{
"mappings": {
"dynamic": true,
"fields": {
"transactions": {
"dynamic": true,
"fields": {
"symbol": {
"type": "string"
},
"transaction_code": {
"type": "string"
}
},
"type": "embeddedDocuments"
}
}
}
}

ドキュメントの配列を含むtransactionsフィールドのサンプル クエリには、出力を10の結果に制限する$limitステージと、次の操作を行う$projectステージが含まれています。

  • account_idとtransaction_countを除くすべてのフィールドを除外します。

  • 集計戦略とさらにスコアの変更を適用するscoreという名前のフィールドを追加します。

    注意

    You can use function inside the score option of the embeddedDocument operator's operator. However, for function score expressions that require the path option, the numeric field that you specify as the path for the expression must be inside the embeddedDocument operator's path. For example, in the following example query, the field transactions.amount that is specified inside the score option of the compound operator on line 24 is inside in the embeddedDocument operator's path transactions on line 4.

クエリは、 AMDを購入したアカウントを検索し、最大のAMD回の購入(共有数)に、アカウントがこの期間に行ったトランザクションの数のlog1pの値を掛けます。 以下に基づいてアカウントをランク付けします。

  • 1 回のトランザクションで購入されたAMDの数

  • 過去の期間のトランザクションの数

1db.transactions.aggregate({
2 "$search": {
3 "embeddedDocument": {
4 "path": "transactions",
5 "operator": {
6 "compound": {
7 "must": [
8 {
9 "text": {
10 "path": "transactions.symbol",
11 "query": "amd"
12 }
13 },
14 {
15 "text": {
16 "path": "transactions.transaction_code",
17 "query": "buy"
18 }
19 }
20 ],
21 "score": {
22 "function": {
23 "path": {
24 "value": "transactions.amount",
25 "undefined": 1.0
26 }
27 }
28 }
29 }
30 },
31 "score": {
32 "embedded": {
33 "aggregate": "maximum",
34 "outerScore": {
35 "function": {
36 "multiply": [
37 {
38 "log1p": {
39 "path": {
40 "value": "transaction_count"
41 }
42 }
43 },
44 {
45 "score": "relevance"
46 }
47 ]
48 }
49 }
50 }
51 }
52 }
53 }
54},
55{ "$limit": 10 },
56{
57 "$project": {
58 "_id": 0,
59 "account_id": 1,
60 "transaction_count": 1,
61 "score": { $meta: "searchScore" }
62 }
63})

MongoDB Search では、過去の期間に多くのトランザクションを実行し、1 回のトランザクションで多くの AMD を購入したアカウントを上位にランク付けします。

1[
2 { account_id: 467651, transaction_count: 99, score: 19982 },
3 { account_id: 271554, transaction_count: 96, score: 19851.822265625 },
4 { account_id: 71148, transaction_count: 99, score: 19840 },
5 { account_id: 408143, transaction_count: 100, score: 19836.76953125 },
6 { account_id: 977774, transaction_count: 98, score: 19822.64453125 },
7 { account_id: 187107, transaction_count: 96, score: 19754.470703125 },
8 { account_id: 492843, transaction_count: 97, score: 19744.998046875 },
9 { account_id: 373169, transaction_count: 93, score: 19553.697265625 },
10 { account_id: 249078, transaction_count: 89, score: 19436.896484375 },
11 { account_id: 77690, transaction_count: 90, score: 19418.017578125 }
12]

The function option allows you to alter the final score of the document using a numeric field . You can specify the numeric field for computing the final score through an expression. If the final result of the function score is less than 0, MongoDB Search replaces the score with 0.

注意

embeddedDocument演算子のoperatorのscoreオプション内でfunctionを使用できます。 ただし、 pathオプションを必要とする関数スコア式の場合、式のpathとして指定する数値フィールドは、 embeddedDocument演算子のpath内になければなりません。

ドキュメントの最終スコアを変更するには、 functionオプションとともに次の式を使用します。

  • 一連の数値を加算または乗算する算術式。

  • 関数スコアで定数を許可する定数式 。

  • 指定されたレートで乗算してスコアを減少または減少させます。

  • インデックス付き数値フィールド値を関数スコアに組み込む パス式 。

  • スコア式 。MongoDB Search によって割り当てられた関連性スコアを返します。

  • 単位 式: 1 つの引数を取る式です。MongoDB Search では、指定された数値のログ10(x) またはログ10(x+1) を計算できます。

注意

MongoDB Search 関数のスコアは常に float 値であり、数値が非常に大きい場合や小さい場合は精度が失われる可能性があります。

算術式は、一連の数値を加算または乗算します。 たとえば、算術式を使用して、データエンタープライズパイプラインの数値フィールドを介して関連性ランキングを変更できます。 次のいずれかのオプションを含める必要があります。

オプション
タイプ
説明

add

array of expressions

Adds a series of numbers. Takes an array of expressions, which can have negative values. Array length must be greater than or equal to 2. For example:

算術式の構文
"function": {
"add": [
{"path": "rating"},
{"score": "relevance"}
]
}

前の例では、 MongoDB Search は path 式と score 式を使用して次の項目を追加します。

  • ratingフィールドまたは0の数値(ドキュメントにratingフィールドが存在しない場合)

  • 関連性スコア( MongoDB Search が関連性に基づいて割り当てるスコア)

multiply

array of expressions

Multiplies a series of numbers. Takes an array of expressions, which can have negative values. Array length must be greater than or equal to 2. For example:

算術式の構文
"function": {
"multiply": [
{
"path": {
"value": "popularity",
"undefined": 2.5
}
},
{
"score": "relevance"
},
{
"constant": 0.75
}
]
}

前の例では、 MongoDB Search は path、score、constant 式を使用して、ドキュメントの最終スコアを変更します。次の値を乗算します。

  • パス式の数値。これはpopularityフィールドまたは2.5の数値popularityフィールドがドキュメントに存在しない場合)

  • 関連性スコア( MongoDB Search が関連性に基づいて割り当てるスコア)

  • の定数値 0.75

注意

undefinedと評価される算術式は定数値に置き換えることはできません。

次の例では、 sample_mflix.moviesコレクションのデフォルト インデックスを使用してtitleフィールドをクエリします。 サンプル クエリには、出力を5の結果に制限する$limitステージと、 titleとscoreを除くすべてのフィールドを除外する$projectステージが含まれています。

例

この例では、テキスト演算子はscoreとfunctionオプションを使用して次の値を乗算します。

  • パス式の数値。ドキュメント内のimdb.ratingフィールドまたは2フィールド(ドキュメントにimdb.ratingフィールドがない場合)の値

  • 関連性スコア、またはドキュメントの現在のスコア

db.movies.aggregate([{
"$search": {
"text": {
"path": "title",
"query": "men",
"score": {
"function":{
"multiply":[
{
"path": {
"value": "imdb.rating",
"undefined": 2
}
},
{
"score": "relevance"
}
]
}
}
}
}
},
{
$limit: 5
},
{
$project: {
"_id": 0,
"title": 1,
"score": { "$meta": "searchScore" }
}
}])

このクエリは、スコアが指定されたfunction値に置き換えられた次の結果を返します。

{ "title" : "Men...", "score" : 23.431293487548828 }
{ "title" : "12 Angry Men", "score" : 22.080968856811523 }
{ "title" : "X-Men", "score" : 21.34803581237793 }
{ "title" : "X-Men", "score" : 21.34803581237793 }
{ "title" : "Matchstick Men", "score" : 21.05954933166504 }