elasticsearch - ElasticSearch高亮不高亮

标签 elasticsearch

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

这是测试索引的设置:

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

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

最佳答案

official documentation

In order to perform highlighting, the actual content of the field is required. If the field in question is stored (has store set to true in the mapping) it will be used, otherwise, the actual _source will be loaded and the relevant field will be extracted from it.

The _all field cannot be extracted from _source, so it can only be used for highlighting if it mapped to have store set to 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
}'

关于elasticsearch - ElasticSearch高亮不高亮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39258928/

相关文章:

elasticsearch - 将Logstash Grok分解存储在字段中

elasticsearch - 为什么我的Elasticsearch多重匹配查询仅查找前缀?

elasticsearch - NEST Elasticsearch Reindex 示例

elasticsearch - Logstash重复事件

java - EnableElasticSearchRepositories 异常

elasticsearch - ELASTICSEARCH-多个字段上的Filter_path

elasticsearch - Filebeats 不转发 Docker 撰写日志,为什么?

elasticsearch - Logstash:使用日志文件的行号作为document_id

elasticsearch - 通过 Fluent Api 在 Elastic Search/NEST 2.0 中设置 Id 字段

java - 在 lucene 索引中存储和检索 Json 对象