elasticsearch,Java,elasticsearch" /> elasticsearch,Java,elasticsearch" />

Elasticsearch:通过Java客户端搜索模板?

Elasticsearch:通过Java客户端搜索模板?,java,elasticsearch,Java,elasticsearch,如上所述,我们可以使用搜索模板通过java API向elasticsearch服务器发送请求: @Test public void testTemplateQuery2() { Map<String, Object> template_params = new HashMap<>(); template_params.put("query_string", "aaa bbb"); StreamInput in; SearchRequestB

如上所述,我们可以使用搜索模板通过java API向elasticsearch服务器发送请求:

@Test
public void testTemplateQuery2() {
    Map<String, Object> template_params = new HashMap<>();
    template_params.put("query_string", "aaa bbb");
    StreamInput in;
    SearchRequestBuilder builder = client.prepareSearch()
            .setTemplateName("template_gender")
            .setTemplateType(ScriptService.ScriptType.FILE)
            .setTemplateParams(template_params);
    SearchResponse searchResponse = builder.get();
    System.out.println(searchResponse);
}
但是,我想在java端对此进行配置,我尝试了以下方法:

   @Test
public void testTemplateQuery3() {
    Map<String, Object> template_params = new HashMap<String, Object>();
    template_params.put("query_string", "aaa");
    SearchRequestBuilder builder = client.prepareSearch().setTimeout(TimeValue.timeValueMinutes(5))
            .setTemplateType(ScriptService.ScriptType.INLINE)
            .setTemplateSource("{'template' : {'size' : 1, '_source' : ['timestamp','field1'], 'query': { 'match': { 'field1': '{{query_string}}'}}}}".replace('\'', '"'))
            .setTemplateParams(template_params);

    SearchResponse searchResponse = builder.get();
    System.out.println(searchResponse);
}

“查询字符串”被解析为空字符串“”,为什么?

我首先建议您安装marvel并尝试运行查询;如果成功了,那么你最好在编程中使用同样的方法;如果没有,那么我建议首先更正查询。查询在Curl中工作,我建议您首先安装marvel并尝试运行查询;如果成功了,那么你最好在编程中使用同样的方法;如果没有,那么我建议首先更正查询
   @Test
public void testTemplateQuery3() {
    Map<String, Object> template_params = new HashMap<String, Object>();
    template_params.put("query_string", "aaa");
    SearchRequestBuilder builder = client.prepareSearch().setTimeout(TimeValue.timeValueMinutes(5))
            .setTemplateType(ScriptService.ScriptType.INLINE)
            .setTemplateSource("{'template' : {'size' : 1, '_source' : ['timestamp','field1'], 'query': { 'match': { 'field1': '{{query_string}}'}}}}".replace('\'', '"'))
            .setTemplateParams(template_params);

    SearchResponse searchResponse = builder.get();
    System.out.println(searchResponse);
}
{"size":1,"_source":["timestamp","field1"],"query":{"match":{"field1":""}}}