对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。

在结果中突出显示搜索词语

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

字符串

要搜索的文档字段。path 字段可能包含:

  • 字符串

  • 字符串数组

  • 包含字符串和 multi 分析器规范组合的数组

  • 通配符 *

是

maxCharsToExamine

int

对字段执行突出显示时在文档上检查的最大字符数。如果省略,则默认为 500,000,这意味着MongoDB Search 仅检查每个文档的搜索字段中的前 500,000 个字符以进行突出显示。

no

maxNumPassages

int

每个字段的 highlights 结果中每个文档要返回的高分段落数。一段话大约是一个句子的长度。如果省略,则默认为 5,这意味着对于每个文档, MongoDB搜索都会返回与搜索文本匹配的前 5 个得分最高的段落。

no

"$meta": "searchHighlights" 字段包含突出显示的结果。该字段不是原始文档的一部分,因此,需要使用 $project 管道阶段将其添加到查询输出中。

highlights 字段是一个包含以下输出字段的数组:

字段
类型
说明

path

字符串

返回匹配项的文档字段。

texts

文档数组

每个搜索匹配都会返回一个或多个对象,其中包含匹配文本和周围文本(如果有)。

texts.value

字符串

返回匹配项的字段中的文本。

texts.type

字符串

结果类型。值可以是以下值之一:

  • hit — 结果包含与查询匹配的词语。

  • text — 结果包含与匹配词语相邻的文本内容。

score

float

分配给匹配结果的分数。highlights 分数是衡量 highlights 对象与查询的相关性的指标。如果返回多个 highlights 对象,则最相关的 highlights 对象的分数最高。

您必须将要索引显示的字段索引为MongoDB Search 字符串 类型,并将 indexOptions设立为 offsets(默认)。

您可以在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 }}

注意

突出显示的一个有用方面是,它显示搜索查询返回的原始文本,该文本可能与搜索词语不完全相同。例如,如果使用语言特定的分析器,文本搜索返回搜索词语的所有词干变体。

突出显示的另一个有用方面是,它可用于突出显示查询 path 内部或外部的任何字段。例如,在您搜索某个词语时,您可以在查询字段以及使用 highlight 选项指定的任何其他字段上突出显示查询词语。如要了解更多信息,请参阅多字段示例。

以下查询演示了MongoDB Search 查询中的$search highlight 选项。

以下查询在启用了 highlight 选项的情况下在fruit 集合的 description 字段中搜索 variety 和 bunch。

该 $project 管道阶段将输出限制到 description 字段,并添加一个名为 highlights 的新字段,其中包含突出显示信息。

db.fruit.aggregate([ { $search: { "text": { "path": "description", "query": ["variety", "bunch"] }, "highlight": { "path": "description" } } }, { $project: { "description": 1, "_id": 0, "highlights": { "$meta": "searchHighlights" } } }])

搜索词语 bunch 返回包含 _id: 2 的文档的匹配项,因为 description 字段包含词语 bunches。搜索词语 variety 返回包含 _id: 3 和 _id: 1的文档的匹配项,因为 description 字段包含词语 varieties。

➤ 在MongoDB Search Playground中尝试一下。

以下查询在 fruit 集合的 description 字段中搜索 variety 和 bunch,启用了 highlight 选项,将要检查的最大字符数设置为 40,并且仅为每个文档返回 1 个高分段落。

该 $project 管道阶段将输出限制到 description 字段,并添加一个名为 highlights 的新字段,其中包含突出显示信息。

db.fruit.aggregate([ { $search: { "text": { "path": "description", "query": ["variety", "bunch"] }, "highlight": { "path": "description", "maxNumPassages": 1, "maxCharsToExamine": 40 } } }, { $project: { "description": 1, "_id": 0, "highlights": { "$meta": "searchHighlights" } } }])

