2015-05-05 14:27:52 +08:00
|
|
|
[[query-dsl-script-query]]
|
2015-06-04 07:59:22 +08:00
|
|
|
=== Script Query
|
2013-08-29 07:24:34 +08:00
|
|
|
|
2015-05-05 14:27:52 +08:00
|
|
|
A query allowing to define
|
2015-09-11 16:35:56 +08:00
|
|
|
<<modules-scripting,scripts>> as queries. They are typically used in a filter
|
|
|
|
context, for example:
|
2013-08-29 07:24:34 +08:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
----------------------------------------------
|
2016-05-24 17:58:43 +08:00
|
|
|
GET /_search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"bool" : {
|
|
|
|
"must" : {
|
|
|
|
"script" : {
|
2016-06-27 21:55:16 +08:00
|
|
|
"script" : {
|
|
|
|
"inline": "doc['num1'].value > 1",
|
|
|
|
"lang": "painless"
|
|
|
|
}
|
2016-05-24 17:58:43 +08:00
|
|
|
}
|
|
|
|
}
|
2013-08-29 07:24:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----------------------------------------------
|
2016-05-24 17:58:43 +08:00
|
|
|
// CONSOLE
|
2013-08-29 07:24:34 +08:00
|
|
|
|
|
|
|
[float]
|
2015-06-04 07:59:22 +08:00
|
|
|
==== Custom Parameters
|
2013-08-29 07:24:34 +08:00
|
|
|
|
|
|
|
Scripts are compiled and cached for faster execution. If the same script
|
|
|
|
can be used, just with different parameters provider, it is preferable
|
|
|
|
to use the ability to pass parameters to the script itself, for example:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
----------------------------------------------
|
2016-05-24 17:58:43 +08:00
|
|
|
GET /_search
|
|
|
|
{
|
|
|
|
"query": {
|
|
|
|
"bool" : {
|
|
|
|
"must" : {
|
|
|
|
"script" : {
|
|
|
|
"script" : {
|
2016-06-27 21:55:16 +08:00
|
|
|
"inline" : "doc['num1'].value > params.param1",
|
|
|
|
"lang" : "painless",
|
2016-05-24 17:58:43 +08:00
|
|
|
"params" : {
|
|
|
|
"param1" : 5
|
|
|
|
}
|
|
|
|
}
|
2015-05-12 17:37:22 +08:00
|
|
|
}
|
2013-08-29 07:24:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-17 07:01:33 +08:00
|
|
|
----------------------------------------------
|
2016-05-24 17:58:43 +08:00
|
|
|
// CONSOLE
|
2013-08-29 07:24:34 +08:00
|
|
|
|