Search code examples
elasticsearchhighlightelasticsearch-7elasticsearch-highlight

Why is elasticsearch only returning 5 highlights for a field with an array of values that have more matches based on the query?


To demonstrate the issue here is a dynamically created index with single document:

PUT /test_highlight_number/_doc/1 
{ 
   "id": 1,
   "names": ["john 1","john 2", "john 3", "john 4", "john 5", "john 6", "john 7", "john 8"]
}

When I run the following search

GET /test_highlight_number/_search
{
  "query": {
    "match": {
      "names": {
        "query": "john"
      }
    }
  },
  "highlight": {
    "fields": {
      "names": {}
    }
  }
}

I get a response that only highlights 5 of the 8 matching names in the highlight key of hits. Is there a way to tell elasticsearch to return all highlights for a field based on the query?

{
  "took" : 12,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.55034834,
    "hits" : [
      {
        "_index" : "test_highlight_number",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.55034834,
        "_source" : {
          "id" : 1,
          "names" : [
            "john 1",
            "john 2",
            "john 3",
            "john 4",
            "john 5",
            "john 6",
            "john 7",
            "john 8"
          ]
        },
        "highlight" : {
          "names" : [
            "<em>john</em> 1",
            "<em>john</em> 2",
            "<em>john</em> 3",
            "<em>john</em> 4",
            "<em>john</em> 5"
          ]
        }
      }
    ]
  }
}

Solution

  • This is because you are not specified number_of_fragments parameter. It is default to 5. You should read highlighting settings part.

    If you want to retrieve all the fragments that match the query. Your query should be like this:

    {
      "query": {
        "match": {
          "names": {
            "query": "john"
          }
        }
      },
      "highlight": {
        "fields": {
          "names": {
            "number_of_fragments": 0
          }
        }
      }
    }