即使搜索字段包含搜索术语varieties,上述结果中的第二个文档也包含空 highlights大量,因为MongoDB Search 仅检查 40 个字符以进行突出显示。同样,单词 includ 会被截断,因为MongoDB Search 仅检查搜索字段中的 40 个字符以进行突出显示。在第三个文档中,尽管多个段落包含搜索术语,但MongoDB Search 仅在 highlights 结果中返回一个段落,因为查询仅需要 highlights 结果中每个文档的 1 个段落。

➤ 在MongoDB Search Playground中尝试一下。

以下查询在fruit 集合的 description 字段中搜索 varieties,并为 description 和 summary 字段启用了 highlight 选项。

$project 管道阶段添加一个名为 highlights 的新字段,它在 highlight 选项中的所有字段上包含查询词语的突出显示信息。

db.fruit.aggregate([ { $search: { "text": { "path": "description", "query": "varieties" }, "highlight": { "path": ["description", "summary" ] } } }, { $project: { "description": 1, "summary": 1, "_id": 0, "highlights": { "$meta": "searchHighlights" } } }])

搜索词语 varieties 返回包含 _id: 1 和 _id: 3 的文档的匹配项,因为这两个文档中的查询字段 description 包含查询词语 varieties。此外,highlights 数组还包括 summary 字段,因为该字段包含查询词语 varieties。

➤ 在MongoDB Search Playground中尝试一下。

以下查询在 fruit 集合中以 des 开头的字段中搜索词语 varieties,并为以 des 开头的字段启用了 highlight 选项。

$project 管道阶段添加一个名为 highlights 的新字段,其中包含突出显示信息。

db.fruit.aggregate([ { "$search": { "text": { "path": {"wildcard": "des*"}, "query": ["variety"] }, "highlight": { "path": {"wildcard": "des*"} } } }, { "$project": { "description": 1, "_id": 0, "highlights": { "$meta": "searchHighlights" } } }])

在MongoDB搜索结果中,以 des 开头的字段会突出显示。

➤ 在MongoDB Search Playground中尝试一下。

以下查询在 category 字段中搜索词语 organic,并在 description 字段中搜索词语 variety。$search compound 查询中的 highlight 选项仅为针对 description 字段的 text 查询请求突出显示信息。请注意,$search 阶段中的 highlight 选项必须是 $search 阶段的子阶段,而不是 $search 阶段中的任何操作符的子操作符。

$project 管道阶段添加一个名为 highlights 的新字段,其中包含突出显示信息。

db.fruit.aggregate([ { "$search": { "compound": { "should": [{ "text": { "path": "category", "query": "organic" } }, { "text": { "path": "description", "query": "variety" } }] }, "highlight": { "path": "description" } } }, { "$project": { "description": 1, "category": 1, "_id": 0, "highlights": { "$meta": "searchHighlights" } } }])

➤ 在MongoDB Search Playground中尝试一下。

在本例中,fruit 集合也有如下索引定义。

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

以下查询在fruit 集合的 description 字段中搜索 var 字符,并为 description 字段启用了 highlight 选项。

$project 管道阶段添加一个名为 highlights 的新字段,其中包含突出显示信息。

重要

要突出显示路径的自动完成索引版本,autocomplete 操作符必须是在查询中使用该路径的唯一操作符。

db.fruit.aggregate([ { "$search": { "autocomplete": { "path": "description", "query": ["var"] }, "highlight": { "path": "description" } } }, { "$project": { "description": 1, "_id": 0, "highlights": { "$meta": "searchHighlights" } } }])

MongoDB搜索会为查询字符串 var 返回包含 _id: 1 和 id_: 2 的文档的匹配项,因为 fruit集合中的 description字段在单词开头包含字符 var。 当突出显示的路径仅在突出显示的查询的 autocomplete 操作符中引用时, MongoDB搜索会更粗略地将突出显示的 hit 与查询词语进行匹配。

➤ 在MongoDB Search Playground中尝试一下。