2013-08-28 19:24:34 -04:00
|
|
|
[[search-request-script-fields]]
|
|
|
|
=== Script Fields
|
|
|
|
|
|
|
|
Allows to return a <<modules-scripting,script
|
|
|
|
evaluation>> (based on different fields) for each hit, for example:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
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" : {
|
2016-06-27 09:55:16 -04:00
|
|
|
"script" : {
|
|
|
|
"lang": "painless",
|
2017-06-09 11:29:25 -04:00
|
|
|
"source": "doc['my_field_name'].value * 2"
|
2016-06-27 09:55:16 -04:00
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
},
|
|
|
|
"test2" : {
|
2015-05-12 05:37:22 -04:00
|
|
|
"script" : {
|
2016-06-27 09:55:16 -04:00
|
|
|
"lang": "painless",
|
2017-06-09 11:29:25 -04:00
|
|
|
"source": "doc['my_field_name'].value * factor",
|
2015-05-12 05:37:22 -04:00
|
|
|
"params" : {
|
|
|
|
"factor" : 2.0
|
|
|
|
}
|
2013-08-28 19:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-18 08:38:54 -04:00
|
|
|
// CONSOLE
|
|
|
|
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2013-11-07 10:56:59 -05:00
|
|
|
Script fields can work on fields that are not stored (`my_field_name` 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
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
2016-05-18 08:38:54 -04:00
|
|
|
// CONSOLE
|
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
|
2013-11-08 18:34:23 -05:00
|
|
|
fields (can't return a json object from it) and make sense only on
|
2017-03-15 20:18:34 -04:00
|
|
|
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.
|
2013-08-28 19:24:34 -04:00
|
|
|
|