Return highlighted fragments and full highlighted field in same request

My apologies in advance as I'm new to ES. This is kind of a strange use-case, but we have a large text field that we would like to do 2 things with:

  1. Show the first X chars of the body text with any search results highlighted
  2. Show the first Y highlights/fragments of the body text with search results highlighted

My goal is to do this all in one search, but I can't find a way to get the same field back with different highlighting parameters. I was thinking maybe if I had some way to create an alias on the field then I could refer to it in the highlights section but I couldn't find a way to do that either.

We can simply store the first X chars of the document as a separate field to work around the issue, but the developer handing ingestion would prefer to avoid that.

Any thoughts would be much appreciated.

1 Like

Right. So if you were willing to hack on highlighting you could implement this but Elasticsearch doesn't have a feature like that. It has "return the first x characters if nothing in this field matched" (no_match_size) and it has "return the x best fragments" (that is highlighting's default).

You can get something kindof like what you want with the limit token filter: Limit token count token filter | Elasticsearch Guide [8.11] | Elastic

You'd make one field but analyze it twice, once limiting the token count to 1000 or something suitably small and the other time without the limit. Then you'd highlight against both of the names. Note: this'd get you the first 1000 words not the first 1000 characters. But words are better than characters any way!

Thanks for the reply. I will take a look at that limit method as that sounds like it might suit our needs.

Cheers.