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.getDayOfWeek();
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.getDayOfWeek()" } }, "number-of-actors": { "script": { "source": "params['_source']['actors'].length" } } } }