ElasticSearch 6.x 学习笔记:19.搜索高亮




19.1 高亮概述

参照官方文档
https://www.elastic.co/guide/en/elasticsearch/reference/6.1/search-request-highlighting.html

Highlighters enable you to get highlighted snippets from one or more fields in your search results so you can show users where the query matches are. When you request highlights, the response contains an additional highlight element for each search hit that includes the highlighted fields and the highlighted fragments.
高亮使您能够从搜索结果中的一个或多个字段中获取突出显示的片段,以便向用户显示查询所匹配的位置。 当我们请求高亮显示时,响应体包含每个搜索匹配的附加突出显示元素,包括突出显示的字段和突出显示的片段。

Highlighting requires the actual content of a field. If the field is not stored (the mapping does not set store to true), the actual _source is loaded and the relevant field is extracted from _source.
高亮显示需要一个字段的实际内容。 如果该字段没有被存储(映射mapping没有将存储设置为 true),则加载实际的_source,并从_source中提取相关的字段。

NOTE:The _all field cannot be extracted from _source, so it can only be used for highlighting if it is explicitly stored.
注:_all字段不能从_source中提取,因此只能用于高亮显示是否明确存储。

19.2 默认高亮

【例子】使用默认高亮显示来获取每个搜索命中title字段的高亮显示,在指定title字段的查询请求中包含高亮显示对象。

GET website/_search
{
    "query" : {
        "match": { "title": "yum" }
    },
    "highlight" : {
        "fields" : {
            "title" : {}
        }
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

查询结果

{
  "took": 27,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.9227539,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "6",
        "_score": 0.9227539,
        "_source": { "title": "CentOS更换国内yum源", "author": "程裕强", "postdate": "2016-12-30", "abstract": "CentOS更换国内yum源", "url": "http://url/53946911" },
        "highlight": { "title": [ "CentOS更换国内<em>yum</em>源" ] } }
    ]
  }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

19.3 自定义高亮标签

pre_tags
Use in conjunction with post_tags to define the HTML tags to use for the highlighted text. By default, highlighted text is wrapped in <em> and</em> tags. Specify as an array of strings.
post_tags
Use in conjunction with pre_tags to define the HTML tags to use for the highlighted text. By default, highlighted text is wrapped in <em>and </em> tags. Specify as an array of strings.

自定义高亮可以通过pre_tags和post_tags设置

GET website/_search
{
    "query" : {
        "match": { "title": "yum" }
    },
    "highlight" : {
        "fields" : {
            "title" : {
              "pre_tags":["<mark>"],
              "post_tags":["</mark>"]
            }
        }
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.9227539,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "6",
        "_score": 0.9227539,
        "_source": { "title": "CentOS更换国内yum源", "author": "程裕强", "postdate": "2016-12-30", "abstract": "CentOS更换国内yum源", "url": "http://url/53946911" },
        "highlight": { "title": [ "CentOS更换国内<mark>yum</mark>源" ] } }
    ]
  }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

19.4 多字段高亮

我们希望搜索title字段时,除了title字段中匹配关键字高亮,摘要abstract字段对应的关键字也要高亮,这需要对require_field_match属性进行设置。

By default, only fields that contains a query match are highlighted. Set require_field_match to false to highlight all fields. Defaults to true.
默认情况下,只有包含查询匹配的字段才会突出显示。 因为默认require_field_match值为true,可以设置为false以突出显示所有字段。

【例子】title和abstract字段高亮

GET website/_search
{
    "query" : {
        "match": { "title": "yum" }
    },
    "highlight" : {
        "require_field_match":false,
        "fields" : {
            "title" : {},
            "abstract" : {}
        }
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.9227539,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "6",
        "_score": 0.9227539,
        "_source": { "title": "CentOS更换国内yum源", "author": "程裕强", "postdate": "2016-12-30", "abstract": "CentOS更换国内yum源", "url": "http://url/53946911" },
        "highlight": { "abstract": [ "CentOS更换国内<em>yum</em>源" ], "title": [ "CentOS更换国内<em>yum</em>源" ] } }
    ]
  }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

19.5 高亮性能分析

Elasticsearch supports three highlighters: unified, plain, and fvh (fast vector highlighter). You can specify the highlighter type you want to use for each field.
Elasticsearch支持三个高亮器:unified,plain和fvh(快速向量高亮器)。 您可以指定要为每个字段使用的高亮器类型。

(1)Unified高亮器
The unified highlighter uses the Lucene Unified Highlighter. This highlighter breaks the text into sentences and uses the BM25 algorithm to score individual sentences as if they were documents in the corpus. It also supports accurate phrase and multi-term (fuzzy, prefix, regex) highlighting. This is the default highlighter.
unified高亮器使用Lucene统一高亮器。 这个高亮器将文本分解为句子,并使用BM25算法对单个句子进行评分,就好像它们是文集中的文档一样。 它还支持准确的短语和多项(模糊,前缀,正则表达式)突出显示。 这是默认的高亮器

(2)Plain高亮器
The plain highlighter uses the standard Lucene highlighter. It attempts to reflect the query matching logic in terms of understanding word importance and any word positioning criteria in phrase queries.
plain高亮器使用标准的Lucene高亮器。 它试图在短语查询中理解单词重要性和任何单词定位标准来反映查询匹配逻辑。

(3)fvh高亮器
The fvh highlighter uses the Lucene Fast Vector highlighter. This highlighter can be used on fields with term_vector set to with_positions_offsets in the mapping. The fast vector highlighter:
fvh高亮器使用Lucene Fast Vector高亮器。此高亮器可用于在映射中将term_vector设置为with_positions_offsets的字段。 快速向量高亮器:

  • Can be customized with a boundary_scanner. (可以使用boundary_scanner进行自定义。)
  • Requires setting term_vector to with_positions_offsets which increases the size of the index (需要将term_vector设置为with_positions_offsets,这会增加索引的大小)
  • Can combine matches from multiple fields into one result. See matched_fields (可以将来自多个字段的匹配组合成一个结果。 请参阅matched_fields)
  • Can assign different weights to matches at different positions allowing for things like phrase matches being sorted above term matches when highlighting a Boosting Query that boosts phrase matches over term matches
    (可以为不同的位置上的匹配分配不同的权重,从而允许在突出显示提高词条匹配的term匹配)

【例子】设置高亮类型
The type field allows to force a specific highlighter type. The allowed values are: unified, plain and fvh. The following is an example that forces the use of the plain highlighter
type字段允许强制设定的高亮器类型。允许的值是:unified, plain和fvh。 下面是一个强制使用plain高亮器的例子

GET website/_search
{
    "query" : {
        "match": { "title": "yum" }
    },
    "highlight" : {
        "fields" : {
            "comment" : {"type" : "plain"}
        }
    }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.9227539,
    "hits": [
      {
        "_index": "website",
        "_type": "blog",
        "_id": "6",
        "_score": 0.9227539,
        "_source": { "title": "CentOS更换国内yum源", "author": "程裕强", "postdate": "2016-12-30", "abstract": "CentOS更换国内yum源", "url": "http://url/53946911" } }
    ]
  }
}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

【例子】
Here is an example of setting the comment field to allow for highlighting using the term_vectors (this will cause the index to be bigger):
这是一个设置comment字段使用fvh高亮器的例子,通过term_vectors进行设置,这会导致索引变大。

PUT /example 

{ 

  "mappings": { 

    "doc" : { 

      "properties": { 

        "comment" : { 

          "type": "text", 

          "term_vector" : "with_positions_offsets" 

        } 

      } 

    } 

  } 

}
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值