In Elasticsearch, I would like to know what variables are available in the scope of a script used in a (sub)aggregation.
I already read resources on scripting and text scoring in scripts as well as the documentation of most aggregation types but could not find a comprehensive reference of what can be used depending on the situation.
From what I could gather, most scripts in aggregations are run per-hit and have access to the following scope:
- document fields, through
doc['field_name'].*
- document as parsed from index, through
_source
- stored fields, through
_fields['field_name']
- document score, through
_score
- shard statistics, through
_index.*
- per-shard field statistics, through
_index['field_name'].*
- per-shard term statistics, through
_index['field_name']['term'].*
The Scripted Metric aggregation also exposes an _agg
variable in per-shard & per-hit scripts (init, map & combine scripts) and an _aggs
variable at the coordinating node level (reduce script).
Of course, the map script can also access every other per-hit variable as defined above.
I could not find documentation indicating whether _index*
was available in per-shard scripts but it would seem logic.
Also, scripted_metric is the only case of scripts at the shard or coordinating node level that I could find.
The Signicant Terms aggregation has a special case of running a scoring script per-bucket, with the following variables in scope:
_subset_freq
,_superset_freq
,_subset_size
,_superset_size
However it does not seem to have access to other bucket-dependent stuff, like bucket key (term) or single-value metric sub-aggregations, which is a pitty because it would be super-useful (at least, I could not find any documentation on how to access such bucket-dependent information).
The Terms aggregation also has a special case of a value script (when both parameters field
and script
are defined), which can only access a _value
variable.
Is the information summed up here accurate/complete?