AI エージェント向け: ドキュメントインデックスは https://www.mongodb.com/ja-jp/docs/llms.txt で利用できます。すべてのページの markdown バージョンは、いずれかの URL パスに .md を追加することで利用できます。
Docs Menu

phrase (MongoDB Search 演算子)

phrase

演算子は、 phraseインデックス構成 で指定された アナライザ を使用して、順序付けられたタームのシーケンスを含むドキュメントを検索します。アナライザが指定されていない場合は、デフォルトの標準アナライザ が使用されます。

phrase 演算子を使用して、複数の単語で構成される語句の完全一致を検索できます。

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 では、次の用語を使用してクエリを作成します。

フィールド
タイプ
説明
必要性

query

文字列または複数の文字列の配列

検索する文字列または複数の文字列。

はい

path

文字列または複数の文字列の配列

検索対象となるインデックス付きフィールド。ワイルドカード パスを指定して検索することもできます。

重要: phrase演算子を使用するには、 プロパティをstring indexOptionspositionsまたはデフォルト値の に設定して、クエリフィールドをoffsets としてインデックス必要があります。詳細については、 「string フィールドのプロパティの構成」 を参照してください。

はい

slop

integer

queryフレーズ内の単語間の許容距離。値が小さいほど単語間の位置的距離が少なくなり、値が大きいほど単語の再編成が容易になり、単語間の距離が大きくなり、クエリを満たすことができます。デフォルトは0です。つまり、単語がクエリと完全に同じ位置にある場合にのみマッチとみなされます。完全一致のスコアは高くなります。

no

score

オブジェクト

一致する検索結果に割り当てる スコア 。 デフォルトのスコアは、次のオプションを使用して変更できます。

  • boost: 結果のスコアに指定された数値を掛けます。

  • constant: 結果のスコアを指定された数値に置き換えます。

  • function: 結果のスコアを指定された式で置き換えます。

配列値をクエリする場合、配列内のクエリに一致する値が多いほど、 MongoDB Search はより高いスコアを割り当てます。

デフォルトのスコアを変更するオプションの詳細については、「 結果内のドキュメントのスコアリング 」を参照してください。

no

synonyms

string

シノニム(同意語)を使用してクエリを実行するために必要です。

インデックス定義内のシノニム(同意語)マッピングの定義名。値は空の文字列にできません。

シノニム(同意語)マッピングを使用するクエリをMongoDB Search が実行するのにかかる時間は、 シノニム(同意語)ソースコレクション内のドキュメントの数とサイズによって異なります。

たとえば、非常に少ないシノニムドキュメントに基づいたシノニムマッピングを使うクエリは、多くのシノニムドキュメントに基づいたシノニムマッピングを使うクエリよりも速くなるかもしれません。

任意

デフォルトでは、text 演算子はbm25の類似性アルゴリズムを使用して、クエリとの関連性に応じてドキュメントにスコアを付けます。

MongoDB Search インデックス定義のstringフィールドのsimilarity.typeプロパティで別の類似性アルゴリズムを指定することにより、使用する類似性アルゴリズムを変更できます。string型の MongoDB Search インデックスを設定する方法については、「文字列フィールドのインデックス作成方法」を参照してください。

サポートされている類似性アルゴリズムの詳細については、 「スコアの詳細」を参照してください。

このページの例では、sample_mflix データベース内の movies コレクションを使用します。サンプルデータセット をクラスターにロードしたら、動的マッピングを使用して MongoDB Search インデックスを作成し、クラスターで例クエリを実行します。シノニムの例を試すには、synonymous_terms コレクションsample_mflix データベースに追加し、synonyms マッピングコレクションでインデックスを定義する必要があります。

次の MongoDB Search の例では、title フィールドに対してクエリ文字列 new york の基本的な検索を実行します。クエリに slop がないため、slop の値はデフォルトで 0 になります。つまり、title フィールドには、すべての単語が同じ位置にある正確なクエリ文字列が含まれている必要があり、その場合にのみ結果に含まれます。クエリには、次のものも含まれます。

  • $limit ステージを使用して、出力結果を 10 に制限します。

  • $projectステージでは、 title以外のすべてのフィールドを除外し、 scoreという名前のフィールドを追加します。

次のクエリは、検索条件に一致するドキュメントを返します。

1db.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 の排他的上限

1db.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の例では、titleフィールドでクエリ文字列the manthe moonを検索します。クエリにslopがないため、slopの値はデフォルトで0に設定されます。これは、結果に含めるには、titleにすべての単語が同じ位置にある正確なクエリ文字列が含まれている必要があることを意味します。クエリには、次のものも含まれます。

  • $limit ステージを使用して、出力を 10 に制限します。

  • $projectステージでは、 title以外のすべてのフィールドを除外し、 scoreという名前のフィールドを追加します。

1db.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 womentitleフィールドを検索します。 query5slop 値により、単語の移動が大きくなり、menwomen の間の距離が満たされます。クエリには、次の操作を行うための $project ステージが含まれています。

  • すべてのフィールドを除外。例外: title

  • 次のフィールドを追加: score

1db.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 です。

  • automobilecar、または vehicle は、synonymous_terms コレクションで equivalent のシノニムとして定義されています。

  • racecontest、または rally は、sy``synonymous_terms`` コレクションで explicit シノニムとして定義されています。

MongoDB Search では、car race または vehicle race の検索でも同様の結果が返されます。これは、automobilecarvehicleequivalent シノニムとして設定したためです。ただし、automobile contest の結果には race または rally を持つドキュメントは含まれず、automobile rally の結果には race または contest を持つドキュメントは含まれません。これは、contest を構成しなかったためです。または rally は、任意のタームの同義語になります。