Script query

Filters documents based on a provided script. The script query is typically used in a filter context.

Using scripts can result in slower search speeds. See Scripts and search speed.

Example request

  1. GET /_search
  2. {
  3. "query": {
  4. "bool": {
  5. "filter": {
  6. "script": {
  7. "script": {
  8. "source": "doc['num1'].value > 1",
  9. "lang": "painless"
  10. }
  11. }
  12. }
  13. }
  14. }
  15. }

Top-level parameters for script

script

(Required, script object) Contains a script to run as a query. This script must return a boolean value, true or false.

Notes

Custom Parameters

Like filters, scripts are cached for faster execution. If you frequently change the arguments of a script, we recommend you store them in the script’s params parameter. For example:

  1. GET /_search
  2. {
  3. "query": {
  4. "bool": {
  5. "filter": {
  6. "script": {
  7. "script": {
  8. "source": "doc['num1'].value > params.param1",
  9. "lang": "painless",
  10. "params": {
  11. "param1": 5
  12. }
  13. }
  14. }
  15. }
  16. }
  17. }
  18. }

Allow expensive queries

Script queries will not be executed if search.allow_expensive_queries is set to false.