我试图实现模板查询的功能。请参阅http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-template.html
的最后一节我使用sense添加了一个查询模板。现在需要通过elasticsearch的JAVA API,我需要执行此查询模板并将结果存储在SearchResponse中。但是,我无法找到任何与查询模板相关的API。唯一可用的类文件是TemplateQueryBuilder。这个类完美地构造了模板查询,但我不确定从Client调用哪个方法来传递TemplateQueryBuilder的对象。 感谢您的帮助。
答案 0 :(得分:2)
以下是如何操作:
SearchRequestBuilder request = client;.prepareSearch();
request.setTemplateName(templateName);
request.setTemplateType(ScriptService.ScriptType.INDEXED);
request.setTemplateParams(templateParams);
SearchResponse response = request.get();
你只需解析响应对象然后..
答案 1 :(得分:0)
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