4

I was trying to implement the feature of template query. Refer to the last section of http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-template.html

I added a query template using sense. Now the need is through JAVA API of elasticsearch, I need to execute this query template and store the result in SearchResponse. However I am not able to find any API related to query Template. The only class file which is available is TemplateQueryBuilder. This class constructs the template query perfectly but I am not sure of which method to be called from Client in order to pass the object of TemplateQueryBuilder. Help in this is appreciated.

2 Answers 2

3

Here is 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 ..

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

Sign up to request clarification or add additional context in comments.

Comments

1

Note that with API version 2.X, as request.setTemplateX methods are deprecated, you should use a different approach. Either you can use request.setTemplate(Template template) which is similar to the accepted answer, or you can go with the more generic QueryBuilders approach:

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

More to read: https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.4/java-specialized-queries.html#java-query-dsl-template-query

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.