Elasticsearch实战——搜索高亮

Elasticsearch实战——搜索高亮

1. 自定义高亮片段

在前面的基本查询中,我们简单的使用了高亮功能标记查询关键字,ES默认会用<em></em>标签标记关键字。如果我们想使用自定义标签,可以在高亮属性中给需要高亮的字段加上pre_tagspost_tags。示例如下:

GET website/_search
{
    
    
    "query":{
    
    
        "match":{
    
    
            "title":"自行车"
        }
    },
    "highlight":{
    
    
        "fields":{
    
    
            "title":{
    
    
                "pre_tags":["<strong>"],
                "post_tags":["</strong>"]
            }
        }
    }
}

2. 多字段高亮

搜索高亮的时候可以设置多个字段同时高亮。比如,搜索title字段的时候,我们希望address字段中的关键字也能高亮。这个时候我们需要把require_field_match属性的值设置为falserequire_field_match的默认值为true,只会高亮匹配的字段。多字段高亮如下:

GET website/_search
{
    
    
  "query": {
    
    
    "match_phrase": {
    
    
      "title": "北京"
    }
  },
  "highlight": {
    
    
    "require_field_match": false,
    "fields": {
    
    
      "title": {
    
    
        "pre_tags": [
          "<strong>"
        ],
        "post_tags": [
          "</strong>"
        ]
      },
      "address": {
    
    
        "pre_tags": [
          "<strong>"
        ],
        "post_tags": [
          "</strong>"
        ]
      }
    }
  }
}

3. 高亮性能分析

ES提供了三种高亮器,分别是默认的highlighter高亮器postings-highlighter高亮器fast-vector-highlighter高亮器

默认的highlighter是最基本的高亮器。highlighter高亮器实现高亮功能需要对_source中保存的原始文档进行二次分析,它的速度在三种高亮器里面最慢,优点是不需要额外的存储空间

postings-highlighter高亮器实现高亮不需要二次分析,但是需要在字段的映射中设置index_options参数的值为offsets,保留关键词的偏移量,速度快于highlighter高亮器。如:

PUT website
{
    
    
    "mappings":{
    
    
        "properties":{
    
    
            "title":{
    
    
                "type":"text",
                "analyzer":"ik_max_word",
                "search_analyzer":"ik_smart",
                "index_options":"offsets"
            }
        }
    }
}

fast-vector-highlighter高亮器实现高亮速度最快,但是需要在字段的映射中设置term_vector参数的取值为with_opsitions_offsets,保存关键词的位置和偏移信息,占用的存储空间最大,是一种以空间换时间的做法

PUT website
{
    
    
    "mappings":{
    
    
        "properties":{
    
    
            "title":{
    
    
                "type":"text",
                "analyzer":"ik_max_word",
                "search_analyzer":"ik_smart",
                "term_vector":"with_positions_offsets"
            }
        }
    }
}

4. 关注我

搜索微信公众号:java架构强者之路
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/dwjf321/article/details/103960571