ES提供了三种高亮类型,Lucene的plain highlighter,以及fast vector highlighter(fvh)以及posting highlighter
1. plain highlighter
plain highlighter 是es的默认高亮选择,占用空间小,但是对大字段高亮慢
{
"query":{
"match":{
"content":"debug"
}
},
"highlight": {
"fields": {
"content": {}
}
}
}
默认是<em></em>
2. Fast Vector Highlighter
若要使用 fast vector highlighter 作为高亮选择,则要在mapping中对字段指定了term_vector参数,它对于大字段来说比较快(>1M),可以分配不同的权重来匹配不同的位置
{
"properties":{
"id":{
"type": "long"
},
"type":{
"type": "keyword"
},
"content":{
"type": "text",
"analyzer":"ik_max_word",
"term_vector" : "with_positions_offsets"
},
"logTime":{
"type":"date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
3.Posting Highlighter
在mapping中对字段指定了index_options,比起plain highlight性能更快一点,因为它不需要重新分析文档:尤其是对大文件对性能的提高更为明显,占用更少的磁盘空间,使用BM25算法,使搜索的时候像是整篇文档
{
"properties":{
"id":{
"type": "long"
},
"type":{
"type": "keyword"
},
"content":{
"type": "text",
"analyzer":"ik_max_word",
"index_options" : "offsets"
},
"publishTime":{
"type":"date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
4. 自定义高亮标签
{
"query":{
"match":{
"content":"高亮"
}
},
"highlight":{
"pre_tags": ["<b>"],
"post_tags": ["</b>"],
"fields":{
"content":{
}
}
}
}
5.高亮片段fragment的设置
fragment_size: 将要显示的文本拆成*长的一段来进行显示(默认大小是100)
number_of_fragments:高亮的文本片段有多个,你希望展示几个(相当于显示几条)
no_match_size: 可能没有高亮的文本片段,这个参数可以设置从该字段的开始制定长度为多少,然后作为默认的显示
{
"query":{
"match":{
"content":"高亮"
}
},
"highlight":{
"pre_tags": ["<b>"],
"post_tags": ["</b>"],
"fields":{
"content":{
"fragment_size" : 100,
"number_of_fragments" : 1,
"no_match_size": 2
}
}
}
}
表示长度是100(如果没有高亮,从第二位开始),显示一条