liveBook · Manning

Text highlighting

As explained in Elasticsearch in Action, Second Edition

Highlighting is the presentation of matching portions of returned text with markup or visual emphasis.

Text highlighting marks portions of returned search text that match the query, helping users scan results quickly. Add a highlight object beside the top-level query and name the fields to emphasize. section 2.3.6 section 8.8.2

Listing 8.19 Highlighting results when matched
GET movies/_search
{
  "_source": false,      #1
  "query": {
    "term": {
      "title": {
        "value": "godfather"
      }
    }
  },"highlight": {       #2
    "fields": {
      "title": {}        #3
    }
  }
}
#1 Suppresses the source to be returned
#2 Includes a highlight object along with the fields to be highlighted
#3 The field we want to highlight

A movie search suppresses the original source, searches the title with a term query, and returns matching title text inside a highlight field. section 8.8.2

Configure highlighting around the query:

  • Place "highlight" at the same level as "query". section 2.3.6
  • List one or more target fields inside "highlight.fields"; each field can use default settings. section 8.8.2
  • Expect highlighted fragments under each hit's "highlight" property, commonly wrapped in <em> tags. section 2.3.6 section 8.8.2
  • Replace the default tags with custom pre_tags and post_tags when the client needs another marker, such as {{ and }}. section 8.8.2

Highlighting prefix matches

A prefix search for "Mar" in actors.original can return matches such as <em>Marlon Brando</em>, <em>Martin Balsam</em>, and <em>Mark Hamill</em>. Set "_source": false when only highlighted matches are needed. section 9.8.1

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Defines Query DSL placement, target fields, response markup, and custom tags without detailing highlighting internals.
Examples
Uses book synopses, movie titles, and actor names with default <em> tags or custom curly-brace markers.
What you can do after
Apply it via Query DSL to return matched fields with default or custom visual markers.
How it connects
Connects highlighting with Query DSL, match_phrase queries, term queries, prefix queries, and search-result presentation.

This explainer was generated by Manning's knowledge graph from the text of Elasticsearch in Action, Second Edition. The section links point to the parts of the book it draws on.