[英]elasticsearch | template query | java API
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
请参阅http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-template.html的最后一节
I added a query template using sense. 我使用sense添加了一个查询模板。 Now the need is through JAVA API of elasticsearch, I need to execute this query template and store the result in SearchResponse.
现在需要通过elasticsearch的JAVA API,我需要执行此查询模板并将结果存储在SearchResponse中。 However I am not able to find any API related to query Template.
但是,我无法找到任何与查询模板相关的API。 The only class file which is available is TemplateQueryBuilder.
唯一可用的类文件是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.
这个类完美地构造了模板查询,但我不确定从Client调用哪个方法来传递TemplateQueryBuilder的对象。 Help in this is appreciated.
感谢您的帮助。
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 参考: http : //www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/search.html#java-search-template
Note that with API version 2.X, as request.setTemplateX
methods are deprecated, you should use a different approach. 请注意,对于API版本2.X,由于不推荐使用
request.setTemplateX
方法,因此应使用其他方法。 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: 您可以使用与接受的答案类似的
request.setTemplate(Template template)
,也可以使用更通用的QueryBuilders
方法:
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 更多内容请阅读: https : //www.elastic.co/guide/en/elasticsearch/client/java-api/2.4/java-specialized-queries.html#java-query-dsl-template-query
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.