ElasticSearch:突出显示短语查询中的每个单词

标签 elasticsearch

如何让 Elastic Search 只突出显示导致文档返回的单词?

我有以下索引

{
  "mappings": {
    "document": {
      "properties": {
        "content": {
          "type": "string",
          "fields": {
            "english": {
              "type": "string",
              "analyzer": "english"
            }
          }
        }
      }
    }
  }
}

假设我已经编入索引:

Nuclear power is the use of nuclear reactions that release nuclear energy[5] to generate heat, which most frequently is then used in steam turbines to produce electricity in a nuclear power station. The term includes nuclear fission, nuclear decay and nuclear fusion. Presently, the nuclear fission of elements in the actinide series of the periodic table produce the vast majority of nuclear energy in the direct service of humankind, with nuclear decay processes, primarily in the form of geothermal energy, and radioisotope thermoelectric generators, in niche uses making up the rest.

然后搜索“核元素”~2

我只想突出显示“元素的核裂变”或“元素的核裂变”的一部分,但现在每个出现的核都被突出显示。

如果有帮助,这是我的查询:

{
  "fields": [
  ],
  "query": {
    "query_string": {
      "query": "\"nuclear elements\"~2",
      "fields": [
        "content.english"
      ]
    }
  },
  "highlight": {
    "pre_tags": [
      "<em class='h'>"
    ],
    "post_tags": [
      "</em>"
    ],
    "fragment_size": 500,
    "number_of_fragments": 20,
    "fields": {
      "content.english": {}
    }
  }
} 

最佳答案

有一个highlighting bug在 ES 2.1 中,这是由于 this change 引起的.这已被修复 Pull Request .

根据 ES 开发人员的说法

This is a bug that I introduced in #13239 while thinking that the differences were due to changes in Lucene: extractUnknownQuery is also called when span extraction already succeeded, so we should only fall back to Weight.extractTerms if no spans have been extracted yet.

它适用于 2.0 之前的旧版本,并且在未来的版本中将按预期工作。

关于ElasticSearch:突出显示短语查询中的每个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34643781/

相关文章:

elasticsearch - 当单个客户端节点发生故障时,Elasticsearch客户端不会获取结果

elasticsearch - 如何快速创建具有数百个字段的ElasticSearch2.0映射

Elasticsearch 子集过滤器

elasticsearch - 以24/h时间格式设置Logstash输出索引

java - Elasticsearch Java API boolQuery

elasticsearch - 将可重用字段类型添加到 elasticsearch

python - 如何使用python在Elasticsearch中将索引修改后的内容复制到另一个内容

postgresql - 使用 Postgres 进行 Azure 搜索

json - Elasticsearch 的 "include_in_parent"/"include_in_root"是如何工作的?它应该显示在 '_source' 中吗?

elasticsearch - Elasticsearch长短语搜索