定義
構文
compound の構文は次のとおりです。
1 { 2 $search: { 3 "index": <index name>, // optional, defaults to "default" 4 "compound": { 5 <must | mustNot | should | filter>: [ { <clauses> } ], 6 "score": <options>, 7 "doesNotAffect": "<facet-to-exclude>" | [<array-of-facets>] 8 } 9 } 10 }
Each must, mustNot, should, and filter clause contains an array of subclauses. Use array syntax even if the array contains only one subclause. See the examples on this page.
オプション
compound では、次の用語を使用してクエリを作成します。
ドキュメントが結果に含まれるために一致する必要がある句。返されるスコアは、句内のすべてのサブクエリのスコアの合計です。
| ||||||||||||||
ドキュメントが結果に含まれるためには、一致してはならない句。
| ||||||||||||||
結果に含まれるドキュメントの中で、一致させたい句。
複合クエリ内で | ||||||||||||||
ドキュメントが結果に含まれるために、すべて一致しなければならない句。 たとえば、 代わりに、 | ||||||||||||||
| 句全体の スコア を変更します。 | |||||||||||||
| このクエリに基づいてカウントの再計算から除外するファセットまたはファセットのリスト。値は、 |
使用法
クエリ条件の指定には、autocomplete、text、span などの任意の最上位の演算子を含む句を使用できます。
To optimize performance, place non-scoring operators such as equals, range, and in in the filter clause to avoid unnecessary scoring operations. If you want to run multiple operations that must be evaluated as an OR condition, nest a should clause within the top-level filter.
例
[ { $search: { index: "default", compound: { filter: [ { compound: { should: [ { equals: { path: "accommodates", value: 2 } }, { range: { path: "pricePerNight", lte: 200 } } ] } } ] } } } ]
スコアリングの動作
MongoDB Search は、ドキュメントで一致を生成した個々の句ごとにドキュメントが受け取ったスコアを合計することで、結果セット内のドキュメントをスコアリングします。スコアリングには、must 句と should 句のみが参加します。結果セットは、スコアの高いものから低いものの順に並べられます。
次の表は、スコアに貢献するものと貢献しないものの compound 句を示しています。
句 | スコアに貢献する |
|---|---|
| |
| |
| |
|
You can boost or replace the score of the entire compound query using the score option. For an example of replacing the entire compound score, see Compound Score Example below. You can use score to also boost or alter the score for each subquery in each clause. For some examples of altered scores in compound operator clauses, see Modify the Score.
例
次の例は、 MongoDB Search Playground または クラスターで試すことができます。
サンプル コレクション
このページの例では、次のドキュメントを含む fruit というコレクションを使用します。
1 { 2 "_id" : 1, 3 "type" : "apple", 4 "description" : "Apples come in several varieties, including Fuji, Granny Smith, and Honeycrisp.", 5 "category" : "nonorganic", 6 "in_stock" : false 7 }, 8 { 9 "_id" : 2, 10 "type" : "banana", 11 "description" : "Bananas are usually sold in bunches of five or six.", 12 "category" : "nonorganic", 13 "in_stock" : true 14 }, 15 { 16 "_id" : 3, 17 "type" : "pear", 18 "description" : "Bosc and Bartlett are the most common varieties of pears.", 19 "category" : "organic", 20 "in_stock" : true 21 }
サンプル インデックス
fruit コレクションには、コレクション内のすべてのフィールドを自動的にインデックス化し、デフォルトの標準アナライザを使用する動的マッピングを持つデフォルトのインデックスがあります。standard アナライザはすべての単語を小文字にし、よく使われるストップワード("the", "a", "and", など)は無視します。
サンプル クエリ
次のクエリは、 MongoDB Search クエリの $search compound 演算子を示しています。
must and mustNot Example
次の例では、must 句と mustNot 句の組み合わせを使用してクエリを構築します。must 句はテキスト演算子を使用して、description フィールド内の varieties というタームを検索します。ドキュメントが一致するには、must 句を満たす必要があります。mustNot 句は、description フィールド内の apples というタームの検索操作を実行します。ドキュメントが一致するには、mustNot 句を満たさない必要があります。
1 db.fruit.aggregate([ 2 { 3 "$search": { 4 "compound": { 5 "must": [{ 6 "text": { 7 "query": "varieties", 8 "path": "description" 9 } 10 }], 11 "mustNot": [{ 12 "text": { 13 "query": "apples", 14 "path": "description" 15 } 16 }] 17 } 18 } 19 } 20 ])
上記のクエリでは、description フィールドに varieties という単語が含まれ、apples が含まれていないため、_id: 3 を含むドキュメントが返されます。
➤ MongoDB Search Playground でこれを試してみてください。
must and should Example
次のクエリでは、must を使用して満たす必要のある検索条件を指定し、should を使用して Fuji という単語を含むドキュメントの優先設定を指定します。
このクエリでは、$project パイプライン ステージは _id を除くすべてのドキュメント フィールドを除外し、ドキュメントの関連性スコアを表示する score フィールドを追加します。
1 db.fruit.aggregate([ 2 { 3 "$search": { 4 "compound": { 5 "must": [{ 6 "text": { 7 "query": "varieties", 8 "path": "description" 9 } 10 }], 11 "should": [{ 12 "text": { 13 "query": "Fuji", 14 "path": "description" 15 } 16 }] 17 } 18 } 19 }, 20 { 21 "$project": { 22 "score": { "$meta": "searchScore" } 23 } 24 } 25 ])
{ "_id" : 1, "score" : 0.6425117254257202 } { "_id" : 3, "score" : 0.21649497747421265 }
_id: 1 を含むドキュメントは、description フィールドに単語 Fuji が含まれており、should 句を満たしているため、スコアが高くなります。
➤ MongoDB Search Playground でこれを試してみてください。
次のクエリでは、結果内のすべてのドキュメントに対して constant スコア 3 も指定しています。このクエリでは、$project パイプライン ステージは _id を除くすべてのドキュメント フィールドを除外し、score フィールドを追加します。
1 db.fruit.aggregate([ 2 { 3 "$search": { 4 "compound": { 5 "must": [{ 6 "text": { 7 "query": "varieties", 8 "path": "description" 9 } 10 }], 11 "should": [{ 12 "text": { 13 "query": "Fuji", 14 "path": "description" 15 } 16 }], 17 "score": { "constant": { "value": 3 } } 18 } 19 } 20 }, 21 { 22 "$project": { 23 "score": { "$meta": "searchScore" } 24 } 25 } 26 ])
[ { _id: 1, score: 3 }, { _id: 3, score: 3 } ]
クエリの constant オプションによって結果内の各ドキュメントのスコアが数値 3 に置き換えられるため、両方のドキュメントは同じスコアを受け取ります。
➤ MongoDB Search Playground でこれを試してみてください。
minimumShouldMatch の例
複数の should 句を含むクエリでは、miniumumShouldMatch オプションを使用して、結果を返すために一致する必要がある句の最小数を指定できます。
次のクエリには must 句が 1 つと should 句が 2 つあり、minimumShouldMatch の値は 1 です。結果セットに含めるには、ドキュメントの description フィールドに varieties というタームが含まれている必要があり、説明フィールドに Fuji または Golden Delicious のいずれかが含まれている必要があります。
1 db.fruit.aggregate([ 2 { 3 $search: { 4 "compound": { 5 "must": [{ 6 "text": { 7 "query": "varieties", 8 "path": "description" 9 } 10 }], 11 "should": [{ 12 "text": { 13 "query": "Fuji", 14 "path": "description" 15 } 16 }, 17 { 18 "text": { 19 "query": "Golden Delicious", 20 "path": "description" 21 } 22 }], 23 "minimumShouldMatch": 1 24 } 25 } 26 } 27 ])
1 { 2 "_id" : 1, 3 "type" : "apple", 4 "description" : "Apples come in several varieties, including Fuji, Granny Smith, and Honeycrisp.", 5 "category" : "nonorganic", 6 "in_stock" : false 7 }
_id: 1 の一致を含むドキュメントは、must 句と 2 つの should 句のうちの最初の句と一致します。
➤ MongoDB Search Playground でこれを試してみてください。
filter 例
filter filter 句は返されたドキュメントのスコアには考慮されないため、返されるドキュメントの順序に影響しないことを除いて must と同じように動作します。
次のクエリでは次の句を使用します。
mustそしてfilterで、満たす必要のある検索条件を指定します。shouldbananaという単語を含むドキュメントを優先するように指定します。should句にはminimumShouldMatchオプションは含まれていません。minimumShouldMatchを省略すると、デフォルトは0となります。
1 db.fruit.aggregate([ 2 { 3 "$search": { 4 "compound": { 5 "must": [{ 6 "text": { 7 "query": "varieties", 8 "path": "description" 9 } 10 }], 11 "should": [{ 12 "text": { 13 "query": "banana", 14 "path": "description" 15 } 16 }], 17 "filter": [{ 18 "text": { 19 "query": "granny", 20 "path": "description" 21 } 22 }] 23 } 24 } 25 } 26 ])
1 { 2 "_id" : 1, 3 "type" : "apple", 4 "description" : "Apples come in several varieties, including Fuji, Granny Smith, and Honeycrisp.", 5 "category" : "nonorganic", 6 "in_stock" : false 7 }
返されたドキュメントは、含めるためのすべての要件を満たしています。
must句とfilter句の両方が一致します。minimumShouldMatchの値が指定されていないため、デフォルトで0になります。その結果、should句は失敗し、ドキュメントが返されます。
➤ MongoDB Search Playground でこれを試してみてください。
$match を $in に filter 句を使用して、クラスター上のデータに対するクエリで置き換えることができます。次のクエリは、$search ステージで filter を使用して、一致する必要がある検索用語を指定する方法を示しています。このクエリではまた、should を使用して、varieties というタームを含むドキュメントの優先順位を指定します。このクエリには、次の操作を実行する $projectパイプラインステージが含まれています。
_idとdescriptionを除くすべてのフィールドを除外します。ドキュメントの関連性スコアを表示する
scoreフィールドを追加します。
1 db.fruit.aggregate([ 2 { 3 "$search": { 4 "compound": { 5 "filter": [{ 6 "text": { 7 "query": ["apples", "bananas"], 8 "path": "description" 9 } 10 }], 11 "should": [{ 12 "text": { 13 "query": "varieties", 14 "path": "description" 15 } 16 }] 17 } 18 } 19 }, 20 { 21 "$project": { 22 "description": 1, 23 "score": { "$meta": "searchScore" } 24 } 25 } 26 ])
1 [ 2 { 3 _id: 1, 4 description: 'Apples come in several varieties, including Fuji, Granny Smith, and Honeycrisp. The most popular varieties are McIntosh, Gala, and Granny Smith.', 5 score: 0.36074575781822205 6 }, 7 { 8 _id: 2, 9 description: 'Bananas are usually sold in bunches of five or six.', 10 score: 0 11 } 12 ]
結果に含まれるドキュメントは、含めるためのすべての要件を満たしています。
両方のドキュメントには、クエリの
filter句で指定された用語applesまたはbananasが含まれています。_id: 1を含むドキュメントは、クエリのshould句で指定されたvarietiesというタームが含まれているため、_id: 2を含むドキュメントよりもスコアが高くなります。
➤ MongoDB Search Playground でこれを試してみてください。
ネストされた例
次の例では、ネストされた compound 句を使用してクエリを構築します。この例では、fruit コレクションには type、 category、および in_stock のフィールドにインデックスがあり、そのテキスト フィールドではデフォルトのアナライザが使用されます。クエリでは、ドキュメントが次の should 句のいずれか 1 つだけを満たす必要があります。
typeフィールドにappleという単語を含めます。categoryフィールドにorganicというタームが含まれ、in_stockフィールドにtrueという値が含まれます。
1 db.fruit.aggregate([ 2 { 3 $search: { 4 "compound": { 5 "should": [ 6 { 7 "text": { 8 "query": "apple", 9 "path": "type" 10 } 11 }, 12 { 13 "compound": { 14 "must": [ 15 { 16 "text": { 17 "query": "organic", 18 "path": "category" 19 } 20 }, 21 { 22 "equals": { 23 "value": true, 24 "path": "in_stock" 25 } 26 } 27 ] 28 } 29 } 30 ], 31 "minimumShouldMatch": 1 32 } 33 } 34 } 35 ])
1 { 2 "_id" : 3, 3 "type" : "pear", 4 "description" : "Bosc and Bartlett are the most common varieties of pears.", 5 "category" : "organic", 6 "in_stock" : true 7 } 8 { 9 "_id" : 1, 10 "type" : "apple", 11 "description" : "Apples come in several varieties, including Fuji, Granny Smith, and Honeycrisp.", 12 "category" : "nonorganic", 13 "in_stock" : false 14 }
結果に含まれるドキュメントは、含まれるためのすべての要件を満たしています。
_id: 3のあるドキュメントは、2 番目のshould句内にネストされているmust句と一致します。_id: 1のあるドキュメントは最初のshould句に一致します。
➤ MongoDB Search Playground でこれを試してみてください。
メタデータの例
次のクエリはメタデータを検索し、$searchMetaパイプラインステージ、または$searchステージの$$SEARCHMETA変数のいずれかを使用して、fruitコレクションのカテゴリの数を返します。