原文地址: https://www.elastic.co/guide/cn/elasticsearch/guide/current/highlighting-intro.html, 版权归 www.elastic.co 所有
英文版地址: https://www.elastic.co/guide/en/elasticsearch/guide/current/highlighting-intro.html
英文版地址: https://www.elastic.co/guide/en/elasticsearch/guide/current/highlighting-intro.html
请注意:
本书基于 Elasticsearch 2.x 版本,有些内容可能已经过时。
本书基于 Elasticsearch 2.x 版本,有些内容可能已经过时。
高亮搜索 (Highlighting Our Searches)edit
许多应用都倾向于在每个搜索结果中 高亮(highlight ) 文本片段,以便让用户知道该文档为什么符合查询条件。在 Elasticsearch 中检索出高亮片段也很容易。
再次执行前面的查询,但是增加一个新的 highlight 参数:
GET /megacorp/employee/_search
{
"query" : {
"match_phrase" : {
"about" : "rock climbing"
}
},
"highlight": {
"fields" : {
"about" : {}
}
}
}
当执行该查询时,返回结果与之前一样,但此时结果中还多了一个叫做 highlight 的部分。这个部分包含了 about 字段匹配的文本片段,并以 HTML 标签 <em></em> 包裹:
{
...
"hits": {
"total": 1,
"max_score": 0.23013961,
"hits": [
{
...
"_score": 0.23013961,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [ "sports", "music" ]
},
"highlight": {
"about": [
"I love to go <em>rock</em> <em>climbing</em>"
]
}
}
]
}
}
关于高亮搜索片段,可以在 highlighting reference documentation 了解更多信息。