5.5 高亮 (Highlighting) 5.5 Elasticsearch 高亮 (Highlighting) 在 Elasticsearch 中,高亮 (Highlighting) 是一个强大的功能,它允许你在搜索结果中突出显示与查询匹配的文本片段。这对于提高用户体验至关重要,因为它能帮助用户快速找到他们正在寻找的信息。 5.5.1 高亮原理 Elasticsearch 的高亮功能通过分析查询语句和文档内容,找出匹配的关键词或短语,然后使用预定义的标签(默认为 和 )将这些匹配项包裹起来。高亮过程发生在搜索之后,但返回结果之前。 5.5.
在 Elasticsearch 中,高亮 (Highlighting) 是一个强大的功能,它允许你在搜索结果中突出显示与查询匹配的文本片段。这对于提高用户体验至关重要,因为它能帮助用户快速找到他们正在寻找的信息。
Elasticsearch 的高亮功能通过分析查询语句和文档内容,找出匹配的关键词或短语,然后使用预定义的标签(默认为 <em> 和 </em>)将这些匹配项包裹起来。高亮过程发生在搜索之后,但返回结果之前。
假设我们有一个名为 articles 的索引,其中包含以下文档:
PUT /articles/_doc/1 { "title": "Elasticsearch: The Definitive Guide", "content": "This book provides a comprehensive introduction to Elasticsearch, covering topics such as indexing, searching, and analysis." } PUT /articles/_doc/2 { "title": "Mastering Elasticsearch", "content": "Learn advanced techniques for Elasticsearch, including performance tuning, cluster management, and security." }
现在,我们想要搜索包含 "Elasticsearch" 的文档,并高亮显示匹配的词语。
GET /articles/_search { "query": { "match": { "content": "Elasticsearch" } }, "highlight": { "fields": { "content": {} } } }
这个查询会返回包含 "Elasticsearch" 的文档,并在 highlight 字段中包含高亮显示的文本片段:
{ "hits": { "total": { "value": 2, "relation": "eq" }, "max_score": 0.6931471, "hits": [ { "_index": "articles", "_id": "1", "_score": 0.6931471, "_source": { "title": "Elasticsearch: The Definitive Guide", "content": "This book provides a comprehensive introduction to Elasticsearch, covering topics such as indexing, searching, and analysis." }, "highlight": { "content": [ "This book provides a comprehensive introduction to <em>Elasticsearch</em>, covering topics such as indexing, searching, and analysis." ] } }, { "_index": "articles", "_id": "2", "_score": 0.6931471, "_source": { "title": "Mastering Elasticsearch", "content": "Learn advanced techniques for Elasticsearch, including performance tuning, cluster management, and security." }, "highlight": { "content": [ "Learn advanced techniques for <em>Elasticsearch</em>, including performance tuning, cluster management, and security." ] } } ] } }
注意 highlight 字段中的 content 数组,它包含了高亮显示的文本片段,其中 "Elasticsearch" 被 <em> 和 </em> 标签包裹。
Elasticsearch 提供了许多选项来定制高亮的行为,包括:
pre_tags 和 post_tags: 定义高亮标签的前缀和后缀。
fragment_size: 指定高亮片段的最大长度。
number_of_fragments: 指定返回的最大片段数量。
fragmenter: 控制如何分割文本片段。
require_field_match: 是否只高亮查询的字段。
highlight_query: 使用单独的查询来确定高亮范围。
type: 指定高亮器类型,包括 plain, fvh, 和 unified。
你可以使用 pre_tags 和 post_tags 选项来更改默认的高亮标签。
GET /articles/_search { "query": { "match": { "content": "Elasticsearch" } }, "highlight": { "fields": { "content": { "pre_tags": [ "<mark>" ], "post_tags": [ "</mark>" ] } } } }
现在,高亮显示的文本将使用 <mark> 和 </mark> 标签:
{ "hits": { "total": { "value": 2, "relation": "eq" }, "max_score": 0.6931471, "hits": [ { "_index": "articles", "_id": "1", "_score": 0.6931471, "_source": { "title": "Elasticsearch: The Definitive Guide", "content": "This book provides a comprehensive introduction to Elasticsearch, covering topics such as indexing, searching, and analysis." }, "highlight": { "content": [ "This book provides a comprehensive introduction to <mark>Elasticsearch</mark>, covering topics such as indexing, searching, and analysis." ] } }, { "_index": "articles", "_id": "2", "_score": 0.6931471, "_source": { "title": "Mastering Elasticsearch", "content": "Learn advanced techniques for Elasticsearch, including performance tuning, cluster management, and security." }, "highlight": { "content": [ "Learn advanced techniques for <mark>Elasticsearch</mark>, including performance tuning, cluster management, and security." ] } } ] } }
你可以使用 fragment_size 和 number_of_fragments 选项来控制高亮片段的大小和数量。
GET /articles/_search { "query": { "match": { "content": "Elasticsearch" } }, "highlight": { "fields": { "content": { "fragment_size": 50, "number_of_fragments": 2 } } } }
这个查询会返回最多两个片段,每个片段最多 50 个字符。 如果匹配的文本超过了 fragment_size,Elasticsearch 会尝试在句子的边界分割片段。
fragmenter 选项控制如何分割文本片段。 Elasticsearch 提供了两种 fragmenter:
simple: 在空格处分割文本。
span: 更智能地分割文本,考虑词语的边界。
GET /articles/_search { "query": { "match": { "content": "Elasticsearch" } }, "highlight": { "fields": { "content": { "fragmenter": "span", "fragment_size": 50, "number_of_fragments": 2 } } } }
Elasticsearch 提供了几种高亮器类型,每种类型都有不同的特点和适用场景:
plain: 默认高亮器,简单快速,但可能不适用于复杂的查询。
fvh (Fast Vector Highlighter): 使用 term vectors,速度快,支持复杂的查询,但需要启用 term vectors。
unified: 基于 Lucene Unified Highlighter,功能强大,支持复杂的查询,但可能比 plain 高亮器慢。
要使用 fvh 高亮器,首先需要在索引映射中启用 term vectors:
PUT /articles/_mapping { "properties": { "content": { "type": "text", "term_vector": "with_positions_offsets" } } }
然后,可以使用 fvh 高亮器:
GET /articles/_search { "query": { "match": { "content": "Elasticsearch" } }, "highlight": { "fields": { "content": { "type": "fvh" } } } }
unified 高亮器使用方式类似:
GET /articles/_search { "query": { "match": { "content": "Elasticsearch" } }, "highlight": { "fields": { "content": { "type": "unified" } } } }
highlight_queryhighlight_query 允许你使用一个单独的查询来确定高亮范围,这在某些情况下非常有用,例如,当你想高亮显示与原始查询不同的内容时。
GET /articles/_search { "query": { "match": { "content": "Elasticsearch" } }, "highlight": { "fields": { "content": { "highlight_query": { "match": { "title": "Elasticsearch" } } } } } }
在这个例子中,即使查询的是 content 字段,高亮也会基于 title 字段中 "Elasticsearch" 的匹配。
多字段高亮: 你可以同时高亮多个字段,只需在 fields 对象中指定多个字段即可。
嵌套对象高亮: 对于嵌套对象,你需要使用点符号来指定嵌套字段的路径。
使用自定义分析器: 你可以为高亮指定自定义分析器,以更精确地控制高亮过程。
Elasticsearch 的高亮功能是一个强大的工具,可以显著提高搜索结果的可用性。 通过理解不同的高亮选项和技巧,你可以根据你的具体需求定制高亮行为,从而为用户提供更好的搜索体验。 记住,选择合适的高亮器类型和配置取决于你的数据和查询的复杂性。
希望这篇文章能够帮助你更好地理解和使用 Elasticsearch 的高亮功能。