Highlight property not appearing in hits


(Radar) #1

I'm trying to get highlights returned in results when I do a query with some highlights. I'm using Elasticsearch 2.2. Here's the query + results that I've tried so far:

curl -X GET http://localhost:9200/teg_test_news/_search -d '{
  "query": {
    "match": {
      "_all": {
        "query": "Lorem",
        "operator":"and"
      }
    }
  },
  "highlight": {
    "pre_tags": ["<span class=\"match\">"],
    "post_tags": ["</span>"],
    "fields": {
      "title": {},
      "body": {}
    }
  },
  "_source": true
}'
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.10848885,
    "hits": [
      {
        "_index": "teg_test_news",
        "_type": "news",
        "_id": "1",
        "_score": 0.10848885,
        "_source": {
          "id": "1",
          "title": "Bob's Bikes to open new store in Lorem",
          "body": "\nLorem Ipsum\n\n"
        }
      }
    ]
  }
}

As you can see, the hit returned does not have any highlights.

However, when I use Elasticsearch 1.7 I am getting highlights:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.10848885,
    "hits": [
      {
        "_index": "teg_test_news",
        "_type": "news",
        "_id": "1",
        "_score": 0.10848885,
        "_source": {
          "id": "1",
          "title": "Bob's Bikes to open new store in Lorem",
          "body": "\nLorem Ipsum\n\n"
        },
        "highlight": {
          "title": [
            "Bob's Bikes to open new store in <span class=\"match\">Lorem</span>"
          ],
          "body": [
            "\n<span class=\"match\">Lorem</span> Ipsum\n\n"
          ]
        }
      }
    ]
  }
}

What do I need to change here so that I get highlights returned in hits in Elasticsearch 2.2?


(Radar) #2

I had to change the query to query on the fields explicitly:

 queries[:query] = {
  bool: {
    should: [
      {
        match: {
          body: { query: query, operator: 'and' }
        }
      },
      {
        match: {
          title: { query: query, operator: 'and' }
        }
      }
    ]
  }

I don't know if this is the right way to do it, but it makes the test pass and so I am happy.


(Nik Everett) #3

You can also set require_field_match to false. I believe the default changed from false to true in 2.0 but I can't find the reference.

BTW you can also use multi_match if you'd like to mention all the fields.


(Radar) #4

Thanks Nik, this is spot on. That's allowed me to keep the query simple while still being able to highlight both of those fields.


(system) #5