Bird
Raised Fist0
Elasticsearchquery~20 mins

Highlighting matched text in Elasticsearch - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Highlighting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the highlighted output for a simple match query?
Given the following Elasticsearch query with highlighting, what will be the highlighted fragment in the search results?
Elasticsearch
{
  "query": {
    "match": {
      "content": "quick brown"
    }
  },
  "highlight": {
    "fields": {
      "content": {}
    }
  }
}
A["The <em>quick</em> <em>brown</em> fox jumps"]
B["The quick brown fox jumps"]
C["The <strong>quick</strong> <strong>brown</strong> fox jumps"]
D["The <highlight>quick</highlight> <highlight>brown</highlight> fox jumps"]
Attempts:
2 left
💡 Hint
Elasticsearch uses tags by default for highlighting matched terms.
🧠 Conceptual
intermediate
2:00remaining
Which option correctly changes the highlight tags?
You want to change the default tags to tags in Elasticsearch highlighting. Which highlight configuration achieves this?
A"highlight": { "fields": { "content": {} }, "tags": ["<strong>", "</strong>"] }
B"highlight": { "fields": { "content": {} }, "highlight_tags": ["<strong>", "</strong>"] }
C"highlight": { "fields": { "content": {} }, "pre_tags": ["<strong>"], "post_tags": ["</strong>"] }
D"highlight": { "fields": { "content": {} }, "wrap_tags": ["<strong>", "</strong>"] }
Attempts:
2 left
💡 Hint
Look for the official parameters to customize highlight tags.
🔧 Debug
advanced
2:30remaining
Why does this highlight query return no highlights?
Consider this Elasticsearch query. Why does it return no highlighted fragments even though the document contains the word 'fox'?
Elasticsearch
{
  "query": {
    "match": {
      "content": "fox"
    }
  },
  "highlight": {
    "fields": {
      "content": { "type": "fvh" }
    }
  }
}
AThe field 'content' is not stored or indexed with term vectors required by 'fvh' highlighter.
BThe 'fvh' highlighter does not support the 'match' query type.
CThe highlight field name 'content' is misspelled.
DThe query syntax is invalid causing no results.
Attempts:
2 left
💡 Hint
The 'fvh' highlighter requires specific field settings.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this highlight query
Which option contains a syntax error that will cause Elasticsearch to reject the highlight query?
Elasticsearch
{
  "query": {
    "match": {
      "content": "dog"
    }
  },
  "highlight": {
    "fields": {
      "content": {
        "pre_tags": ["<em>"],
        "post_tags": ["</em>"]
      }
    }
  }
}
A "highlight": { "fields": { "content": { "pre_tags": ["<em>"], "post_tags": ["</em>"] } } } }
B{ "highlight": { "fields": { "content": { "pre_tags": ["<em>"], "post_tags": ["</em>"] } } } }
C} } } } ]">me/<"[ :"sgat_tsop" ,]">me<"[ :"sgat_erp" { :"tnetnoc" { :"sdleif" { :"thgilhgih" {
D{ "highlight": { "fields": { "content": { "pre_tags": "<em>", "post_tags": "</em>" } } } }
Attempts:
2 left
💡 Hint
Check the data types of pre_tags and post_tags values.
🚀 Application
expert
2:30remaining
How many highlighted fragments will be returned?
Given this query with highlight settings, how many fragments will Elasticsearch return if the content contains the phrase 'lazy dog' repeated 3 times?
Elasticsearch
{
  "query": {
    "match_phrase": {
      "content": "lazy dog"
    }
  },
  "highlight": {
    "fields": {
      "content": {
        "number_of_fragments": 2,
        "fragment_size": 10
      }
    }
  }
}
A0
B2
C1
D3
Attempts:
2 left
💡 Hint
The number_of_fragments setting limits how many fragments are returned.

Practice

(1/5)
1. What is the main purpose of the highlight section in an Elasticsearch query?
easy
A. To filter documents before searching
B. To sort the search results by relevance
C. To update documents in the index
D. To show matched words clearly in search results

Solution

  1. Step 1: Understand the role of highlighting

    Highlighting is used to emphasize matched words in search results so users can see why a document matched.
  2. Step 2: Compare with other query parts

    Sorting, filtering, and updating are different functions not related to highlighting matched text.
  3. Final Answer:

    To show matched words clearly in search results -> Option D
  4. Quick Check:

    Highlight = show matched words [OK]
Hint: Highlight section shows matched words in results [OK]
Common Mistakes:
  • Confusing highlight with sorting or filtering
  • Thinking highlight updates documents
  • Assuming highlight changes search relevance
2. Which of the following is the correct syntax to add highlighting for the field content in an Elasticsearch query?
easy
A. "highlight": {"fields": {"content": {}}}
B. "highlight": {"field": "content"}
C. "highlight": {"content": {"fields": {}}}
D. "highlight": {"fields": ["content"]}

Solution

  1. Step 1: Recall correct highlight syntax

    The correct syntax uses "highlight" with "fields" as an object mapping field names to options, like {"content": {}}.
  2. Step 2: Check each option

    "highlight": {"fields": {"content": {}}} matches the correct structure. Options B, C, and D use incorrect keys or data types.
  3. Final Answer:

    "highlight": {"fields": {"content": {}}} -> Option A
  4. Quick Check:

    Highlight fields syntax = "highlight": {"fields": {"content": {}}} [OK]
Hint: Use "highlight": {"fields": {"fieldname": {}}} [OK]
Common Mistakes:
  • Using "field" instead of "fields"
  • Putting fields inside an array instead of object
  • Misplacing keys inside highlight
3. Given this Elasticsearch query snippet, what will the highlight section return for a match in the title field?
{
  "query": {"match": {"title": "quick fox"}},
  "highlight": {"fields": {"title": {}}}
}
medium
A. An error because highlight requires custom tags
B. Only the word "quick" highlighted, "fox" ignored
C. The matched words "quick" and "fox" wrapped in default highlight tags
D. No highlighting because tags are missing

Solution

  1. Step 1: Understand default highlight behavior

    When highlight is enabled with empty options, Elasticsearch wraps matched words with default tags like <em> and </em>.
  2. Step 2: Analyze the query

    The query matches both "quick" and "fox" in the title, so both words will be highlighted.
  3. Final Answer:

    The matched words "quick" and "fox" wrapped in default highlight tags -> Option C
  4. Quick Check:

    Default highlight tags wrap matched words [OK]
Hint: Empty highlight options use default tags to wrap matches [OK]
Common Mistakes:
  • Thinking highlight needs custom tags to work
  • Assuming only first matched word is highlighted
  • Believing missing tags cause errors
4. Identify the error in this Elasticsearch highlight query snippet:
{
  "query": {"match": {"description": "fast car"}},
  "highlight": {"fields": ["description"]}
}
medium
A. "match" query is invalid for highlighting
B. "fields" should be an object, not an array
C. "highlight" must be inside "query"
D. "description" field cannot be highlighted

Solution

  1. Step 1: Check highlight fields syntax

    The "fields" key must be an object mapping field names to options, not an array.
  2. Step 2: Verify other parts

    "match" query is valid, "highlight" is correctly placed, and any field can be highlighted if indexed.
  3. Final Answer:

    "fields" should be an object, not an array -> Option B
  4. Quick Check:

    Highlight fields = object, not array [OK]
Hint: Highlight fields must be an object, not a list [OK]
Common Mistakes:
  • Using array instead of object for fields
  • Placing highlight inside query incorrectly
  • Assuming some fields can't be highlighted
5. You want to highlight matched text in both title and content fields with custom tags <strong> and </strong>. Which is the correct highlight section to achieve this?
hard
A. "highlight": {"fields": {"title": {}, "content": {}}, "pre_tags": [""], "post_tags": [""]}
B. "highlight": {"fields": ["title", "content"], "pre_tags": "", "post_tags": ""}
C. "highlight": {"fields": {"title": {"tags": ["", ""]}, "content": {"tags": ["", ""]}}}
D. "highlight": {"fields": {"title": {}, "content": {}}, "tags": ["", ""]}

Solution

  1. Step 1: Understand how to set custom tags

    Custom tags are set globally using "pre_tags" and "post_tags" arrays outside the fields object.
  2. Step 2: Check each option's structure

    "highlight": {"fields": {"title": {}, "content": {}}, "pre_tags": [""], "post_tags": [""]} correctly sets "fields" as an object and uses "pre_tags" and "post_tags" arrays. Options B, C, and D misuse keys or data types.
  3. Final Answer:

    "highlight": {"fields": {"title": {}, "content": {}}, "pre_tags": [""], "post_tags": [""]} -> Option A
  4. Quick Check:

    Custom tags use pre_tags/post_tags arrays [OK]
Hint: Use pre_tags/post_tags arrays for custom highlight tags [OK]
Common Mistakes:
  • Using arrays for fields instead of objects
  • Setting tags inside each field incorrectly
  • Using single strings instead of arrays for tags
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard