MongoDB 検索インデックスを作成するときに、次の方法を使用してインデックスを作成するフィールドを指定できます。
動的マッピング: MongoDB Search を有効にして、デフォルトまたは設定されたフィールドタイプ(
typeSet)に基づいてすべてのフィールドを自動的にインデックス化します。静的マッピング:インデックスを作成するフィールドを指定できるようにします。
データ型の制限事項
MongoDB Search はデフォルトで、レプリカセットまたは単一のシャードで、インデックス オブジェクトが 2,100,000,000 を超えるインデックスについて変更のレプリケーションを停止します。この場合、各インデックス作成されたドキュメントまたはネストされた
embeddedDocumentは 1 つのオブジェクトとしてカウントされます。つまり、インデックスはクエリ可能ですが、古い結果が返される可能性があります。If you plan to index fields that might exceed 2,100,000,000 objects, where an index object is a top-level document or an embedded document, use the numPartitions index option to partition your index (supported only on Search Nodes deployment) or shard your cluster.
フィールド名の先頭にドル記号(
$)が含まれるフィールドにはインデックスを付けられません。The autocomplete field type can create large indexes and take longer to build than other field types. While you can use the
autocompletetype in dynamic mappings by including it in a customtypeSetdefinition, we recommend using theautocompletetype in static mappings only to avoid unintended performance, storage, and scoring implications. To learn more, see How to Index Fields for Autocompletion and Improve MongoDB Search Index Performance.
構文
次の構文は、MongoDB Searchを有効にして、動的マッピングと静的マッピングを使用してフィールドをインデックス化する方法を示しています。動的マッピングと静的マッピングの詳細については、「動的マッピングと静的マッピング」を参照してください。
1 { 2 "mappings": { 3 "dynamic": true|false | { 4 "typeSet": "<typeset-name>" 5 }, 6 "fields": { 7 "<field-name>": { 8 "type": "<field-type>", 9 ... 10 }, 11 ... 12 } 13 }, 14 "typeSets": [ 15 { 16 "name": "<typeset-name>", 17 "types": [ 18 { 19 "type": "<field-type>", 20 ... 21 }, 22 ... 23 ] 24 }, 25 ... 26 ] 27 }
動的マッピングと静的マッピング
MongoDB Search は、次のコマンドを使用して構成できます。
デフォルトまたは構成されたタイプのセットに基づいてフィールドを自動的にインデックスための 動的マッピング (
typeSet)インデックスされたフィールドのみへの静的マッピング
You can also use dynamic mappings with static mappings. Static mappings override the dynamic mappings configuration. When you define a static mapping for a field, MongoDB Search indexes that field only according to the static mapping definition. The dynamic mapping no longer applies to that field.
例えば、dynamic: trueを有効にすると、MongoDB Search はデフォルトで string フィールドを string タイプとしてインデックスします。その後、特定の string フィールドに対して静的な autocomplete マッピングを追加すると、MongoDB Search はそのフィールドを autocomplete タイプとしてのみインデックスします。静的マッピングに string を明示的に含めない限り、string としてもインデックスされません。フィールドを両方のタイプでインデックスするには、フィールドの静的マッピング配列で両方のタイプを定義する必要があります。
ダイナミックマッピング
MongoDB Search の動的マッピングを使用すると、データ内のフィールドに自動的かつ再帰的にインデックスを付けられるように MongoDB Search を構成できます。フィールドは、デフォルトのタイプセットに基づいて、または typeSet を構成することでインデックス化できます。
動的マッピングを有効または構成できます。
ドキュメント全体に適用するルート
mappingsレベル。[推奨] 指定されたオブジェクトに適用する
documentフィールドタイプ内で。[推奨] フィールドタイプは、要素単位のクエリ比較を必要とするオブジェクトの指定された配列に適用するために、
embeddedDocumentsフィールドタイプ内で使用します($elemMatchと同様)。
注意
ベストプラクティス
ダイナミックマッピングでは、多くの一意のフィールドのインデックスが作成される可能性があり、ディスク領域を多く占有し、パフォーマンスが低下する可能性があります。ダイナミックマッピングは、定期的に変更されるフィールドや不明なフィールドのインデックスを作成する必要がある場合にのみ使用します。ダイナミックマッピングは、常にドキュメント内で使用し、親ドキュメントレベルでは使用しないでください。
Tip
動的マッピング を使用してデータをインデックスする場合、
MongoDB Search は、データ内の
documentオブジェクト内のtypeSetによってサポートされているすべてのネストされたフィールドも動的にインデックス化します。フィールドに多形データが含まれている場合、MongoDB Search はインデックスで使用される
typeSetでサポートされているすべての型としてフィールドを自動的にインデックス化します。フィールドにtypeSetでサポートされていない型のデータが含まれている場合、MongoDB Search はそのデータをインデックス化しません。
デフォルトを有効 typeSet
MongoDB Search uses the default typeSet when dynamic is set to true. In the default typeSet, MongoDB Search indexes the BSON types as MongoDB Search field types. The following table shows the BSON types that MongoDB Search automatically indexes as MongoDB Search field types when you use the default typeSet. MongoDB Search also automatically indexes the following BSON types when they are contained within arrays and objects.
BSON 型 | MongoDB Search フィールド タイプ |
|---|---|
ブール値 | |
日付 | |
Double、32ビット整数、64ビット整数 | |
ObjectId | |
文字列 | |
UUID | |
null |
以下は、動的マッピングでデフォルトのタイプセットを有効にするための構文です。
1 { 2 "mappings": { 3 "dynamic": true 4 } 5 }
デフォルトのtypeSet を使用してすべてのフィールドにインデックスを作成するインデックス例については、動的マッピングの例 を参照してください。
configurea typeSet
Specify the MongoDB Search field types to index dynamically by configuring a typeSet. You can configure any BSON type to be automatically indexed as any MongoDB Search field type except document, embeddedDocuments, vector, or deprecated field types.
typeSet の構文は次のとおりです。
注意
Atlas UI Visual Editor から typeSet を設定することはできません。代わりに Atlas UI JSON Editor を使用してください。
1 { 2 "mappings": { 3 "dynamic": { 4 "typeSet": "<typeset-name>" 5 }, 6 "fields": { 7 "<field-name>": { 8 "type": "<field-type>", 9 ... 10 }, 11 ... 12 } 13 }, 14 "typeSets": [ 15 { 16 "name": "<typeset-name>", 17 "types": [ 18 { 19 "type": "<field-type>" 20 }, 21 ... 22 ] 23 }, 24 ... 25 ] 26 }
typeSet を設定する前に、以下の点を考慮してください。
同じ
typeSetオブジェクト内で、同じフィールドタイプを複数回定義することはできません。各フィールド型に対して 1 つのtypeSet定義のみを構成できます。例、同じ
typeSet定義内でnumberタイプに複数の構成を定義することはできません。multiアナライザを使用して動的マッピングを構成できます。
カスタム typeSet 構成の使用を示すインデックスの例については、動的マッピングの例 を参照してください。
静的マッピング
静的マッピングを使用して、動的にインデックスを作成 したくない フィールドのインデックスオプションを設定したり、1 つのフィールドをインデックス内の他のフィールドと独立して構成したりします。静的マッピングを使用する場合、 MongoDB Search は mappings.fields で指定したフィールドのみをインデックス化します。静的マッピングを使用して、インデックスの作成からフィールドを除外することもできます。
静的マッピングを使用して一部のフィールドのみのインデックスオプションを構成するには、mappings.dynamic を false に設定し、インデックスを作成するフィールドごとにフィールド名、データ型 などの構成オプションを指定します。任意の順序でフィールドを指定できます。
mappings.dynamic フィールドを省略すると、デフォルトで false になります。
1 { 2 "mappings": { 3 "dynamic": true|false, 4 "fields": { 5 "<field-name>": { 6 "type": "<field-type>", 7 ... 8 }, 9 ... 10 } 11 } 12 }
ネストされたフィールドのインデックスを定義するには、そのネストされたフィールドの各親フィールドのマッピングを定義する必要があります。ドット表記を使用してネストされたフィールドを静的にインデックスことはできません。例については、以下の 例 または 組み合わせマッピングの例 を参照してください。
静的マッピングを使用して、フィールドを複数のタイプとしてインデックスできます。フィールドを複数のタイプとしてインデックスには、フィールドのフィールド定義配列でタイプを定義します。静的マッピングを使用して、サポートされているタイプとして任意のフィールドをインデックスできます。詳しくは、 MongoDB Search フィールド タイプ を参照してください。
次の例では、フィールドを複数のタイプとしてインデックスを作成するためのフィールド定義を示しています。
1 { 2 "mappings": { 3 "dynamic": true|false | { 4 "typeSet": "<type-set-name>" 5 }, 6 "fields": { 7 "<field-name>": [ 8 { 9 "type": "<field-type>", 10 ... 11 }, 12 { 13 "type": "<field-type>", 14 ... 15 }, 16 ... 17 ], 18 ... 19 } 20 }. 21 "typeSets": [ 22 { 23 "name": "<typeset-name>", 24 "types": [ 25 { 26 "type": "<field-type>" 27 }, 28 ... 29 ] 30 }, 31 ... 32 ] 33 }
静的マッピングを示すその他のインデックス例については、静的マッピングの例を参照してください。
MongoDB 検索する フィールド タイプ
MongoDB Search は次のBSONデータ型をサポートしていません。
Decimal128
JavaScript コード(スコープ付き)
Max key
Min key
正規表現
タイムスタンプ
MongoDB Search automatically stores fields of type string on mongot. You can store fields of all supported data types on MongoDB Search using the Define Stored Source Fields in Your MongoDB Search Index option in your index definition. To learn more about mongot and MongoDB Search node architecture, see MongoDB Search Deployment Options.
The following table enumerates the supported BSON data types and the MongoDB Search field types that you can use to index the BSON data types. The table also lists the operators and collectors that you can use to query the field values.
BSON 型 | MongoDB Search フィールド タイプ | 演算子とコレクター |
|---|---|---|
配列内のデータ型をサポートする演算子。 | ||
ブール値 | ||
日付 | ||
日付 | ||
Double | ||
Double | ||
Double | ||
32 ビット整数 | ||
32 ビット整数 | ||
64 ビット整数 | ||
64 ビット整数 | ||
null | 該当なし | |
オブジェクト | オブジェクト内のフィールド型をサポートする演算子。 | |
オブジェクト | 埋め込みドキュメント(オブジェクトの配列用) | |
ObjectId | ||
文字列 | ||
文字列 | ||
文字列 | ||
文字列 | ||
ベクトル |
これらの演算子は文字列の配列をサポートしていません。
MongoDB Search は、静的にインデックスが付けられたフィールドと動的にインデックスが付けられたフィールドの両方の null 値を自動的にインデックス化するため、 MongoDB Search には null 値をインデックス化するためのフィールドタイプが含まれていません。
非推奨。非推奨のファセットフィールド型とその更新された対応タイプの詳細については、ファセットのフィールド型の比較を参照してください。
double または int の配列。
注意
You can store fields of all supported data types on MongoDB Search using the storedSource option.
例
The following sample index definitions on the sample_mflix.movies collection demonstrate how to index fields using dynamic and static mappings. You can load the sample data on your cluster to try the examples. To learn more about how to create MongoDB Search indexes, see MongoDB Search Quick Start.
ダイナミックマッピングの例
次のサンプルインデックス定義は、動的マッピングの使用方法を示しています。
次のインデックス定義。
- ルートレベルでの動的マッピングを有効にし、
only_stringsという名前のtypeSet定義に基づいてコレクション内のフィールドを自動的にインデックスます。これにより、positionsインデックスオプションを使用し、storeをfalseに設定します。 。これらの構成オプションでは、クエリ強調表示用にフィールド値またはタームオフセットを保存しないため、ディスク領域が節約されます。
{ "mappings": { "dynamic": { "typeSet": "only_strings" } }, "typeSets": [ { "name": "only_strings", "types": [ { "type": "string", "store": false, "indexOptions": "positions" } ] } ] }
次のインデックス定義。
ルートレベルでの動的マッピングを有効にし、
only_stringsという名前のtypeSetに基づいてコレクション内のフィールドを動的にインデックス。これにより、positionsインデックスオプションとstoreがfalseに設定され、コレクション内の string フィールドにインデックスが作成されます。クエリ強調表示用のフィールド値またはタームオフセットを保存しないため、ディスク領域が節約されます。awardsフィールドをdocumentタイプとしてインデックス化し、only_numberstypeSetとdynamicオプションを併用して、awardsドキュメントの数値サブフィールドのみを動的にインデックスます。つまり、数値サブフィールドwinsとnominationsはインデックス付きですが、string サブフィールドtextはインデックス化されていません。
{ "mappings": { "dynamic": { "typeSet": "only_strings" }, "fields": { "awards": { "type": "document", "dynamic": { "typeSet": "only_numbers" } } } }, "typeSets": [ { "name": "only_numbers", "types": [ { "type": "number" } ] }, { "name": "only_strings", "types": [ { "type": "string", "store": false, "indexOptions": "positions" } ] } ] }
次のインデックス定義。
以下の動作を指定する
indexedTypesという名前のtypeSetを使用して、特定のフィールドタイプの動的マッピングを構成します。string フィールドを
tokenタイプとして自動的にインデックス。数値フィールドを
numberタイプとして自動的にインデックス。
plotフィールドをインデックス作成から除外します。
{ "mappings": { "dynamic": { "typeSet": "indexedTypes" }, "fields": { "plot": [] } }, "typeSets": [ { "name": "indexedTypes", "types": [ { "type": "token" }, { "type": "number" } ] } ] }
静的マッピングの例
次のサンプルインデックス定義は、静的マッピングを使用する方法を示しています。
次のインデックス定義。
ルート レベルで静的フィールドマッピングを指定します(
dynamic:false)。つまり、mappings.fieldsで指定されていないフィールドにはインデックスが付けられません。インデックス定義には、インデックスの作成用の次のフィールドが明示的に含まれています。タイプ
documentであるawardsフィールド。埋め込まれたサブフィールドには、wins、nominations、textの 3 つがあります。winsとnominationsのサブフィールドには数値データが含まれており、number型としてインデックス付けされています。nominationsはint64表現でインデックス付けされているため、デフォルトのnumber型よりも大きい整数値をサポートできます。textサブフィールドには string データが含まれており、英語のテキストに最適化された lucene.english アナライザ を使用してstring型としてインデックス付けされます。また、textフィールドではignoreAboveオプションを使用して、長さが 255 文字を超える文字列を無視します。string データを含む
titleフィールドは、製品名やタイトルなどの多くの空白を含むテキストに最適化されている lucene.whitespace アナライザ を使用してタイプstringとしてインデックス付けされます。titleフィールドはマルチオプションを使用してlucene.frenchアナライザをセカンダリアナライザとして指定します。これにより、$searchクエリでpathとして{ "value": "title", "multi": "frenchAnalyzer" }を指定し、フランス語のクエリ用語をよりターゲットにできます。文字列の配列を含み、タイプ
stringとしてインデックス付けされるgenresフィールド。MongoDB Search は、デフォルトで lucene.standard アナライザを使用して配列要素をインデックス化します。配列のインデックスの作成において、 MongoDB Search では配列要素のデータ型のみが必要です。インデックス定義でデータが配列に含まれていることを指定する必要はありません。
{ "mappings": { "dynamic": false, "fields": { "awards": { "type": "document", "fields": { "wins": { "type": "number" }, "nominations": { "type": "number", "representation": "int64" }, "text": { "type": "string", "analyzer": "lucene.english", "ignoreAbove": 255 } } }, "title": { "type": "string", "analyzer": "lucene.whitespace", "multi": { "frenchAnalyzer": { "type": "string", "analyzer": "lucene.french" } } }, "genres": { "type": "string" } } } }
複合マッピングの例
次のインデックス定義例では、動的マッピングと静的マッピングを組み合わせています。
このインデックス定義:
静的フィールドマッピングを指定します(
dynamic:false)。つまり、明示的に指定されていないフィールドとフィールド型はインデックス化されません。したがって、インデックス定義には次のものが含まれます。titlestring データを含むstringフィールドは、製品名やタイトルなど、多くの空白を含むテキストに最適化されている lucene.whitespaceアナライザ を使用して タイプとしてインデックス付けされます。タイプ
documentで、3 つのサブフィールドを含むawardsフィールド。winsとnominationsサブフィールドには数値データが含まれ、textサブフィールドには string データが含まれています。ドキュメント内のネストされた各フィールドのデータ型を明示的に設定する代わりに、インデックス定義により、ドキュメント内のすべてのサブフィールドの動的なマッピングが可能になります。これにより、winsとnominationsはnumberタイプとして、textはstringタイプとしてインデックス化されます。textフィールドのデータは、デフォルトでlucene.standardアナライザを使用してインデックス化されます。
{ "mappings": { "dynamic": false, "fields": { "title": { "type": "string", "analyzer": "lucene.whitespace" }, "awards": { "type": "document", "dynamic": true } } } }
このインデックス定義:
ルート レベルで静的フィールドマッピングを指定します(
dynamic:false)。つまり、mappings.fieldsオプションで指定されていないフィールドにはインデックスが付けられません。movieAwardsという名前のカスタムtypeSetを使用して、documentタイプであるawardsフィールドの動的マッピングを定義します。awardsフィールドには、wins、nominations、textの 3 つのサブフィールドが埋め込まれています。ルートレベルでdynamicをtrueに設定してサブフィールドを自動的にインデックスの作成するか、静的マッピングを使用してネストされた各フィールドを明示的にインデックスの作成することで、サブフィールドを自動的にインデックスの作成する代わりに、インデックス定義はtypeSetを使用してdocumentフィールドタイプの動的マッピングを構成しますmovieAwardsという名前の定義。movieAwardstypeSetは次の処理を行います。マルチオプションを使用して複数のアナライザを指定し、string フィールドを
stringタイプとしてインデックスします。multiオプションを使用して:lucene.englishとlucene.frenchnumber型のデフォルト設定を使用して、数値フィールドをnumberとしてインデックスします。{ "mappings": { "dynamic": false, "fields": { "awards": { "type": "document", "dynamic": { "typeSet": "movieAwards" } } } }, "typeSets": [ { "name": "movieAwards", "types": [ { "type": "string", "multi": { "english": { "type": "string", "analyzer": "lucene.english" }, "french": { "type": "string", "analyzer": "lucene.french" } } }, { "type": "number" } ] } ] }