liveBook · Manning

Painless

As explained in Elasticsearch in Action, Second Edition

Painless is Elasticsearch's scripting language for safely executing scripts within the platform.

Painless is Elasticsearch’s default scripting language for scripted document updates. Use it to change fields, edit lists, or apply conditional business logic. section 5.4.3

A script contains three main parts:

  • source — the update logic, such as modifying ctx._source. section 5.4.3
  • lang — the scripting language; Painless is the default, with other supported language options listed. section 5.4.3
  • params — values supplied separately from the logic, such as a configurable earnings threshold. section 5.4.3

Conditionally flag a blockbuster

POST movies/_update/1 { "script": { "source": "if (ctx._source.boxoffice_gross_in_millions > params.gross_earnings_threshold) { ctx._source.blockbuster = true } else { ctx._source.blockbuster = false }", "params": { "gross_earnings_threshold": 150 } } } The script reads the document through ctx._source and keeps the threshold outside the logic, so the value can change without rewriting the condition.

How Elasticsearch in Action, Second Edition covers it

How deep it goes
Explains script anatomy and parameter passing, but defers detailed scripting internals.
Examples
Demonstrates movie updates that edit actors, add or remove fields, set multiple values, and flag blockbusters.
What you can do after
Apply it via Elasticsearch’s _update API to modify fields, lists, and conditional document state.
How it connects
Connects Painless with scripted updates, document fields, conditional logic, and script parameters.

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.