8
votes

I want the matched results to be highlighted. This works for me if I mention the field name and it returns the highlighted text, however if I give the field as "_all", it is not returning any value. This works for me:

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

This returns the expected value as follows: [highlight] => stdClass Object ( [my_field] => Array ( [0] => stackoverflow is the best website for techies ) )

But when I give this:

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

I get null value/no result.

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

How do I get it to work on any field so that I don't have to mention the field name?

4

4 Answers

28
votes

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":{
      "*":{}
    }
  }
}'
16
votes

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
 }
5
votes

You need to map the _all field as stored. The mapping below should do the trick. Note though that this will add to the index size.

{
  "my_type": {
      "_all": {
        "enabled": true,
        "store": "yes"
      }
  }}
-1
votes

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