Scripting

Sandboxed scripting — scripted_metric, script_fields, script_score

/ Scripting, / Search, / Aggregation

Sandboxed scripting — scripted_metric, script_fields, script_score #

Scripts landed across the whole request path — the engine runs the examples from the Elasticsearch docs unmodified. The engine is Rhai rather than Painless, but doc['x'].value, _score and params.<name> mean what they mean there.

Aggregations. scripted_metric runs your own four-phase map/reduce — init_script, map_script, combine_script, reduce_script — with per-segment states folded at the end. The single-value metric aggs (sum, avg, min, max, cardinality) accept a script in place of a field ( details), and pipeline aggregations keep their buckets_path arithmetic.

...

Script fields

/ Script_fields, / Search, / Scripting

Script fields #

Compute extra values for each hit, at fetch time, from the hit’s own _source — like highlighting, but producing numbers and strings instead of fragments. Scripts run on Pizza’s sandboxed script engine (see the script language reference), only for the hits actually returned on the page.

Every returned hit carries the computed values under fields, as arrays:

...

Scripted queries

/ Script_score, / Script, / _Script, / Scripting

Scripted queries #

Scripts in the search request: script_score to compute the score, a script predicate in a boolean filter, and a _script sort key. All run on Pizza’s sandboxed script engine (see the script language reference) — Rhai, not Painless, but the doc['x'].value, _score, params.<name> spellings run unmodified.

script_score — score by script #

Wrap a query; the script’s value replaces the score. The script sees _score (the wrapped query’s BM25 score), doc (the hit’s _source in doc-values shape) and params:

...

Scripted metric aggregation

/ Scripted_metric, / Aggregation, / Scripting

Scripted metric aggregation #

A metrics aggregation whose result is computed by your own scripts, in four phases, running on Pizza’s sandboxed script engine (see the script language reference).

How it works #

One state map per segment, folded once at the end:

PhaseRunsScope
init_scriptonce per segmentstate, params
map_scriptonce per documentstate, doc, params
combine_scriptonce per segmentstate, params
reduce_scriptonce, at the endstates, params

state is a map you own — initialize it in init_script, accumulate into it in map_script. combine_script folds one segment’s state into a single value (a script that ends in a statement returns the state itself, the default when combine_script is omitted). reduce_script receives all combined values as the states array; if it is omitted, a single state passes through unchanged and several come back as an array — the parts, rather than a wrong merge. Builds without the scripting feature return null for the whole aggregation.

...