Highlighting #
Ask for matched fragments of your text fields back alongside each hit, with the matched terms wrapped in tags — ready to render as a search result list.
POST /my-collection/_search
Example #
{
"query": { "match": { "field": "message", "query": "search" } },
"highlight": {
"fields": {
"message": {
"fragment_size": 100,
"number_of_fragments": 3
}
}
},
"size": 5
}
Each returned hit carries a highlight object whose keys are the requested
fields; values are fragment strings with matched terms wrapped in the tags
(default <em> … </em>). Custom tags — same idea with <b> wrapping:
Parameters #
Top level:
fields
(Required, object) Map of field name (or"*"for every text field in the schema that the query touched) to per-field options below.pre_tags
(Optional, array of strings, default:["<em>"]) Tag(s) emitted before a matched term.post_tags
(Optional, array of strings, default:["</em>"]) Tag(s) emitted after a matched term.
Per field:
fragment_size
(Optional, integer, default:100) Target length of each fragment in characters.0returns the whole field value instead of fragments.number_of_fragments
(Optional, integer, default:5) Maximum fragments returned per field.0returns the whole field.no_match_size
(Optional, integer) When the document has the field but nothing matched, return this many characters from the start of the value instead of nothing.
Notes #
- Highlighting uses the same analyzer as the field, so phrase and span matches highlight the same terms the query scored.
- Fragments are selected around the densest cluster of matches, best first.
- Combine with
fields/_sourcefiltering to return highlights without the full source when bandwidth matters.