Highlighting issues

Hi,

I tried to highlight search results:

POST one/two
{
  "name": "Jonn Doe"
}

GET one/two/_search
{
  "query": {
    "query_string": {
      "analyze_wildcard": true,
      "query": "*Doe*"
    }
  },
  "highlight": {
    "fields": {
      "_all": {}
    }
  }
}

But I can't see highlighted results:

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 1,
    "hits": [
      {
        "_index": "one",
        "_type": "two",
        "_id": "AVk5uY9jRC6_QLkwtbju",
        "_score": 1,
        "_source": {
          "name": "Jonn Doe"
        }
      }
    ]
  }
}

What's wrong?

Have you seen the note at the top of the highlighting documentation page?

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.

Can't understand :frowning:

My mapping looks like:

{
  "one": {
    "mappings": {
      "two": {
        "properties": {
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}

Is field 'name' stored or no? Need I add 'store' attribute for it? Btw, I tried use 'name' instead of '_all', but result was the same. So, can you show me what I need to fix in my example to see highlighting?

No, it's not stored by default separately, but content can be extracted from _source. So, it is not the issue.

Unless you also specified name in the query, your search was executed against the field _all. By default, highlighting only works with the fields you are searching against. So, you can do something like

GET one/two/_search
{
  "query": {
    "query_string": {
      "analyze_wildcard": true,
      "query": "name:*Doe*"
    }
  },
  "highlight": {
    "fields": {
      "name": {}
    }
  }
}

Or you can disable this behavior using require_field_match parameter and highlight any field:

GET one/two/_search
{
  "query": {
    "query_string": {
      "analyze_wildcard": true,
      "query": "*Doe*"
    }
  },
  "highlight": {
    "require_field_match": false,
    "fields": {
      "name": { }
    }
  }
}

Or you can store _all field, by recreating your index

DELETE one

PUT one
{
  "mappings": {
    "two": {
      "_all": {
        "store": "yes"
      }
    }
  }
}

POST one/two
{
  "name": "Jonn Doe"
}

GET one/two/_search
{
  "query": {
    "query_string": {
      "analyze_wildcard": true,
      "query": "*Doe*"
    }
  },
  "highlight": {
    "fields": {
      "_all": {}
    }
  }
}

Thank you

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.