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

結果で検索タームを強調表示

MongoDB Search highlight オプションは、元のコンテキストで検索タームを表示するフィールドを結果セットに追加します。この演算子をすべての $search 演算子と組み合わせて使用すると、返されたドキュメントに表示される検索タームと、それに続くテキスト コンテンツ(存在する場合)を表示できます。highlight の結果は $metaフィールドの一部として返されます。

MongoDB Search highlight オプションは、embeddedDocument 演算子と組み合わせて使用できません。

highlight の構文は次のとおりです。

{
$search: {
"index": "<index name>", // optional, defaults to "default"
"<operator>": { // such as "text", "compound", or "phrase"
<operator-specification>
},
"highlight": {
"path": "<field-to-search>",
"maxCharsToExamine": "<number-of-chars-to-examine>", // optional, defaults to 500,000
"maxNumPassages": "<number-of-passages>" // optional, defaults to 5
}
}
},
{
$project: {
"highlights": { "$meta": "searchHighlights" }
}
}
フィールド
タイプ
説明
必須

path

string

検索するドキュメント フィールド。 pathフィールドには以下の値を含めることができます。

  • A string

  • 文字列の配列

  • 文字列とマルチアナライザの仕様の組み合わせを含む配列

  • ワイルドカード文字 *

はい

maxCharsToExamine

整数

フィールドを強調表示するときにドキュメントで確認する最大文字数。省略した場合、デフォルトは 500,000 になります。つまり、 MongoDB Search は強調表示する各ドキュメントの検索フィールドの最初の 500,000 文字のみを検査します。

no

maxNumPassages

整数

各フィールドの highlights 結果で返されるドキュメントごとの高スコア パスの数。通過は、ほぼ文章の長さです。省略した場合、デフォルトは 5 になります。つまり、各ドキュメントに対して、 MongoDB Search は、検索テキストに一致する上位の 5 の高スコア パスを返します。

no

The "$meta": "searchHighlights" field contains the highlighted results. That field isn't part of the original document, so it is necessary to use a $project pipeline stage to add it to the query output.

highlightsフィールドは、次の出力フィールドを含む配列です。

フィールド
タイプ
説明

path

string

一致を返したドキュメント フィールド。

texts

ドキュメントの配列

各検索一致は、一致するテキストとその周囲のテキスト(存在する場合)を含む 1 つ以上の オブジェクトを返します。

texts.value

string

一致を返したフィールドのテキスト。

texts.type

string

結果のタイプ。 値は次のいずれかになります。

  • hit - 結果にはクエリに一致した というタームが含まれます。

  • text - 結果には、一致するタームに隣接するテキスト コンテンツが含まれます。

score

float

一致する結果に割り当てられたスコアhighlightsスコアは、クエリに対するhighlightsオブジェクトの関連性の測定値です。 複数のhighlightsオブジェクトが返された場合、最も関連性の高いhighlightsオブジェクトが最もスコアが高くなります。

indexOptionsoffsets に設定(デフォルト)して、ハイライトするフィールドをMongoDB Search string タイプとしてインデックス必要があります。

次の例は、 MongoDB Search Playground または クラスターで試すことができます。

このページの例では、次のドキュメントを含むfruitというコレクションを使用します。

{
"_id" : 1,
"type" : "fruit",
"summary" : "Apple varieties",
"description" : "Apples come in several varieties, including Fuji, Granny Smith, and Honeycrisp. The most popular varieties are McIntosh, Gala, and Granny Smith.",
"category": "organic"
},
{
"_id" : 2,
"type" : "fruit",
"summary" : "Banana",
"description" : "Bananas are usually sold in bunches of five or six.",
"category": "nonorganic"
},
{
"_id" : 3,
"type" : "fruit",
"summary" : "Pear varieties",
"description" : "Bosc and Bartlett are the most common varieties of pears.",
"category": "nonorganic"
}

