赞
踩
注:version:elasticsearch-7.11.2
添加测试数据
#新建索引 PUT /high_light_test { "mappings": { "properties": { "title": { "type": "text", "analyzer": "ik_max_word" }, "content": { "type": "text", "analyzer": "ik_max_word" } } }, "settings": { "number_of_shards": 1, "number_of_replicas": 0 } } #或者用这种写法新建索引,可以设置默认分词器 PUT /high_light_test { "settings" : { "index" : { "analysis.analyzer.default.type": "ik_max_word" }, "number_of_shards": 1, "number_of_replicas": 0 } } PUT /high_light_test/_doc/1 { "title": "2021年最新好看的电影推荐", "content": "新年又到了,2021年最新好看的电影有不少,我给大家推荐几部:balabala" }
#单条件查询高亮显示 GET /high_light_test/_search { "query": { "match": { "title": "最新电影推荐" } }, "highlight": { "fields": { "title": {} } } } #组合查询高亮显示。在 highlight 里面填写要高亮显示的字段 GET /high_light_test/_search { "query": { "bool": { "should": [ { "match": { "title": "新电影" } }, { "match": { "content": "最新电影推荐" } } ] } }, "highlight": { "fields": { "title": {}, "content": {} } } }
<em>会让搜索词红色高亮显示
高亮字体默认显示红色,我们也可以改变高亮字体的颜色。例:
(fields: * 代表所有field都高亮显示)
- #组合查询高亮显示
- GET /high_light_test/_search
- {
- "query": {
- "bool": {
- "should": [
- {
- "match": {
- "title": "最新电影推荐"
- }
- },
- {
- "match": {
- "content": "最新电影推荐"
- }
- }
- ]
- }
- },
- "highlight": {
- "require_field_match": false,
- "fields": {
- "*": {
- "pre_tags": [
- "<font color='yellow'>"
- ],
- "post_tags": [
- "</font>"
- ]
- }
- }
- }
- }
默认的高亮方式,也是lucene自带的高亮方式
用法:在 mappings 里指定 index_options=offsets
优点:(1)性能比plain highlight要高,因为不需要重新对高亮文本进行分词
(2)对磁盘的消耗更少
例:
#删除旧索引 DELETE /high_light_test #重建索引,指定content的 "index_options": "offsets" PUT /high_light_test { "mappings": { "properties": { "title": { "type": "text", "analyzer": "ik_max_word" }, "content": { "type": "text", "analyzer": "ik_max_word", "index_options": "offsets" } } }, "settings": { "number_of_shards": 1, "number_of_replicas": 0 } } #重新添加一下测试数据 PUT /high_light_test/_doc/1 { "title": "2021年最新好看的电影推荐", "content": "新年又到了,2021年最新好看的电影有不少,我给大家推荐几部:balabala" }
高亮查询:
测试结果看不出差别
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。