liveBook · Manning

Ctx context variable

As explained in Elasticsearch in Action, Second Edition

The ctx context variable exposes the current document and update context to an Elasticsearch update script.

The ctx context variable gives an Elasticsearch update script access to the document being changed. Use ctx._source.<field> to read or modify document fields. section 5.4.3

Add an actor to a movie

A scripted update can append Diane Keaton directly to the existing actors array: ctx._source.actors.add('Diane Keaton').

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains field access and demonstrates array, field, multi-field, and conditional mutations, without descending into script execution internals.
Examples
Uses movie actors, ratings, runtime, review scores, box-office earnings, and blockbuster status as update examples.
What you can do after
Apply it via Elasticsearch scripted updates to add, remove, or conditionally modify document fields and array values.
How it connects
Connects ctx to scripted updates, the update API, arrays, conditional logic, and script parameters.

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.