template-query.asciidoc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. [[query-dsl-template-query]]
  2. === Template Query
  3. coming[1.1.0]
  4. A query that accepts a query template and a map of key/value pairs to fill in
  5. template parameters.
  6. [source,js]
  7. ------------------------------------------
  8. GET _search
  9. {
  10. "query": {
  11. "template": {
  12. "query": {"match_{{template}}": {}},
  13. "params" : {
  14. "template" : "all"
  15. }
  16. }
  17. }
  18. }
  19. ------------------------------------------
  20. Alternatively escaping the template works as well:
  21. [source,js]
  22. ------------------------------------------
  23. GET _search
  24. {
  25. "query": {
  26. "template": {
  27. "query": "{\"match_{{template}}\": {}}\"",
  28. "params" : {
  29. "template" : "all"
  30. }
  31. }
  32. }
  33. }
  34. ------------------------------------------
  35. You register a template by storing it in the conf/scripts directory of
  36. elasticsearch. In order to execute the stored template reference it in the query parameters:
  37. [source,js]
  38. ------------------------------------------
  39. GET _search
  40. {
  41. "query": {
  42. "template": {
  43. "query": "storedTemplate",
  44. "params" : {
  45. "template" : "all"
  46. }
  47. }
  48. }
  49. }
  50. ------------------------------------------
  51. Templating is based on Mustache. For simple token substitution all you provide
  52. is a query containing some variable that you want to substitute and the actual
  53. values:
  54. [source,js]
  55. ------------------------------------------
  56. GET _search
  57. {
  58. "query": {
  59. "template": {
  60. "query": {"match_{{template}}": {}},
  61. "params" : {
  62. "template" : "all"
  63. }
  64. }
  65. }
  66. }
  67. ------------------------------------------
  68. which is then turned into:
  69. [source,js]
  70. ------------------------------------------
  71. GET _search
  72. {
  73. "query": {
  74. "match_all": {}
  75. }
  76. }
  77. ------------------------------------------
  78. For more information on how Mustache templating and what kind of templating you
  79. can do with it check out the [online
  80. documentation](http://mustache.github.io/mustache.5.html) of the mustache project.