template-query.asciidoc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. [[java-query-dsl-template-query]]
  2. ==== Template Query
  3. See {ref}/search-template.html[Search Template] documentation
  4. In order to use the `template` query from the Java API
  5. the lang-mustache module dependency should be on the classpath and
  6. the transport client should be loaded with the lang-mustache plugin:
  7. [source,java]
  8. --------------------------------------------------
  9. TransportClient transportClient = TransportClient.builder()
  10. .settings(Settings.builder().put("node.name", "node"))
  11. .addPlugin(MustachePlugin.class)
  12. .build();
  13. transportClient.addTransportAddress(
  14. new InetSocketTransportAddress(new InetSocketAddress(InetAddresses.forString("127.0.0.1"), 9300))
  15. );
  16. --------------------------------------------------
  17. Define your template parameters as a `Map<String,Object>`:
  18. [source,java]
  19. --------------------------------------------------
  20. Map<String, Object> template_params = new HashMap<>();
  21. template_params.put("param_gender", "male");
  22. --------------------------------------------------
  23. You can use your stored search templates in `config/scripts`.
  24. For example, if you have a file named `config/scripts/template_gender.mustache` containing:
  25. [source,js]
  26. --------------------------------------------------
  27. {
  28. "template" : {
  29. "query" : {
  30. "match" : {
  31. "gender" : "{{param_gender}}"
  32. }
  33. }
  34. }
  35. }
  36. --------------------------------------------------
  37. // NOTCONSOLE
  38. Define your template query:
  39. [source,java]
  40. --------------------------------------------------
  41. QueryBuilder qb = new TemplateQueryBuilder(
  42. "gender_template", <1>
  43. ScriptService.ScriptType.FILE, <2>
  44. template_params); <3>
  45. --------------------------------------------------
  46. <1> template name
  47. <2> template stored on disk in `gender_template.mustache`
  48. <3> parameters
  49. You can also store your template in the cluster state:
  50. [source,java]
  51. --------------------------------------------------
  52. client.admin().cluster().preparePutStoredScript()
  53. .setScriptLang("mustache")
  54. .setId("template_gender")
  55. .setSource(new BytesArray(
  56. "{\n" +
  57. " \"template\" : {\n" +
  58. " \"query\" : {\n" +
  59. " \"match\" : {\n" +
  60. " \"gender\" : \"{{param_gender}}\"\n" +
  61. " }\n" +
  62. " }\n" +
  63. " }\n" +
  64. "}")).get();
  65. --------------------------------------------------
  66. To execute a stored templates, use `ScriptService.ScriptType.STORED`:
  67. [source,java]
  68. --------------------------------------------------
  69. QueryBuilder qb = new TemplateQueryBuilder(
  70. "gender_template", <1>
  71. ScriptType.STORED, <2>
  72. template_params); <3>
  73. --------------------------------------------------
  74. <1> template name
  75. <2> template stored in the cluster state
  76. <3> parameters