您好我正在尝试使用spring RestTemplate在弹性搜索中搜索数据。 ElasticSearch有用户名和密码,我想通过json搜索。
我为此编写了代码,但我没有得到任何结果或异常。我这辈子第一次这样做,很抱歉,如果有一些愚蠢的错误。
@Override
protected List<JobPosts> doInBackground(Object[] objects) {
List list = null;
try {
SearchForm searchForms = (SearchForm) objects[0];
String plainCreds = "******:********";
final String url = "*******";
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
HttpEntity<String> request = new HttpEntity<>(searchJson, headers);
Log.d("location", "before exchange");
ResponseEntity<JobPosts[]> response = restTemplate.exchange(url, HttpMethod.GET, request, JobPosts[].class);
JobPosts[] jobPosts = response.getBody();
Log.d("location", "after exchange");
list = Arrays.asList(jobPosts);
} catch (Exception e) {
Log.d("location", e.getMessage());
}
答案 0 :(得分:1)
与其他关系数据库不同,您不需要Spring RestTemplate来查询弹性数据库。 ElasticSearch附带内置的Java API库。您可以直接使用这些函数来创建查询并获得结果。
结帐此链接。它包含有关如何使用API的文档。
答案 1 :(得分:0)
我建议使用Tanay提到的ES Java API。
像这样设置你的连接
//Create the ES clien
org.elasticsearch.client.Client client;
//Setup the connection. Make sure you use port 9300 and not 9200 here.
client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), "9300"));
//Interact with your index, for example getting an object by its ID
GetResponse response = client.prepareGet("index", "type", "id")
.setOperationThreaded(false)
.get();
//Close the connection
client.close();