- Painless Scripting Language: other versions:
- Painless Guide
- Painless Language Specification
- Painless contexts
- Context example data
- Runtime fields context
- Ingest processor context
- Update context
- Update by query context
- Reindex context
- Sort context
- Similarity context
- Weight context
- Score context
- Field context
- Filter context
- Minimum should match context
- Metric aggregation initialization context
- Metric aggregation map context
- Metric aggregation combine context
- Metric aggregation reduce context
- Bucket script aggregation context
- Bucket selector aggregation context
- Analysis Predicate Context
- Watcher condition context
- Watcher transform context
- Painless API Reference
- Shared API
- Aggregation Selector API
- Aggs API
- Aggs Combine API
- Aggs Init API
- Aggs Map API
- Aggs Reduce API
- Analysis API
- Bucket Aggregation API
- Field API
- Filter API
- Ingest API
- Interval API
- Moving Function API
- Number Sort API
- Painless Test API
- Processor Conditional API
- Score API
- Script Heuristic API
- Similarity API
- Similarity Weight API
- String Sort API
- Template API
- Terms Set API
- Update API
- Watcher Condition API
- Watcher Transform API
- Xpack Template API
Field context
editField context
editUse a Painless script to create a script field to return a customized value for each document in the results of a query.
Variables
-
params
(Map
, read-only) - User-defined parameters passed in as part of the query.
-
doc
(Map
, read-only) -
Contains the fields of the specified document where each field is a
List
of values. -
params['_source']
(Map
, read-only) -
Contains extracted JSON in a
Map
andList
structure for the fields existing in a stored document.
Return
-
Object
- The customized value for each document.
API
The standard Painless API is available.
Example
To run this example, first follow the steps in context examples.
You can then use these two example scripts to compute custom information for each search hit and output it to two new fields.
The first script gets the doc value for the datetime
field and calls
the getDayOfWeek
function to determine the corresponding day of the week.
doc['datetime'].value.getDayOfWeekEnum();
The second script calculates the number of actors. Actors' names are stored
as a text array in the actors
field.
By default, doc values are not available for text fields. However,
you can still calculate the number of actors by extracting actors
from |
Submit the following request:
GET seats/_search { "query": { "match_all": {} }, "script_fields": { "day-of-week": { "script": { "source": "doc['datetime'].value.getDayOfWeekEnum()" } }, "number-of-actors": { "script": { "source": "params['_source']['actors'].size()" } } } }