Elasticsearch | request template | Java API

I tried to implement a template request function. See the last section http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-template.html

I added a query template using meaning. Now I need elasticsearch via the JAVA API, I need to execute this query template and save the result in SearchResponse. However, I cannot find the API associated with the request template. The only class file that is available is TemplateQueryBuilder. This class ideally builds a template request, but I'm not sure which method is called from Client to pass a TemplateQueryBuilder object. Help is appreciated.

+4
source share
2 answers

Here's how to do it:

SearchRequestBuilder request = client;.prepareSearch();
request.setTemplateName(templateName);
request.setTemplateType(ScriptService.ScriptType.INDEXED);
request.setTemplateParams(templateParams);
SearchResponse response = request.get();

You just need to parse the response object, then ..

link to: http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/search.html#java-search-template

+2
source

Please note that with API version 2.X, since the methods request.setTemplateXare deprecated, you should use a different approach. Either you can use request.setTemplate(Template template)that is similar to the accepted answer, or you can go with a more general approach QueryBuilders:

QueryBuilder qb = QueryBuilders.templateQuery(
    "templateName",
    ScriptService.ScriptType.FILE,
    templateParams);

Read more at: https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.4/java-specialized-queries.html#java-query-dsl-template-query

0
source

All Articles