按照Elastic Search的文档能够为其API设置Java环境。 但是,当尝试从名为 twitter 的索引进行访问时,在名为 tweet 的类型下,id 值为 1,响应为 null,控制台中没有错误。
上述代码
Settings settings = Settings.builder()
.put("cluster.name", "elasticsearch").build();
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
GetResponse response = client.prepareGet("twitter", "tweet", "2").get();
System.out.println(response.getFields());
client.close();
Kibana 控制台数据相同
{
"_index": "twitter",
"_type": "tweet",
"_id": "2",
"_score": 1,
"_source": {
"user": "Rahul",
"post_date": "2009-11-15T14:12:12",
"message": "trying out Elasticsearch"
}
},
{
"_index": "twitter",
"_type": "tweet",
"_id": "1",
"_score": 1,
"_source": {
"user": "kimchy",
"post_date": "2009-11-15T14:12:12",
"message": "trying out Elasticsearch"
}
}
最佳答案
请尝试这样的事情希望它有帮助
RestClient restClient = RestClient.builder(
new HttpHost("localhost", 9200, "http")).build();
Response response = restClient.performRequest("GET", "/twitter/_search",
Collections.singletonMap("pretty", "true"));
/*
JsonObject user = new JsonObject();
user.addProperty("user","Yash");
user.addProperty("post_date","2009-11-15T14:12:12");
user.addProperty("message","Checking json");
HttpEntity entity = new NStringEntity(user.toString());
Response indexResponse = restClient.performRequest(
"PUT",
"/twitter/tweet/3",
Collections.<String, String>emptyMap(),
entity);
*/
System.out.println(EntityUtils.toString(response.getEntity()));
restClient.close();
关于java - 当尝试使用 java 访问 Elastic Search 时,得到空响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44141789/