fruitコレクションには、 英語 の アナライザ と 動的フィールドマッピング を使用する インデックス定義 もあります。

{
"analyzer": "lucene.english",
"searchAnalyzer": "lucene.english",
"mappings": {
"dynamic": true
}
}

注意

強調表示の便利な点の 1 つは、検索クエリーによって返される元のテキストが表示されることです。このテキストは、検索タームと完全に一致しない場合があります。例、言語固有のアナライザを使用すると、テキスト検索では検索タームのすべての語幹付きバリエーションが返されます。

強調表示のもう 1 つの便利な面は、クエリpath内または外部で任意のフィールドを強調表示するために使用できることです。 たとえば、 というタームを検索する場合、クエリ フィールドとhighlightオプションを使用して指定したその他のフィールドのクエリ用語を強調表示できます。 詳細については、「マルチフィールドの例 」を参照してください。

次のクエリは、 MongoDB Search クエリの $search highlight オプションを示しています。

次のクエリは、 fruitコレクションのdescriptionフィールドでvarietybunchを検索し、 highlightオプションを有効にします。

The $project pipeline stage restricts the output to the description field and adds a new field called highlights, which contains highlighting information.

1db.fruit.aggregate([
2 {
3 $search: {
4 "text": {
5 "path": "description",
6 "query": ["variety", "bunch"]
7 },
8 "highlight": {
9 "path": "description"
10 }
11 }
12 },
13 {
14 $project: {
15 "description": 1,
16 "_id": 0,
17 "highlights": { "$meta": "searchHighlights" }
18 }
19 }
20])

検索タームbunchは、 _id: 2を含むドキュメントに一致を返します。これは、 descriptionフィールドにbunchesという単語が含まれているためです。 検索タームvarietyは、 _id: 3_id: 1を含むドキュメントに一致します。これは、 descriptionフィールドにvarietiesという単語が含まれているためです。

MongoDB Search Playground でこれを試してみてください。

次のクエリは、 fruitコレクションのdescriptionフィールドでvarietybunchを検索し、 highlightオプションを有効にし、検索する最大文字数を40に設定し、 1のみを検索します。ドキュメントごとに返される高スコアのパス。

The $project pipeline stage restricts the output to the description field and adds a new field called highlights, which contains highlighting information.

1db.fruit.aggregate([
2 {
3 $search: {
4 "text": {
5 "path": "description",
6 "query": ["variety", "bunch"]
7 },
8 "highlight": {
9 "path": "description",
10 "maxNumPassages": 1,
11 "maxCharsToExamine": 40
12 }
13 }
14 },
15 {
16 $project: {
17 "description": 1,
18 "_id": 0,
19 "highlights": { "$meta": "searchHighlights" }
20 }
21 }
22])

上記の結果の 2 番目のドキュメントには、検索フィールドに検索タームvarieties が含まれているにもかかわらず空の highlights 配列が含まれています。これは、 MongoDB Search が強調表示するために 40 文字のみを検査したためです。同様に、 MongoDB Search は強調表示用に検索フィールドの 40文字のみを検査したため、includ という単語は切り捨てられます。3 番目のドキュメントでは、複数のパスに検索タームが含まれていますが、 MongoDB Search は highlights 結果の 1 つのパスのみを返します。クエリでは、highlights 結果内のドキュメントごとに 1 パスのみが必要であったためです。

MongoDB Search Playground でこれを試してみてください。

varietiesdescriptionfruit次のクエリは、 コレクションの フィールドでhighlight を検索し、description summaryフィールドと フィールドの両方で オプションを有効にします。

The $project pipeline stage adds a new field called highlights, which contains highlighting information for the query term across all fields in the highlight option.

1db.fruit.aggregate([
2 {
3 $search: {
4 "text": {
5 "path": "description",
6 "query": "varieties"
7 },
8 "highlight": {
9 "path": ["description", "summary" ]
10 }
11 }
12 },
13 {
14 $project: {
15 "description": 1,
16 "summary": 1,
17 "_id": 0,
18 "highlights": { "$meta": "searchHighlights" }
19 }
20 }
21])

