定義
構文
phrase
の構文は次のとおりです。
1 { 2 $search: { 3 "index": <index name>, // optional, defaults to "default" 4 "phrase": { 5 "query": "<search-string>", 6 "path": "<field-to-search>", 7 "score": <options>, 8 "slop": <distance-number>, 9 "synonyms": "<synonyms-mapping-name>" 10 } 11 } 12 }
オプション
phrase
では、次の用語を使用してクエリを作成します。
フィールド | タイプ | 説明 | 必要性 |
---|---|---|---|
| 文字列または複数の文字列の配列 | 検索する文字列または複数の文字列。 | はい |
| 文字列または複数の文字列の配列 | 検索対象となるインデックス付きフィールド。ワイルドカード パスを指定して検索することもできます。 重要: | はい |
| integer |
| no |
| オブジェクト | 一致する検索結果に割り当てる スコア 。 デフォルトのスコアは、次のオプションを使用して変更できます。
配列値をクエリする場合、 MongoDB Search は、配列内のクエリに一致する値の数に関係なく、同じスコアを割り当てます。 デフォルトのスコアを変更するオプションの詳細については、「 結果内のドキュメントのスコアリング 」を参照してください。 | no |
| string | シノニム(同意語)を使用してクエリを実行するために必要です。 インデックス定義内のシノニム(同意語)マッピングの定義名。値は空の文字列にできません。 シノニム(同意語)マッピングを使用するクエリをMongoDB Search が実行するのにかかる時間は、 シノニム(同意語)ソースコレクション内のドキュメントの数とサイズによって異なります。 たとえば、非常に少ないシノニムドキュメントに基づいたシノニムマッピングを使うクエリは、多くのシノニムドキュメントに基づいたシノニムマッピングを使うクエリよりも速くなるかもしれません。 | 任意 |
例
このページの例では、 sample_mflix
データベース内の movies
コレクションを使用します。サンプルデータセット をクラスターにロードしたら、動的マッピングを含むMongoDB Searchインデックスを作成し、クラスターで例クエリを実行します。シノニム(同意語)の例を試すには、同意語_タームコレクションを sample_mflix
データベースに追加し、 synonyms
マッピングコレクションを使用してインデックスを定義する必要があります。
単一フレーズの例
MongoDB Search の次の例では、クエリ文字列new york
の title
フィールドの基本検索を実行します。 クエリに slop
がないため、slop
値はデフォルトで 0
になります。つまり、結果に含めるには、単語の位置がクエリ文字列と完全に一致している必要があることを意味します。 クエリには次の要素も含まれます。
基本的な例
次のクエリは、検索条件に一致するドキュメントを返します。
1 db.movies.aggregate([ 2 { 3 "$search": { 4 "phrase": { 5 "path": "title", 6 "query": "new york" 7 } 8 } 9 }, 10 { $limit: 10 }, 11 { 12 $project: { 13 "_id": 0, 14 "title": 1, 15 score: { $meta: "searchScore" } 16 } 17 } 18 ])
1 [ 2 { title: 'New York, New York', score: 6.786321640014648 } 3 { title: 'New York', score: 6.258549213409424 } 4 { title: 'New York Stories', score: 5.3813982009887695 } 5 { title: 'New York Minute', score: 5.3813982009887695 } 6 { title: 'Synecdoche, New York', score: 5.3813982009887695 } 7 { title: 'New York Doll', score: 5.3813982009887695 } 8 { title: 'Little New York', score: 5.3813982009887695 } 9 { title: 'Escape from New York', score: 4.719893455505371 } 10 { title: 'Naked in New York', score: 4.719893455505371 } 11 { title: 'Autumn in New York', score: 4.719893455505371 } 12 ]
メタデータの例
次のクエリは、検索条件に一致するメタデータの結果を返します。このクエリでは $searchMeta
ステージを使用して、次のバケット(年)内で検索条件に一致する映画数を取得します。
2000 (このバケットの下限値を含む)
2005: 2000 バケットの 排他的上限であり、このバケットの下限
2010: 2005 バケットの 排他的上限であり、このバケットの下限
2010: 2010 の排他的上限
例
1 db.movies.aggregate([ 2 { 3 "$searchMeta": { 4 "facet": { 5 "operator": { 6 "phrase": { 7 "path": "title", 8 "query": "new york" 9 } 10 }, 11 "facets": { 12 "yearFacet": { 13 "type": "number", 14 "path": "year", 15 "boundaries": [2000, 2005, 2010, 2015 ] 16 } 17 } 18 } 19 } 20 } 21 ])
1 [ 2 { 3 count: { lowerBound: Long('27') }, 4 facet: { 5 yearFacet: { 6 buckets: [ 7 { _id: 2000, count: Long('3') }, 8 { _id: 2005, count: Long('8') }, 9 { _id: 2010, count: Long('4') } 10 ] 11 } 12 } 13 } 14 ]
この結果からは、 2000 と 2005 の間には 3 件の映画、 2005 と 2010 の間には 8 件の映画、2010 と 2015 の間には title
に用語new york
を含む 4 件の映画があることがわかります。
複数フレーズの例
MongoDB Search の次の例では、クエリ文字列 the man
と the
moon
の title
フィールドで基本的な検索を実行します。クエリに slop
がないため、slop
値はデフォルトで 0
になります。つまり、結果に含めるには、単語の位置がクエリ文字列と完全に一致している必要があることを意味します。 クエリには次の要素も含まれます。
1 db.movies.aggregate([ 2 { 3 "$search": { 4 "phrase": { 5 "path": "title", 6 "query": ["the man", "the moon"] 7 } 8 } 9 }, 10 { $limit: 10 }, 11 { 12 $project: { 13 "_id": 0, 14 "title": 1, 15 score: { $meta: "searchScore" } 16 } 17 } 18 ])
1 [ 2 { title: 'The Man in the Moon', score: 4.4830474853515625 }, 3 { title: 'Shoot the Moon', score: 3.252699851989746 }, 4 { title: 'Kick the Moon', score: 3.252699851989746 }, 5 { title: 'The Man', score: 2.8923356533050537 }, 6 { title: 'The Moon and Sixpence', score: 2.8528637886047363 }, 7 { title: 'The Moon Is Blue', score: 2.8528637886047363 }, 8 { title: 'Racing with the Moon', score: 2.8528637886047363 }, 9 { title: 'Mountains of the Moon', score: 2.8528637886047363 }, 10 { title: 'Man on the Moon', score: 2.8528637886047363 }, 11 { title: 'Castaway on the Moon', score: 2.8528637886047363 } 12 ]
スローの例
MongoDB Search の次の例では、クエリ文字列men women
の title
フィールドを検索します。 query
の 5
の slop
値により、単語の移動が大きくなり、men
と women
の間の距離が満たされます。クエリには、次の操作を行うための $project
ステージが含まれています。
すべてのフィールドを除外。例外:
title
次のフィールドを追加:
score
1 db.movies.aggregate([ 2 { 3 "$search": { 4 "phrase": { 5 "path": "title", 6 "query": "men women", 7 "slop": 5 8 } 9 } 10 }, 11 { 12 $project: { 13 "_id": 0, 14 "title": 1, 15 score: { $meta: "searchScore" } 16 } 17 } 18 ])
1 [ 2 { title: 'Men Without Women', score: 3.367523193359375 }, 3 { title: 'Men Vs Women', score: 3.367523193359375 }, 4 { title: 'Good Men, Good Women', score: 2.8529787063598633 }, 5 { title: 'The War Between Men and Women', score: 2.1851978302001953 }, 6 { title: 'Women Without Men', score: 1.9656763076782227 }, 7 { title: 'Women Vs Men', score: 1.9656763076782227 } 8 ]
シノニム(同意語)の例
次のクエリは、movies
コレクションの plot
フィールドで、指定されたクエリ文字列内のタームを検索します。 MongoDB Search では、 シノニム(同意語)ソースコレクション内の synonymous_terms
マッピング タイプ (sample_mflix.movies
)に基づき結果が返されます。このマッピング タイプは コレクションのインデックスのシノニム マッピング定義で指定されます。
次のクエリは、plot
フィールドの任意の場所で、単語間の距離が 5
までの範囲で automobile race
というフレーズを検索します。
db.movies.aggregate([ { $search: { "phrase": { "path": "plot", "query": "automobile race", "slop": 5, "synonyms": "my_synonyms" } } }, { "$limit": 5 }, { $project: { "_id": 0, "plot": 1, "title": 1, score: { $meta: "searchScore" } } } ])
[ { plot: 'When a popular daredevil proposes an automobile race across three continents, his arch rival vows to beat him, while an ambitious female reporter has her own plans for victory.', title: 'The Great Race', score: 29.569732666015625 }, { plot: 'A wide variety of eccentric competitors participate in a wild and illegal cross-country car race.', title: 'The Cannonball Run', score: 25.50379180908203 }, { plot: 'A mechanic takes his family to a car race and a series of events occur which brings problems, betrayals, violence and the unexpected death of an elderly person.', title: 'National Mechanics', score: 21.538257598876953 }, { plot: "It's time for the annual London to Brighton antique car rally, and Alan McKim and Ambrose Claverhouse are not going to let their friendship stop them from trying to humiliate each other. ...", title: 'Genevieve', score: 20.19266128540039 }, { plot: "A naive drifter runs away from his army father in hopes of making it on the car racing circuit. In Las Vegas, he meets a young scam artist, who develops a crush on him. He is then ...", title: 'Speedway Junky', score: 18.639965057373047 } ]
結果に含まれる文書の plot
フィールドに次の用語が含まれており、用語間の距離は最大で 5
です。
automobile
、car
、またはvehicle
は、synonymous_terms
コレクションでequivalent
のシノニムとして定義されています。race
、contest
、またはrally
は、sy``synonymous_terms`` コレクションでexplicit
シノニムとして定義されています。
MongoDB Search では、car race
または vehicle
race
の検索でも同様の結果が返されます。これは、automobile
、car
、vehicle
を equivalent
シノニムとして設定したためです。ただし、automobile
contest
の結果には race
または rally
を持つドキュメントは含まれず、automobile rally
の結果には race
または contest
を持つドキュメントは含まれません。これは、contest
を構成しなかったためです。または rally
は、任意のタームの同義語になります。