2019-07-17 08:49:22 -04:00
|
|
|
[[request-body-search-script-fields]]
|
2019-07-19 14:35:36 -04:00
|
|
|
==== Script Fields
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
Allows to return a <<modules-scripting,script
|
|
|
|
evaluation>> (based on different fields) for each hit, for example:
|
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2016-05-18 08:38:54 -04:00
|
|
|
GET /_search
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
2020-07-21 15:49:58 -04:00
|
|
|
"query": {
|
|
|
|
"match_all": {}
|
|
|
|
},
|
|
|
|
"script_fields": {
|
|
|
|
"test1": {
|
|
|
|
"script": {
|
|
|
|
"lang": "painless",
|
|
|
|
"source": "doc['price'].value * 2"
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
},
|
2020-07-21 15:49:58 -04:00
|
|
|
"test2": {
|
|
|
|
"script": {
|
|
|
|
"lang": "painless",
|
|
|
|
"source": "doc['price'].value * params.factor",
|
|
|
|
"params": {
|
|
|
|
"factor": 2.0
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
2020-07-21 15:49:58 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
2020-07-21 15:49:58 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2018-05-22 14:22:42 -04:00
|
|
|
// TEST[setup:sales]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-05-27 05:47:46 -04:00
|
|
|
Script fields can work on fields that are not stored (`price` in
|
2013-08-28 19:24:34 -04:00
|
|
|
the above case), and allow to return custom values to be returned (the
|
|
|
|
evaluated value of the script).
|
|
|
|
|
2017-03-15 20:18:34 -04:00
|
|
|
Script fields can also access the actual `_source` document and
|
2017-03-15 20:29:31 -04:00
|
|
|
extract specific elements to be returned from it by using `params['_source']`.
|
2017-03-15 20:18:34 -04:00
|
|
|
Here is an example:
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2019-09-09 12:35:50 -04:00
|
|
|
[source,console]
|
2013-08-28 19:24:34 -04:00
|
|
|
--------------------------------------------------
|
2016-05-18 08:38:54 -04:00
|
|
|
GET /_search
|
2013-08-28 19:24:34 -04:00
|
|
|
{
|
|
|
|
"query" : {
|
2016-05-18 08:38:54 -04:00
|
|
|
"match_all": {}
|
2013-08-28 19:24:34 -04:00
|
|
|
},
|
|
|
|
"script_fields" : {
|
|
|
|
"test1" : {
|
2017-03-15 20:18:34 -04:00
|
|
|
"script" : "params['_source']['message']"
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2017-03-15 20:18:34 -04:00
|
|
|
// TEST[setup:twitter]
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2013-11-08 18:34:23 -05:00
|
|
|
Note the `_source` keyword here to navigate the json-like model.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2013-11-08 18:34:23 -05:00
|
|
|
It's important to understand the difference between
|
2017-03-15 20:18:34 -04:00
|
|
|
`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
|
2013-08-28 19:24:34 -04:00
|
|
|
consumption. Also, the `doc[...]` notation only allows for simple valued
|
2018-01-11 04:57:46 -05:00
|
|
|
fields (you can't return a json object from it) and makes sense only for
|
|
|
|
non-analyzed or single term based fields. However, using `doc` is
|
2017-03-15 20:18:34 -04:00
|
|
|
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.
|
2013-08-28 19:24:34 -04:00
|
|
|
|