検索タームvarietiesは、 _id: 1_id: 3を含むドキュメントに一致を返します。両方のドキュメントのクエリ フィールドdescriptionにクエリ用語varietiesが含まれているためです。 さらに、 highlights配列にはsummaryフィールドが含まれます。このフィールドにはクエリ用語varietiesが含まれているためです。

MongoDB Search Playground でこれを試してみてください。

次のクエリは、 fruitコレクション内のdesで始まるフィールドでvarietiesというタームを検索し、 desで始まるフィールドではhighlightオプションを有効にします。

The $project pipeline stage adds a new field called highlights, which contains highlighting information.

1db.fruit.aggregate([
2 {
3 "$search": {
4 "text": {
5 "path": {"wildcard": "des*"},
6 "query": ["variety"]
7 },
8 "highlight": {
9 "path": {"wildcard": "des*"}
10 }
11 }
12 },
13 {
14 "$project": {
15 "description": 1,
16 "_id": 0,
17 "highlights": { "$meta": "searchHighlights" }
18 }
19 }
20])

MongoDB Search の結果では、des で始まるフィールドが強調表示されます。

MongoDB Search Playground でこれを試してみてください。

次のクエリは、 categoryフィールドでorganicを検索し、 descriptionフィールドでvarietyという用語を検索します。 $search複合クエリのhighlightオプションは、 descriptionフィールドに対するテキストクエリのみの情報を強調表示するリクエストです。 ただし、 $searchステージ内のhighlightオプションは$searchステージの子であり、 $searchステージ内の演算子であってはなりません。

The $project pipeline stage adds a new field called highlights, which contains highlighting information.

1db.fruit.aggregate([
2 {
3 "$search": {
4 "compound": {
5 "should": [{
6 "text": {
7 "path": "category",
8 "query": "organic"
9 }
10 },
11 {
12 "text": {
13 "path": "description",
14 "query": "variety"
15 }
16 }]
17 },
18 "highlight": {
19 "path": "description"
20 }
21 }
22 },
23 {
24 "$project": {
25 "description": 1,
26 "category": 1,
27 "_id": 0,
28 "highlights": { "$meta": "searchHighlights" }
29 }
30 }
31])

MongoDB Search Playground でこれを試してみてください。

この例では、 fruitコレクションには次のインデックス定義もあります。

{
"mappings": {
"dynamic": false,
"fields": {
"description": [
{
"type": "autocomplete",
"tokenization": "edgeGram",
"minGrams": 2,
"maxGrams": 15,
"foldDiacritics": true
}
]
}
}
}

次のクエリは、fruit コレクションの description フィールドで var の文字を検索し、description フィールドで highlight オプションを有効にします。

The $project pipeline stage adds a new field called highlights, which contains highlighting information.

重要

パスのオートコンプリート インデックス バージョンを強調表示するには、オートコンプリート演算子がクエリ内でそのパスを使用する唯一の演算子である必要があります。

1db.fruit.aggregate([
2 {
3 "$search": {
4 "autocomplete": {
5 "path": "description",
6 "query": ["var"]
7 },
8 "highlight": {
9 "path": "description"
10 }
11 }
12 },
13 {
14 "$project": {
15 "description": 1,
16 "_id": 0,
17 "highlights": { "$meta": "searchHighlights" }
18 }
19 }
20])

MongoDB Search では、クエリ文字列var に対して _id: 1id_: 2 を含むドキュメントが一致します。これは、fruitコレクションの descriptionフィールドに単語の先頭に文字 var が含まれているためです。 強調表示されたパスが強調表示されたクエリのオペレーターでのみ参照されている場合、 MongoDB Search は強調表示された hit を、より大まかにクエリタームと一致させます。

MongoDB Search Playground でこれを試してみてください。