ElasticSearch学习之路-day19

转载自:https://blog.csdn.net/chengyuqiang/column/info/18392,ES版本号6.3.0

高亮概述
高亮使您能够从搜索结果中的一个或多个字段中获取突出显示的片段,以便向用户显示查询所匹配的位置。 当我们请求高亮显示时,响应体包含每个搜索匹配的附加突出显示元素,包括突出显示的字段和突出显示的片段。
高亮显示需要一个字段的实际内容。 如果该字段没有被存储(映射mapping没有将存储设置为 true),则加载实际的_source,并从_source中提取相关的字段。

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

GET website/_search
{
  "query": {
    "match": {
      "title": "yum"
    }
  },
  "highlight": {
    "fields": {"title": {}}
  }
}

查询结果返回

{
  "took": 186,
  "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.cn/53946911"
        },
        "highlight": {
          "title": [
            "CentOS更换国内<em>yum</em>源"
          ]
        }
      }
    ]
  }
}

使用自定义高亮标签
自定义高亮可以通过pre_tags和post_tags设置

GET website/_search
{
  "query": {
    "match": {
      "title": "yum"
    }
  },
  "highlight": {
    "fields": {
      "title": {
        "pre_tags": [
          "<mark>"
        ],
        "post_tags": [
          "</mark>"
        ]
      }
    }
  }
}

返回结果

{
  "took": 5,
  "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.cn/53946911"
        },
        "highlight": {
          "title": [
            "CentOS更换国内<mark>yum</mark>源"
          ]
        }
      }
    ]
  }
}

多字段高亮
我们希望搜索title字段时,除了title字段中匹配关键字高亮,摘要abstract字段对应的关键字也要高亮,这需要对require_field_match属性进行设置。
默认情况下,只有包含查询匹配的字段才会突出显示,因为默认require_field_match值为true,可以设置为false以突出显示所有字段。
title和abstract字段高亮

GET website/_search
{
  "query": {
    "match": {
      "title": "yum"
    }
  },
  "highlight": {
    "require_field_match":false,
    "fields": {
      "title": {},
      "abstract": {}
    }
  }
}

返回结果

{
  "took": 5,
  "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.cn/53946911"
        },
        "highlight": {
          "abstract": [
            "CentOS更换国内<em>yum</em>源"
          ],
          "title": [
            "CentOS更换国内<em>yum</em>源"
          ]
        }
      }
    ]
  }
}

高亮性能分析
Elasticsearch支持三个高亮器:unified,plain和fvh(快速向量亮器)。您可以指定要为每个字段使用的高亮类型
(1)unified高亮器
unified高亮器使用Lucene统一高亮器。 这个高亮器将文本分解为句子,并使用BM25算法对单个句子进行评分,就好像它们是文集中的文档一样。 它还支持准确的短语和多项(模糊,前缀,正则表达式)突出显示。 这是默认的高亮器。
(2)Plain高亮器
plain高亮器使用标准的Lucene高亮器。 它试图在短语查询中理解单词重要性和任何单词定位标准来反映查询匹配逻辑。
(3)fvh高亮器
fvh高亮器使用Lucene Fast Vector高亮器。此高亮器可用于在mapping中将term_vector设置为with_positions_offsets的字段。 快速向量高亮器:
1.Can be customized with a boundary_scanner. (可以使用boundary_scanner进行自定义。)
2.Requires setting term_vector to with_positions_offsets which increases the size of the index (需要将term_vector设置为with_positions_offsets,这会增加索引的大小)
3.Can combine matches from multiple fields into one result. See matched_fields (可以将来自多个字段的匹配组合成一个结果。 请参阅matched_fields)
4.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匹配)type字段允许强制设定的高亮器类型。允许的值是:unified, plain和fvh。
 下面是一个强制使用plain高亮器的例子

GET website/_search
{
  "query": {
    "match": {
      "title": "yum"
    }
  },
  "highlight": {
    "fields": {

        "title":{"type":"plain"}
 
    }
  }
}

返回

{
  "took": 26,
  "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.cn/53946911"
        },
        "highlight": {
          "title": [
            "CentOS更换国内<em>yum</em>源"
          ]
        }
      }
    ]
  }
}

设置comment字段使用fvh高亮器的例子,通过term_vectors进行设置,将会导致索引变大

扫描二维码关注公众号,回复: 6532175 查看本文章
PUT /example
{
  "mappings": {
    "doc" : {
      "properties": {
        "comment" : {
          "type": "text",
          "term_vector" : "with_positions_offsets"
        }
      }
    }
  }
}
PUT example/doc/1
{"comment":"Reform and reform of public service integration began. In accordance with the requirements of the Central Committee of the Communist Party of China and the State Council's Opinions on Establishing a More Effective Regional Coordination and Development Mechanism, we will accelerate the formation of a new regional coordinated development mechanism that is co-ordinated, competitive, orderly, green, and shared, including the establishment of a regional strategic coordination mechanism. Improve market integration development mechanism, deepen regional cooperation mechanism, optimize regional mutual assistance mechanism, improve inter-regional interest compensation mechanism, improve basic public service equalization mechanism, innovate regional policy control mechanism, and improve regional"}

查询

GET example/_search
{
  "query": {
    "match": {
      "comment": "improve"
    }
  },
  "highlight": {
    "fields": {
        "comment":{"type":"fvh"}
 
    }
  }
}

返回结果

{
  "took": 18,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.49185416,
    "hits": [
      {
        "_index": "example",
        "_type": "doc",
        "_id": "1",
        "_score": 0.49185416,
        "_source": {
          "comment": "Reform and reform of public service integration began. In accordance with the requirements of the Central Committee of the Communist Party of China and the State Council's Opinions on Establishing a More Effective Regional Coordination and Development Mechanism, we will accelerate the formation of a new regional coordinated development mechanism that is co-ordinated, competitive, orderly, green, and shared, including the establishment of a regional strategic coordination mechanism. Improve market integration development mechanism, deepen regional cooperation mechanism, optimize regional mutual assistance mechanism, improve inter-regional interest compensation mechanism, improve basic public service equalization mechanism, innovate regional policy control mechanism, and improve regional"
        },
        "highlight": {
          "comment": [
            "of a regional strategic coordination mechanism. <em>Improve</em> market integration development mechanism, deepen",
            "assistance mechanism, <em>improve</em> inter-regional interest compensation mechanism, <em>improve</em> basic public service",
            "innovate regional policy control mechanism, and <em>improve</em> regional"
          ]
        }
      }
    ]
  }
}

猜你喜欢

转载自blog.csdn.net/qq_23536449/article/details/92572376
今日推荐