注意
Atlas Search embeddedDocuments 型、 embeddedDocument 演算子、embedded
スコアリング オプションはプレビュー段階です。レプリカセットまたは単一のMongoDBシャード上の Atlas Searchインデックスが 2、100、000、000インデックスオブジェクトに達すると、Atlas Search はインデックスを古く、クエリ可能な状態に移行します。 Atlas Search2 が将来、 、100 、000 、000 を超えるインデックスオブジェクトをサポートしたい場合は、 MongoDBフィードバック エンジンでこのリクエストに投票してください。
embeddedDocument
演算子の制限
embeddedDocument
演算子内のクエリでは を強調表示できません。
定義
embeddedDocument
embeddedDocument
演算子は$elemMatch演算子に似ています。 複数のクエリ述語が、埋め込みドキュメントの配列の単一の要素で満たされるように制約されます。embeddedDocument
はembeddedDocuments型のフィールドに対するクエリにのみ使用できます。
構文
embeddedDocument
の構文は次のとおりです。
{ "embeddedDocument": { "path": "<path-to-field>", "operator": { <operator-specification> }, "score": { <score-options> } } }
オプション
embeddedDocument
では、次のオプションを使用してクエリを作成します。
フィールド | タイプ | 説明 | 必要性 |
---|---|---|---|
| オブジェクト |
| 必須 |
| string | 検索するためのインデックス付きのembeddedDocumentsタイプ フィールド。指定されたフィールドは、 | 必須 |
| オブジェクト | 一致する検索結果に割り当てる スコア 。 | 任意 |
動作
embeddedDocument
演算子を使用して配列内の埋め込みドキュメントをクエリすると、Atlas Search はクエリ実行のさまざまな段階で 演算子クエリ述語を評価し、スコアを付けます。 Atlas Search:
配列内の各埋め込みドキュメントを個別に評価します。
embedded
オプションを使用して設定された一致結果のスコアを組み合わせます。または、embedded
スコア オプションを指定しない場合は、一致結果のスコアを合計したスコアを組み合わせます。compound
演算子を通じて他のクエリ述語が指定されている場合、一致する結果を親ドキュメントと結合します。注意
string ファセットの場合、Atlas Search は結果セット内の各ドキュメントに対して string ファセットを 1 回カウントします。 この動作の例については、「例 」を参照してください。
スコアリングの動作
デフォルトでは、 embeddedDocument
演算子は、埋め込みドキュメント一致のスコアを結合するためにデフォルトの集計戦略sum
を使用します。 embeddedDocument
演算子score
オプションを使用すると、デフォルトを上書きし、 embedded
オプションを使用して一致する結果のスコアを構成できます。
ソート動作
埋め込まれたドキュメント フィールドで親ドキュメントをソートするには、次の操作を行う必要があります。
埋め込まれたドキュメント子フィールドの親をドキュメントタイプとしてインデックス化します。
埋め込みドキュメント内のstring値を持つ子フィールドをトークンタイプとしてインデックスします。 数値値と日付値を持つ子フィールドの場合は、動的マッピングを有効にして、それらのフィールドを自動的にインデックス化します。
Atlas Search は親ドキュメントのみをソートします。 ドキュメントの配列内の子フィールドはソートされません。 例については、「ソートの例 」を参照してください。
ハイライト
演算子内に指定されたクエリ述語の ドキュメント タイプの親フィールドの下にフィールドがインデックスされている場合は、オンフィールドを 強調表示 できます。embeddedDocument
例については、チュートリアル を参照してください。
embeddedDocument
演算子の制限の詳細については、embeddedDocument
演算子の制限 を参照してください。
例
次の例では、サンプル データセットのsample_supplies.sales
コレクションを使用します。
インデックスの定義
これらのサンプル クエリでは、 コレクションで次のインデックス定義を使用します。
{ "mappings": { "dynamic": true, "fields": { "items": [ { "dynamic": true, "type": "embeddedDocuments" }, { "dynamic": true, "fields": { "tags": { "type": "token" } }, "type": "document" } ], "purchaseMethod": { "type": "token" } } } }
基本クエリ
次のクエリは、 コレクションでschool
タグのアイテムを検索し、 backpack
という名前のアイテムを優先して検索します。 Atlas Search は、一致するすべての埋め込みドキュメントの平均(算術平均)スコアに基づいて、結果を降順でスコア付けします。 クエリには、出力を5
ドキュメントに制限する$limit
ステージと、次の操作を行う$project
ステージが含まれています。
items.name
フィールドとitems.tags
フィールドを除くすべてのフィールドを除外次のフィールドを追加:
score
1 db.sales.aggregate({ 2 "$search": { 3 "embeddedDocument": { 4 "path": "items", 5 "operator": { 6 "compound": { 7 "must": [{ 8 "text": { 9 "path": "items.tags", 10 "query": "school" 11 } 12 }], 13 "should": [{ 14 "text": { 15 "path": "items.name", 16 "query": "backpack" 17 } 18 }] 19 } 20 }, 21 "score": { 22 "embedded": { 23 "aggregate": "mean" 24 } 25 } 26 } 27 } 28 }, 29 { 30 $limit: 5 31 }, 32 { 33 $project: { 34 "_id": 0, 35 "items.name": 1, 36 "items.tags": 1, 37 "score": { $meta: "searchScore" } 38 } 39 })
[ { items: [ { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] } ], score: 1.2907354831695557 }, { items: [ { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ] }, { name: 'printer paper', tags: [ 'office', 'stationary' ] }, { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] } ], score: 1.2907354831695557 }, { items: [ { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] } ], score: 1.2907354831695557 }, { items: [ { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] } ], score: 1.2907354831695557 }, { items: [ { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] } ], score: 1.2907354831695557 } ]
ファセット クエリ
次のクエリは、 school
タグのアイテムを検索し、 backpack
という名前のアイテムを優先します。 purchaseMethod
フィールドのファセット情報をリクエストします。
1 db.sales.aggregate({ 2 "$searchMeta": { 3 "facet": { 4 "operator": { 5 "embeddedDocument": { 6 "path": "items", 7 "operator": { 8 "compound": { 9 "must": [ 10 { 11 "text": { 12 "path": "items.tags", 13 "query": "school" 14 } 15 } 16 ], 17 "should": [ 18 { 19 "text": { 20 "path": "items.name", 21 "query": "backpack" 22 } 23 } 24 ] 25 } 26 } 27 } 28 }, 29 "facets": { 30 "purchaseMethodFacet": { 31 "type": "string", 32 "path": "purchaseMethod" 33 } 34 } 35 } 36 } 37 })
[ { count: { lowerBound: Long("2309") }, facet: { purchaseMethodFacet: { buckets: [ { _id: 'In store', count: Long("2751") }, { _id: 'Online', count: Long("1535") }, { _id: 'Phone', count: Long("578") } ] } } } ]
クエリとソート
次のクエリは、 laptop
という名前のアイテムを検索し、その結果をitems.tags
フィールドでソートします。 クエリには、出力を5
ドキュメントに制限する$limit
ステージと、次の操作を行う$project
ステージが含まれています。
items.name
とitems.tags
を除くすべてのフィールドを除外次のフィールドを追加:
score
1 db.sales.aggregate({ 2 "$search": { 3 "embeddedDocument": { 4 "path": "items", 5 "operator": { 6 "text": { 7 "path": "items.name", 8 "query": "laptop" 9 } 10 } 11 }, 12 "sort": { 13 "items.tags": 1 14 } 15 } 16 }, 17 { 18 "$limit": 5 19 }, 20 { 21 "$project": { 22 "_id": 0, 23 "items.name": 1, 24 "items.tags": 1, 25 "score": { "$meta": "searchScore" } 26 } 27 })
1 [ 2 { 3 items: [ 4 { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ] }, 5 { name: 'binder', tags: [ 'school', 'general', 'organization' ] }, 6 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] }, 7 { name: 'laptop', tags: [ 'electronics', 'school', 'office' ] }, 8 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] }, 9 { name: 'printer paper', tags: [ 'office', 'stationary' ] }, 10 { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] }, 11 { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ] }, 12 { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ] } 13 ], 14 score: 1.168686032295227 15 }, 16 { 17 items: [ 18 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] }, 19 { name: 'binder', tags: [ 'school', 'general', 'organization' ] }, 20 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] }, 21 { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ] }, 22 { name: 'printer paper', tags: [ 'office', 'stationary' ] }, 23 { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ] }, 24 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] }, 25 { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] }, 26 { name: 'laptop', tags: [ 'electronics', 'school', 'office' ] } 27 ], 28 score: 1.168686032295227 29 }, 30 { 31 items: [ 32 { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] }, 33 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] }, 34 { name: 'binder', tags: [ 'school', 'general', 'organization' ] }, 35 { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ] }, 36 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] }, 37 { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ] }, 38 { name: 'laptop', tags: [ 'electronics', 'school', 'office' ] } 39 ], 40 score: 1.168686032295227 41 }, 42 { 43 items: [ 44 { name: 'laptop', tags: [ 'electronics', 'school', 'office' ] }, 45 { name: 'binder', tags: [ 'school', 'general', 'organization' ] }, 46 { name: 'binder', tags: [ 'school', 'general', 'organization' ] }, 47 { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] }, 48 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] }, 49 { name: 'printer paper', tags: [ 'office', 'stationary' ] }, 50 { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ] }, 51 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] }, 52 { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ] }, 53 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] } 54 ], 55 score: 1.168686032295227 56 }, 57 { 58 items: [ 59 { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ] }, 60 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] }, 61 { name: 'notepad', tags: [ 'office', 'writing', 'school' ] }, 62 { name: 'backpack', tags: [ 'school', 'travel', 'kids' ] }, 63 { name: 'envelopes', tags: [ 'stationary', 'office', 'general' ] }, 64 { name: 'pens', tags: [ 'writing', 'office', 'school', 'stationary' ] }, 65 { name: 'binder', tags: [ 'school', 'general', 'organization' ] }, 66 { name: 'laptop', tags: [ 'electronics', 'school', 'office' ] }, 67 { name: 'printer paper', tags: [ 'office', 'stationary' ] }, 68 { name: 'binder', tags: [ 'school', 'general', 'organization' ] } 69 ], 70 score: 1.168686032295227 71 } 72 ]
埋め込みドキュメントのみに一致するクエリ
次のクエリは、クエリに一致するネストされたドキュメントのみを返します。クエリは、 ステージで Atlas Search複合演算子句を使用して一致するドキュメントを検索し、次に $search
$project
ステージの集計演算子を使用して一致する埋め込みドキュメントのみを返します。具体的には、このクエリは次のパイプラインステージを指定します。
複合演算子
| |
出力を | |
1 db.sales.aggregate( 2 { 3 "$search": { 4 "embeddedDocument": { 5 "path": "items", 6 "operator": { 7 "compound": { 8 "must": [ 9 { 10 "range": { 11 "path": "items.quantity", 12 "gt": 2 13 } 14 }, 15 { 16 "exists": { 17 "path": "items.price" 18 } 19 }, 20 { 21 "text": { 22 "path": "items.tags", 23 "query": "school" 24 } 25 } 26 ] 27 } 28 } 29 } 30 } 31 }, 32 { 33 "$limit": 2 34 }, 35 { 36 "$project": { 37 "_id": 0, 38 "storeLocation": 1, 39 "items": { 40 "$filter": { 41 "input": "$items", 42 "cond": { 43 "$and": [ 44 { 45 "$ifNull": [ 46 "$$this.price", "false" 47 ] 48 }, 49 { 50 "$gt": [ 51 "$$this.quantity", 2 52 ] 53 }, 54 { 55 "$in": [ 56 "office", "$$this.tags" 57 ] 58 } 59 ] 60 } 61 } 62 } 63 } 64 } 65 )
1 [ 2 { 3 storeLocation: 'Austin', 4 items: [ 5 { 6 name: 'laptop', 7 tags: [ 'electronics', 'school', 'office' ], 8 price: Decimal128('753.04'), 9 quantity: 3 10 }, 11 { 12 name: 'pens', 13 tags: [ 'writing', 'office', 'school', 'stationary' ], 14 price: Decimal128('19.09'), 15 quantity: 4 16 }, 17 { 18 name: 'notepad', 19 tags: [ 'office', 'writing', 'school' ], 20 price: Decimal128('30.23'), 21 quantity: 5 22 }, 23 { 24 name: 'pens', 25 tags: [ 'writing', 'office', 'school', 'stationary' ], 26 price: Decimal128('20.05'), 27 quantity: 4 28 }, 29 { 30 name: 'notepad', 31 tags: [ 'office', 'writing', 'school' ], 32 price: Decimal128('22.08'), 33 quantity: 3 34 }, 35 { 36 name: 'notepad', 37 tags: [ 'office', 'writing', 'school' ], 38 price: Decimal128('21.67'), 39 quantity: 4 40 } 41 ] 42 }, 43 { 44 storeLocation: 'Austin', 45 items: [ 46 { 47 name: 'notepad', 48 tags: [ 'office', 'writing', 'school' ], 49 price: Decimal128('24.16'), 50 quantity: 5 51 }, 52 { 53 name: 'notepad', 54 tags: [ 'office', 'writing', 'school' ], 55 price: Decimal128('28.04'), 56 quantity: 5 57 }, 58 { 59 name: 'notepad', 60 tags: [ 'office', 'writing', 'school' ], 61 price: Decimal128('21.42'), 62 quantity: 5 63 }, 64 { 65 name: 'laptop', 66 tags: [ 'electronics', 'school', 'office' ], 67 price: Decimal128('1540.63'), 68 quantity: 3 69 }, 70 { 71 name: 'pens', 72 tags: [ 'writing', 'office', 'school', 'stationary' ], 73 price: Decimal128('29.43'), 74 quantity: 5 75 }, 76 { 77 name: 'pens', 78 tags: [ 'writing', 'office', 'school', 'stationary' ], 79 price: Decimal128('28.48'), 80 quantity: 5 81 } 82 ] 83 } 84 ]
詳細
詳細については、「埋め込みドキュメントのフィールドに対してAtlas Searchクエリを実行する方法」を参照してください。