【问题标题】:ElasticSearch Highlighting Not highlightingElasticSearch 高亮 不高亮
【发布时间】:2016-09-01 11:42:36
【问题描述】:

我很难理解如何使突出显示起作用。 我的查询正在返回该项目,但我没有看到会导致突出显示的标签。

这是测试索引的设置:

curl -XPUT 'http://localhost:9200/testfoo' -d '{
    "mappings": {
        "entry": {
            "properties": {
                "id": { "type": "integer" },
                "owner": { "type": "string" },
                "target": {
                    "properties": {
                        "id": { "type": "integer" },
                        "type": {
                            "type": "string",
                            "index": "not_analyzed"
                        }
                    }
                },
                "body": { "type": "string" },
                "body_plain": { "type": "string"}
            }
        }
    }
}'

这是几个插入的文档:

curl -XPUT 'http://localhost:9200/testfoo/entry/1' -d'{
    "id": 1,
    "owner": "me",
    "target": {
        "type": "event",
        "id":   100
    },
    "body": "<div>Message One has foobar in it</div>",
    "body_plain": "Message One has foobar in it"
}'

curl -XPUT 'http://localhost:9200/testfoo/entry/2' -d'{
    "id": 2,
    "owner": "me",
    "target": {
        "type": "event",
        "id":   200
    },
    "body": "<div>Message One has no bar in it</div>",
    "body_plain": "Message One has no bar in it"
}'

简单的搜索返回预期的文档:

curl -XPOST 'http://localhost:9200/testfoo/_search?pretty' -d '{
    "query": {
        "simple_query_string": {
            "query": "foobar"
        }
    }
}'
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.09492774,
    "hits" : [ {
      "_index" : "testfoo",
      "_type" : "entry",
      "_id" : "1",
      "_score" : 0.09492774,
      "_source" : {
        "id" : 1,
        "owner" : "me",
        "target" : {
          "type" : "event",
          "id" : 100
        },
        "body" : "<div>Message One has foobar in it</div>",
        "body_plain" : "Message One has foobar in it"
      }
    } ]
  }
}

但是,当我添加“突出显示”时,我得到了相同的 JSON,但 body_plain 没有通过匹配项“突出显示”:

curl -XPOST 'http://localhost:9200/testfoo/_search?pretty' -d '{
    "query":{
            "query": {
                "simple_query_string":{
                    "query":"foobar"
                }
            }
    },
    "highlight": {
        "pre_tags": [ "<div class=\"highlight\">" ],
        "post_tags": [ "</div>" ],
        "fields": {
            "_all": {
                "fragment_size": 10,
                "number_of_fragments": 1
            }
        }
    },
    "sort": [
        "_score"
    ],
    "_source": [ "target", "id", "body_plain", "body" ],
    "min_score": 0.9,
    "size":10
}'
{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "testfoo",
      "_type" : "entry",
      "_id" : "1",
      "_score" : 1.0,
      "_source" : {
        "id" : 1,
        "body" : "<div>Message One has foobar in it</div>",
        "target" : {
          "id" : 100,
          "type" : "event"
        },
        "body_plain" : "Message One has foobar in it"
      }
    } ]
  }
}

我期待 body_plain 的样子

  Message One has <div class="highlight">foobar</div> in it

想知道我做错了什么。谢谢。

【问题讨论】:

  • 在这里查看answers应该会有帮助

标签: elasticsearch


【解决方案1】:

来自official documentation

为了执行高亮,该字段的实际内容是 必需的。如果存储有问题的字段(已将 store 设置为 true 映射)它将被使用,否则,实际的 _source 将是 加载并从中提取相关字段。

_all 字段不能从 _source 中提取,所以只能是 用于突出显示它是否映射为将 store 设置为 true。

你有两种方法可以解决这个问题。您可以更改映射以存储 _all 字段:

{
  "mappings": {
    "entry": {
      "_all": {              <-- add this
        "store": true
      },
      "properties": {
        ...

或者您将查询更改为:

curl -XPOST 'http://localhost:9200/testfoo/_search?pretty' -d '{
    "query":{
            "query": {
                "simple_query_string":{
                    "query":"foobar"
                }
            }
    },
    "highlight": {
        "pre_tags": [ "<div class=\"highlight\">" ],
        "post_tags": [ "</div>" ],
        "require_field_match": false,             <-- add this
        "fields": {
            "*": {                                <-- use this                  
                "fragment_size": 10,
                "number_of_fragments": 1
            }
        }
    },
    "sort": [
        "_score"
    ],
    "_source": [ "target", "id", "body_plain", "body" ],
    "min_score": 0.9,
    "size":10
}'

【讨论】:

  • 感谢 Val,成功了。显然我在阅读时没有建立联系。