liveBook · Manning

_source

As explained in Elasticsearch in Action, Second Edition

The Elasticsearch _source field contains the original JSON document stored with a search or retrieval response.

The _source field holds the original JSON document in an Elasticsearch response. A normal document retrieval commonly returns both _source and metadata such as the index, ID, and version. When an application needs only the document as it was indexed, use the _source endpoint instead of the _doc endpoint. section 5.3.1 section 2.2.2

Fetch the original movie document

Use the _source endpoint to return the indexed JSON without response metadata: GET movies/_source/1 This is useful when a client needs the document itself rather than its ID, index, version, or other retrieval details. section 5.3.1

Response shaping matters when documents are large, contain many attributes, or include data that should not be sent to a client. Set _source to false to omit the original document and return metadata only. This reduces bandwidth when the source data is not needed. section 5.3 section 5.3.2

Return search metadata without documents

A search can suppress every hit’s original JSON while still returning its metadata: GET movies/_search { "_source": false, "query": { "match": { "certificate": "R" } } } The hits retain values such as _index, _id, and _score, but omit _source. section 8.8.5 section 5.3.2

For search results, _source can also select a smaller document view. Provide an array of field patterns to return selected source fields, or use an object with includes and excludes. Wildcards such as title* can match related field names; excludes can remove a matched field such as title.original. section 8.8.5

Return a compact movie source

This query returns only selected parts of each matching movie’s original document: GET movies/_search { "_source": { "includes": ["title*", "synopsis", "genre"], "excludes": ["title.original"] }, "query": { "match": { "certificate": "R" } } } Use this approach when a search screen needs a summary rather than every stored attribute. section 8.8.5

Source filtering is distinct from the fields option. The fields option returns requested values under fields, with each value represented as an array; _source filtering controls which portions of the original document appear under _source. Scripted fields can add computed values alongside a filtered source. section 8.8.5

Listing 8.31 Source filtering using includes and excludes
GET movies/_search
{
  "_source": {
    "includes": ["title*","synopsis","genre"],
    "excludes": ["title.original"]
  }, 
  "query": {
    "match": {
      "certificate": "R"
    }
  }
}

Inspect an includes-and-excludes request that keeps useful movie fields while removing an unwanted matching field. section 8.8.5

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains endpoint retrieval, suppression, wildcard selection, and include/exclude filtering without describing storage internals.
Examples
Uses book and movie documents, metadata-only searches, selected title and rating fields, and include/exclude filters.
What it assumes
Introduces single-document GET retrieval from an index and uses JSON-based Query DSL for search examples.
What you can do after
Apply it via Elasticsearch REST requests to retrieve, suppress, or filter original document data.
How it connects
Connects document retrieval and search responses to metadata, fields, script_fields, Query DSL, and the _bulk API.

Related concepts

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.