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.
フィールド名の先頭にドル記号(
$)が含まれるフィールドにはインデックスを付けられません。autocomplete フィールドタイプは、大きなインデックスを作成する可能性があり、他のフィールドタイプよりもビルドするのに時間がかかります。カスタム
typeSet定義に含めることで、ダイナミック マッピングでautocomplete型を使用できますが、意図しないパフォーマンス、ストレージ、スコアリングへの影響を避けるため、スタティック マッピングではautocomplete型のみを使用することをお勧めします。さらに学ぶには、「オートコンプリートのためのフィールドのインデックス作成方法」、「MongoDB Search インデックスのパフォーマンスの向上」を参照してください。
構文
The following syntax demonstrates how to enable MongoDB Search to index fields using dynamic and static mappings. To learn more about dynamic and static mappings, see Dynamic and Static Mappings.
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 }
For index examples that demonstrate indexing all fields using the default typeSet, see Dynamic Mapping Examples.
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 で指定したフィールドのみをインデックス化します。静的マッピングを使用して、インデックスの作成からフィールドを除外することもできます。
To use static mappings to configure index options for only some fields, set mappings.dynamic to false and specify the field name, data type, and other configuration options for each field that you want to index. You can specify the fields in any order.
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 }
ネストされたフィールドのインデックスを定義するには、そのネストされたフィールドの各親フィールドのマッピングを定義する必要があります。ドット表記を使用してネストされたフィールドを静的にインデックスことはできません。例については、以下の 例 または 組み合わせマッピングの例 を参照してください。
You can use static mappings to index fields as multiple types. To index a field as multiple types, define the types in the field definition array for the field. You can index any field as any supported types using static mappings. To learn more, see MongoDB Search Field Types.
次の例では、フィールドを複数のタイプとしてインデックスを作成するためのフィールド定義を示しています。
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 }
For other index examples that demonstrate static mappings, see Static Mapping Example.
MongoDB 検索する フィールド タイプ
MongoDB Search doesn't support the following BSON data types:
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.
ダイナミックマッピングの例
次のサンプルインデックス定義は、動的マッピングの使用方法を示しています。
静的マッピングの例
次のサンプルインデックス定義は、静的マッピングを使用する方法を示しています。
複合マッピングの例
次のインデックス定義例では、動的マッピングと静的マッピングを組み合わせています。