8

我希望突出显示匹配的结果。如果我提到字段名称并返回突出显示的文本,这对我有用,但是如果我将字段指定为“_all”,它不会返回任何值。这对我有用:

        curl -XGET "http://localhost:9200/my_index/my_type/_search?q=stackoverflow&size=999" -d '{
        "highlight":{
                    "fields":{
                             "my_field":{}
                    }
        }
}'

这将返回预期值如下:[highlight] => stdClass Object ([my_field] => Array ([0] => stackoverflow是技术人员的最佳网站))

但是当我给这个时:

        curl -XGET "http://localhost:9200/my_index/my_type/_search?q=stackoverflow&size=999" -d '{
        "highlight":{
                    "fields":{
                             "_all":{}
                    }
        }
}'

我得到空值/没有结果。

[highlight] => stdClass Object ( [_all] => Array () )

如何让它在任何字段上工作,这样我就不必提及字段名称?

4

4 回答 4

28

To avoid the need to add _all as a stored field in your index

An alternative quick fix: use * instead of _all:

curl -XGET "http://localhost:9200/my_index/my_type/_search?q=stackoverflow&size=999" -d '{
  "highlight":{
    "fields":{
      "*":{}
    }
  }
}'
于 2014-05-16T05:01:13.870 回答
16

If you are using ES 2.x then you need to set require_field_match option to false due to changes made, From the doc

The default value for the require_field_match option has changed from false to true, meaning that the highlighters will, by default, only take the fields that were queried into account.

This means that, when querying the _all field, trying to highlight on any field other than _all will produce no highlighted snippets.

"highlight": {
    "fields": {
      "*": {}
    },
    "require_field_match": false
 }
于 2015-12-18T03:19:02.530 回答
5

您需要将 _all 字段映射为存储的。下面的映射应该可以解决问题。请注意,这将增加索引大小。

{
  "my_type": {
      "_all": {
        "enabled": true,
        "store": "yes"
      }
  }}
于 2011-12-05T16:36:52.277 回答
-1

This library has functions for query highlighting including highlighting across all fields. The README explains how to create an elasticsearch index with _all field stored etc: https://github.com/niranjan-uma-shankar/Elasticsearch-PHP-class

于 2011-12-21T10:45:13.550 回答