Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Controlling Search Results  ›  Capstone Project

Source Filtering and Highlighting

Highlighting 12 min_source filtering chooses which stored fields come back in each hit, and highlighting returns fragments with the matched terms wrapped in tags.

Source Filtering and Highlighting

The bookshop's result list shows only a title, a price and a snippet with the shopper's words in bold. Yet every hit comes back with the whole book — blurb, reviews, attributes and all. Two features shape the response to fit the screen: source filtering trims what comes back, and highlighting marks why each hit matched.

The rule

_source picks the fields you get back; highlight marks the words that matched.

Each hit normally includes _source, the original JSON you indexed. Source filtering trims it:

GET /books/_search
{
  "_source": [ "title", "price" ],
  "query": { "match": { "title": "garden" } }
}

Each hit's _source now holds only title and price. Other forms:

  • "_source": false — no source at all (you still get _id and _score).
  • "_source": { "includes": [ "title", "reviews.*" ], "excludes": [ "reviews.user" ] } — wildcards, plus exclusions applied after the includes.

This changes only what is returned. The query still searches every field, and the stored document is untouched. Smaller responses mean less data over the network and less JSON for the app to parse.

Highlighting returns fragments of text with the matching words wrapped in tags:

GET /books/_search
{
  "query": { "match": { "blurb": "slow travel" } },
  "highlight": {
    "fields": { "blurb": {} },
    "pre_tags": [ "<mark>" ],
    "post_tags": [ "</mark>" ]
  }
}

Each hit gains a highlight section:

"highlight": {
  "blurb": [ "An overnight journey that makes the case for <mark>slow</mark> <mark>travel</mark> across India." ]
}

Without pre_tags and post_tags, the default wrapper is <em>. Highlighting works on the terms the query matched, so a field only gets highlights if the query actually searched it. Long fields are cut into fragments around the matches; fragment_size and number_of_fragments control them.

The shortcut

Design the response from the screen backwards: list what the screen displays, put those fields in _source, and highlight only the fields the query searched.

The search results cardStep 1 of 4
  1. What the card shows

    Title, price, and a blurb snippet with the search words marked.

Drills

Answer fast — trust the rule.

quick drill

What does "_source": false do?

quick drill

With "_source": ["title"], can the query still match on blurb?

quick drill

Which tag wraps highlighted words by default?

quick drill

The query matches on title only, but you ask to highlight blurb. What appears for blurb?

Remember

  • _source picks the fields you get back; highlight marks the words that matched.
  • Source filtering trims the response, never the search or the stored data.
  • Default highlight tag is <em>; highlight only fields the query searched.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainDesign the results card

The results card shows a book's title, price and a blurb snippet with the search words marked. Write out the parts of the search body you would add for this and explain what each changes in the response, and what it does not change.