elasticsearch,Spring,elasticsearch" /> elasticsearch,Spring,elasticsearch" />

Spring 将http转换为RestTemplate

Spring 将http转换为RestTemplate,spring,elasticsearch,Spring,elasticsearch,我有一个elasticsearch查询,当我将其插入浏览器时,它可以正常工作: http://my.server:9200/customer/customer/_search?q=age:[0+TO+1]&pretty 我很想将它与Spring的RestTemplate一起使用: String a = "http://my.server:9200/customer/customer/_search?q=age:[\"0\"+TO+\"1\"]"; String s = restTemp

我有一个elasticsearch查询,当我将其插入浏览器时,它可以正常工作:

http://my.server:9200/customer/customer/_search?q=age:[0+TO+1]&pretty
我很想将它与Spring的RestTemplate一起使用:

String a = "http://my.server:9200/customer/customer/_search?q=age:[\"0\"+TO+\"1\"]";
String s = restTemplate.getForObject(a, String.class);
JSONObject j = new JSONObject(s);
然后通过JSONObject api访问键/值

问题是,我得到了一个错误:

org.springframework.web.client.HttpClientErrorException: 400 Bad Request
发件人:


我错过了什么?

埃斯在扔400吗

您可能想试试:

String url = "http://my.server:9200/customer/customer/_search?q={q}
Map params = new LinkedHashMap();
params.put("q", "age:[0 TO 1]");
restTemplate.getForObject(url, String.class, params)

我试过这个。没有错误,但返回的记录太多。它应该只返回2条记录,但会返回更多记录。您能否确认,当您直接向ES执行此搜索时(通过示例感知),您得到的结果数量是否正确
POST/customer/customer/_search{“query”:{“query_string”:{“query”:“age:[0到1]”}}}}
String url = "http://my.server:9200/customer/customer/_search?q={q}
Map params = new LinkedHashMap();
params.put("q", "age:[0 TO 1]");
restTemplate.getForObject(url, String.class, params)