mirror of
https://github.com/elastic/elasticsearch.git
synced 2025-06-28 17:34:17 -04:00
Template cleanup:
* Removed `Template` class and unified script & template parsing logic. Templates are scripts, so they should be defined as a script. Unless there will be separate template infrastructure, templates should share as much code as possible with scripts. * Removed ScriptParseException in favour for ElasticsearchParseException * Moved TemplateQueryBuilder to lang-mustache module because this query is hard coded to work with mustache only
This commit is contained in:
parent
798ee177ed
commit
e0ebf5da1c
29 changed files with 336 additions and 604 deletions
|
@ -3,6 +3,21 @@
|
|||
|
||||
See {ref}/search-template.html[Search Template] documentation
|
||||
|
||||
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))
|
||||
);
|
||||
--------------------------------------------------
|
||||
|
||||
Define your template parameters as a `Map<String,Object>`:
|
||||
|
||||
[source,java]
|
||||
|
@ -31,7 +46,7 @@ Define your template query:
|
|||
|
||||
[source,java]
|
||||
--------------------------------------------------
|
||||
QueryBuilder qb = templateQuery(
|
||||
QueryBuilder qb = new TemplateQueryBuilder(
|
||||
"gender_template", <1>
|
||||
ScriptService.ScriptType.FILE, <2>
|
||||
template_params); <3>
|
||||
|
@ -40,11 +55,14 @@ QueryBuilder qb = templateQuery(
|
|||
<2> template stored on disk in `gender_template.mustache`
|
||||
<3> parameters
|
||||
|
||||
You can also store your template in a special index named `.scripts`:
|
||||
You can also store your template in the cluster state:
|
||||
|
||||
[source,java]
|
||||
--------------------------------------------------
|
||||
client.preparePutIndexedScript("mustache", "template_gender",
|
||||
client.admin().cluster().preparePutStoredScript()
|
||||
.setScriptLang("mustache")
|
||||
.setId("template_gender")
|
||||
.setSource(new BytesArray(
|
||||
"{\n" +
|
||||
" \"template\" : {\n" +
|
||||
" \"query\" : {\n" +
|
||||
|
@ -53,19 +71,19 @@ client.preparePutIndexedScript("mustache", "template_gender",
|
|||
" }\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
"}").get();
|
||||
"}")).get();
|
||||
--------------------------------------------------
|
||||
|
||||
To execute an indexed templates, use `ScriptService.ScriptType.INDEXED`:
|
||||
To execute a stored templates, use `ScriptService.ScriptType.STORED`:
|
||||
|
||||
[source,java]
|
||||
--------------------------------------------------
|
||||
QueryBuilder qb = templateQuery(
|
||||
QueryBuilder qb = new TemplateQueryBuilder(
|
||||
"gender_template", <1>
|
||||
ScriptType.INDEXED, <2>
|
||||
ScriptType.STORED, <2>
|
||||
template_params); <3>
|
||||
--------------------------------------------------
|
||||
<1> template name
|
||||
<2> template stored in an index
|
||||
<2> template stored in the cluster state
|
||||
<3> parameters
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue