WARNING: Version 5.5 of Elasticsearch has passed its EOL date.
This documentation is no longer being maintained and may be removed. If you are running this version, we strongly advise you to upgrade. For the latest information, see the current release documentation.
Script Fields
editScript Fields
editAllows to return a script evaluation (based on different fields) for each hit, for example:
GET /_search { "query" : { "match_all": {} }, "script_fields" : { "test1" : { "script" : { "lang": "painless", "inline": "doc['my_field_name'].value * 2" } }, "test2" : { "script" : { "lang": "painless", "inline": "doc['my_field_name'].value * factor", "params" : { "factor" : 2.0 } } } } }
Script fields can work on fields that are not stored (my_field_name
in
the above case), and allow to return custom values to be returned (the
evaluated value of the script).
Script fields can also access the actual _source
document and
extract specific elements to be returned from it by using params['_source']
.
Here is an example:
GET /_search { "query" : { "match_all": {} }, "script_fields" : { "test1" : { "script" : "params['_source']['message']" } } }
Note the _source
keyword here to navigate the json-like model.
It’s important to understand the difference between
doc['my_field'].value
and params['_source']['my_field']
. The first,
using the doc keyword, will cause the terms for that field to be loaded to
memory (cached), which will result in faster execution, but more memory
consumption. Also, the doc[...]
notation only allows for simple valued
fields (can’t return a json object from it) and make sense only on
non-analyzed or single term based fields. However, using doc
is
still the recommended way to access values from the document, if at all
possible, because _source
must be loaded and parsed every time it’s used.
Using _source
is very slow.