script-query.asciidoc 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. [[query-dsl-script-query]]
  2. === Script query
  3. ++++
  4. <titleabbrev>Script</titleabbrev>
  5. ++++
  6. A query allowing to define
  7. <<modules-scripting,scripts>> as queries. They are typically used in a filter
  8. context, for example:
  9. [source,js]
  10. ----------------------------------------------
  11. GET /_search
  12. {
  13. "query": {
  14. "bool" : {
  15. "filter" : {
  16. "script" : {
  17. "script" : {
  18. "source": "doc['num1'].value > 1",
  19. "lang": "painless"
  20. }
  21. }
  22. }
  23. }
  24. }
  25. }
  26. ----------------------------------------------
  27. // CONSOLE
  28. [float]
  29. ==== Custom Parameters
  30. Scripts are compiled and cached for faster execution. If the same script
  31. can be used, just with different parameters provider, it is preferable
  32. to use the ability to pass parameters to the script itself, for example:
  33. [source,js]
  34. ----------------------------------------------
  35. GET /_search
  36. {
  37. "query": {
  38. "bool" : {
  39. "filter" : {
  40. "script" : {
  41. "script" : {
  42. "source" : "doc['num1'].value > params.param1",
  43. "lang" : "painless",
  44. "params" : {
  45. "param1" : 5
  46. }
  47. }
  48. }
  49. }
  50. }
  51. }
  52. }
  53. ----------------------------------------------
  54. // CONSOLE