keyword
アナライザは、string または string の配列をパラメータとして受け入れ、それらを単一のターム(トークン)としてインデックスします。 フィールドの完全一致のみが返されます。 すべてのテキストは元の文字大文字と小文字のままになります。
Refine Your Index を選択した場合、Atlas UI はIndex Configurations セクション内に View text analysis of your selected index configuration というタイトルのセクションを表示します。このセクションを展開すると、Atlas UI には、keyword
アナライザが各サンプル列に対して生成するインデックスと検索トークンが表示されます。 Atlas UI Visual Editor でインデックスを作成または編集すると、keyword
アナライザが組み込みサンプルドキュメントとクエリ文字列用に作成するトークンが確認できます。
重要
MongoDB Search は、アナライザトークンのサイズが 32766 バイトを超える、string フィールドのインデックスません。キーワードアナライザを使用している場合、32766 バイトを超える string フィールドはインデックス化されません。
例
次のインデックス定義の例では、 アナライザを使用して、 title
sample_mflix.moviesコレクションの フィールドのインデックスを指定します。この例に従うには、クラスターにサンプルデータをロードし、keyword
を使用するか、 mongosh
Create a Search
IndexMongoDB Search インデックスの作成 チュートリアルの手順に従って Atlas UIの ページに移動します。
次に、movies
コレクションをデータソースとして使用し、 または Atlasmongosh
UIVisual Editor またはJSON editor からインデックスを作成する例の手順に従います。
➤ [言語の選択] ドロップダウン メニューを使用して、このページの例の言語を設定します。
インデックスを設定するには、 Refine Your Indexをクリックします。
Index Configurations セクションで、Dynamic Mapping を off に切り替えます。
Field Mappingsセクションで、 Add FieldをクリックしてAdd Field Mappingウィンドウを開きます。
[Customized Configuration] をクリックします。
Field Nameドロップダウンから
title
を選択します。[ Data Type String選択されていない場合は選択します。
String Propertiesを展開し、次の変更を加えます。
インデックスアナライザ
ドロップダウンから [
lucene.keyword
] を選択します。searchAnalyzer
ドロップダウンから [
lucene.keyword
] を選択します。インデックス オプション
デフォルトの
offsets
を使用します。Store
デフォルトの
true
を使用します。上記を無視
デフォルト設定のままにしてください。
基準
デフォルトの
include
を使用します。[Add] をクリックします。
[Save Changes] をクリックします。
[Create Search Index] をクリックします。
デフォルトのインデックス定義を、以下のインデックス定義で置き換えます。
{ "mappings": { "fields": { "title": { "type": "string", "analyzer": "lucene.keyword" } } } } [Next] をクリックします。
[Create Search Index] をクリックします。
1 db.movies.createSearchIndex( 2 "default", 3 { 4 "mappings": { 5 "fields": { 6 "title": { 7 "type": "string", 8 "analyzer": "lucene.keyword" 9 } 10 } 11 } 12 } 13 )
次のクエリは、 title
フィールドでフレーズClass Action
を検索します。
インデックスの Query ボタンをクリックします。
クエリを編集するには、Edit Query をクリックします。
クエリ バーをクリックし、データベースとコレクションを選択します。
デフォルトのクエリを以下のように置き換え、Find をクリックします。
[ { "$search": { "text": { "query": "Class Action", "path": "title" } } } ] SCORE: 4.346973419189453 _id: "573a1399f29313caabcec6b7" awards: Object cast: Array (4) countries: Array (1) directors: Array (1) fullplot: "Jeb Ward is an attorney who specializes in whistle blower, David vs. G…" genres: Array (2) imdb: Object languages: Array (1) lastupdated: "2015-09-06 00:42:51.167000000" metacritic: 58 num_mflix_comments: 2 plot: "Jeb Ward is an attorney who specializes in whistle blower, David vs. G…" poster: "https://m.media-amazon.com/images/M/MV5BNWY5Mjk4ZmItMTAzYS00NWE3LWEzYz…" rated: "R" released: 1991-03-15T00:00:00.000+00:00 runtime: 110 title: "Class Action" tomatoes: Object type: "movie" writers: Array (3) year: 1991
db.movies.aggregate([ { "$search": { "text": { "query": "Class Action", "path": "title" } } }, { "$project": { "_id": 0, "title": 1 } } ])
[ { title: 'Class Action' } ]
MongoDB Search がドキュメントを返すのは、lucene.keyword
アナライザを使用して、クエリタームClass
Action
が、フィールドのテキスト用に作成される単一のトークン Class Action
と照合したためです。対照的に、 MongoDB Search では、次のクエリの結果は返されません。
db.cases.aggregate([ { "$search": { "text": { "query": "action", "path": "title" } } } ])
コレクション内の多くのドキュメントには string action
が含まれていますが、keyword
アナライザは検索タームがフィールドの内容全体に完全に一致するドキュメントのみに一致させます。上記のクエリでは、keyword
アナライザは結果を返しません。ただし、標準アナライザ または シンプルアナライザ を使用してフィールドをインデックスすると、 MongoDB Search は結果内にタイトルフィールド値が Class
Action
であるドキュメントを含む複数のドキュメントを返します。これは、次のようなトークンを作成するためです。次に、クエリタームに一致させます。
analyzer | 出力トークン | マッチ action | マッチ Class Action |
---|---|---|---|
キーワード アナライザ トークン |
| X | ✓ |
標準アナライザ トークン |
| ✓ | ✓ |
簡易アナライザ トークン |
| ✓ | ✓ |