2015-06-24 17:27:19 -04:00
|
|
|
[[java-query-dsl-template-query]]
|
|
|
|
==== Template Query
|
|
|
|
|
|
|
|
See {ref}/search-template.html[Search Template] documentation
|
|
|
|
|
2016-07-13 16:28:18 -04:00
|
|
|
In order to use the `template` query from the Java API
|
|
|
|
the lang-mustache module dependency should be on the classpath and
|
|
|
|
the transport client should be loaded with the lang-mustache plugin:
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
TransportClient transportClient = TransportClient.builder()
|
|
|
|
.settings(Settings.builder().put("node.name", "node"))
|
|
|
|
.addPlugin(MustachePlugin.class)
|
|
|
|
.build();
|
|
|
|
transportClient.addTransportAddress(
|
|
|
|
new InetSocketTransportAddress(new InetSocketAddress(InetAddresses.forString("127.0.0.1"), 9300))
|
|
|
|
);
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2015-06-24 17:27:19 -04:00
|
|
|
Define your template parameters as a `Map<String,Object>`:
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
|
|
|
Map<String, Object> template_params = new HashMap<>();
|
|
|
|
template_params.put("param_gender", "male");
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
You can use your stored search templates in `config/scripts`.
|
|
|
|
For example, if you have a file named `config/scripts/template_gender.mustache` containing:
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
{
|
|
|
|
"template" : {
|
|
|
|
"query" : {
|
|
|
|
"match" : {
|
|
|
|
"gender" : "{{param_gender}}"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
Define your template query:
|
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2016-07-13 16:28:18 -04:00
|
|
|
QueryBuilder qb = new TemplateQueryBuilder(
|
2015-06-24 17:27:19 -04:00
|
|
|
"gender_template", <1>
|
|
|
|
ScriptService.ScriptType.FILE, <2>
|
|
|
|
template_params); <3>
|
|
|
|
--------------------------------------------------
|
|
|
|
<1> template name
|
|
|
|
<2> template stored on disk in `gender_template.mustache`
|
|
|
|
<3> parameters
|
|
|
|
|
2016-07-13 16:28:18 -04:00
|
|
|
You can also store your template in the cluster state:
|
2015-06-24 17:27:19 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2016-07-13 16:28:18 -04:00
|
|
|
client.admin().cluster().preparePutStoredScript()
|
|
|
|
.setScriptLang("mustache")
|
|
|
|
.setId("template_gender")
|
|
|
|
.setSource(new BytesArray(
|
2015-06-24 17:27:19 -04:00
|
|
|
"{\n" +
|
|
|
|
" \"template\" : {\n" +
|
|
|
|
" \"query\" : {\n" +
|
|
|
|
" \"match\" : {\n" +
|
|
|
|
" \"gender\" : \"{{param_gender}}\"\n" +
|
|
|
|
" }\n" +
|
|
|
|
" }\n" +
|
|
|
|
" }\n" +
|
2016-07-13 16:28:18 -04:00
|
|
|
"}")).get();
|
2015-06-24 17:27:19 -04:00
|
|
|
--------------------------------------------------
|
|
|
|
|
2016-07-13 16:28:18 -04:00
|
|
|
To execute a stored templates, use `ScriptService.ScriptType.STORED`:
|
2015-06-24 17:27:19 -04:00
|
|
|
|
|
|
|
[source,java]
|
|
|
|
--------------------------------------------------
|
2016-07-13 16:28:18 -04:00
|
|
|
QueryBuilder qb = new TemplateQueryBuilder(
|
2015-06-24 17:27:19 -04:00
|
|
|
"gender_template", <1>
|
2016-07-13 16:28:18 -04:00
|
|
|
ScriptType.STORED, <2>
|
2015-06-24 17:27:19 -04:00
|
|
|
template_params); <3>
|
|
|
|
--------------------------------------------------
|
|
|
|
<1> template name
|
2016-07-13 16:28:18 -04:00
|
|
|
<2> template stored in the cluster state
|
2015-06-24 17:27:19 -04:00
|
|
|
<3> parameters
|
|
|
|
|