尝试访问 Elastic Search 时使用 java 获取空响应
When Trying to access Elastic Search get using java getting Empty Response
遵循 Elastic Search 的文档能够为其 API 设置 Java 环境。
但是当尝试从名为 twitter 的索引访问时,在名为 tweet 的类型下,id 值为 1,响应为空,控制台中没有错误。
以上代码
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();
确保您使用的是 HTTP 而不是 HTTPs - 如果您发出简单的纯文本 HTTP 请求,服务器可能会挂断您的电话?
遵循 Elastic Search 的文档能够为其 API 设置 Java 环境。 但是当尝试从名为 twitter 的索引访问时,在名为 tweet 的类型下,id 值为 1,响应为空,控制台中没有错误。
以上代码
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();
确保您使用的是 HTTP 而不是 HTTPs - 如果您发出简单的纯文本 HTTP 请求,服务器可能会挂断您的电话?