search-template.asciidoc 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. [[java-rest-high-search-template]]
  2. === Search Template API
  3. The search template API allows for searches to be executed from a template based
  4. on the mustache language, and also for previewing rendered templates.
  5. [[java-rest-high-search-template-request]]
  6. ==== Search Template Request
  7. ===== Inline Templates
  8. In the most basic form of request, the search template is specified inline:
  9. ["source","java",subs="attributes,callouts,macros"]
  10. --------------------------------------------------
  11. include-tagged::{doc-tests}/SearchDocumentationIT.java[search-template-request-inline]
  12. --------------------------------------------------
  13. <1> The search is executed against the `posts` index.
  14. <2> The template defines the structure of the search source. It is passed
  15. as a string because mustache templates are not always valid JSON.
  16. <3> Before running the search, the template is rendered with the provided parameters.
  17. ===== Registered Templates
  18. Search templates can be registered in advance through stored scripts API. Note that
  19. the stored scripts API is not yet available in the high-level REST client, so in this
  20. example we use the low-level REST client.
  21. ["source","java",subs="attributes,callouts,macros"]
  22. --------------------------------------------------
  23. include-tagged::{doc-tests}/SearchDocumentationIT.java[register-script]
  24. --------------------------------------------------
  25. Instead of providing an inline script, we can refer to this registered template in the request:
  26. ["source","java",subs="attributes,callouts,macros"]
  27. --------------------------------------------------
  28. include-tagged::{doc-tests}/SearchDocumentationIT.java[search-template-request-stored]
  29. --------------------------------------------------
  30. ===== Rendering Templates
  31. Given parameter values, a template can be rendered without executing a search:
  32. ["source","java",subs="attributes,callouts,macros"]
  33. --------------------------------------------------
  34. include-tagged::{doc-tests}/SearchDocumentationIT.java[render-search-template-request]
  35. --------------------------------------------------
  36. <1> Setting `simulate` to `true` causes the search template to only be rendered.
  37. Both inline and pre-registered templates can be rendered.
  38. ===== Optional Arguments
  39. As in standard search requests, the `explain` and `profile` options are supported:
  40. ["source","java",subs="attributes,callouts,macros"]
  41. --------------------------------------------------
  42. include-tagged::{doc-tests}/SearchDocumentationIT.java[search-template-request-options]
  43. --------------------------------------------------
  44. ===== Additional References
  45. The {ref}/search-template.html[Search Template documentation] contains further examples of how search requests can be templated.
  46. [[java-rest-high-search-template-sync]]
  47. ==== Synchronous Execution
  48. The `searchTemplate` method executes the request synchronously:
  49. ["source","java",subs="attributes,callouts,macros"]
  50. --------------------------------------------------
  51. include-tagged::{doc-tests}/SearchDocumentationIT.java[search-template-execute]
  52. --------------------------------------------------
  53. ==== Asynchronous Execution
  54. A search template request can be executed asynchronously through the `searchTemplateAsync`
  55. method:
  56. ["source","java",subs="attributes,callouts,macros"]
  57. --------------------------------------------------
  58. include-tagged::{doc-tests}/SearchDocumentationIT.java[search-template-execute-async]
  59. --------------------------------------------------
  60. <1> The `SearchTemplateRequest` to execute and the `ActionListener` to call when the execution completes.
  61. The asynchronous method does not block and returns immediately. Once the request completes, the
  62. `ActionListener` is called back using the `onResponse` method if the execution completed successfully,
  63. or using the `onFailure` method if it failed.
  64. A typical listener for `SearchTemplateResponse` is constructed as follows:
  65. ["source","java",subs="attributes,callouts,macros"]
  66. --------------------------------------------------
  67. include-tagged::{doc-tests}/SearchDocumentationIT.java[search-template-execute-listener]
  68. --------------------------------------------------
  69. <1> Called when the execution is successfully completed.
  70. <2> Called when the whole `SearchTemplateRequest` fails.
  71. ==== Search Template Response
  72. For a standard search template request, the response contains a `SearchResponse` object
  73. with the result of executing the search:
  74. ["source","java",subs="attributes,callouts,macros"]
  75. --------------------------------------------------
  76. include-tagged::{doc-tests}/SearchDocumentationIT.java[search-template-response]
  77. --------------------------------------------------
  78. If `simulate` was set to `true` in the request, then the response
  79. will contain the rendered search source instead of a `SearchResponse`:
  80. ["source","java",subs="attributes,callouts,macros"]
  81. --------------------------------------------------
  82. include-tagged::{doc-tests}/SearchDocumentationIT.java[render-search-template-response]
  83. --------------------------------------------------
  84. <1> The rendered source in bytes, in our example `{"query": { "match" : { "title" : "elasticsearch" }}, "size" : 5}`.