[英]How to connect to ElasticSearch with Java transport client?
I am following the ElasticSearch documentation on Java Client. 我正在关注Java Client上的ElasticSearch文档。 I have started ElasticSearch and I can interact with it with the Rest API. 我已经启动了ElasticSearch,并且可以与Rest API进行交互。 I want to use the Java Client and so far I have a main like this: 我想使用Java客户端,到目前为止,我有一个这样的主体:
public class TestElastic {
public static void main(String[] args) {
try{
TransportClient client = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
JSONObject place = new JSONObject();
place.put("name", "AAAAA");
IndexResponse response = client.prepareIndex("my_database", "places", "1")
.setSource(place)
.get();
System.out.println(response.toString());
// Index name
String _index = response.getIndex();
System.out.println(_index);
// Type name
String _type = response.getType();
System.out.println(_type);
// Document ID (generated or not)
String _id = response.getId();
System.out.println(_id);
// Version (if it's the first time you index this document, you will get: 1)
long _version = response.getVersion();
System.out.println(_version);
// isCreated() is true if the document is a new one, false if it has been updated
boolean created = response.isCreated();
System.out.println(created);
client.close();
}catch (Exception ex){
ex.printStackTrace();
}
}
} }
In the Java logs I can see that there is a connection with 127.0.0.1:9300. 在Java日志中,我可以看到与127.0.0.1:9300的连接。 But after the "prepare index" command I do not see any error and nothing is printed(I have some system out commands). 但是在“准备索引”命令之后,我没有看到任何错误,也没有打印任何内容(我有一些系统输出命令)。 In the ElasticSearch logs is also nothing relative. 在ElasticSearch日志中也没有相对关系。 When I create an index with the Rest API I can see this in the logs. 当我使用Rest API创建索引时,我可以在日志中看到它。
Ok, as @Val mentioned I forgot to print the errors. 好的,正如@Val提到的,我忘记了打印错误。 The problem was that JSONObject is not the format that ElasticSearch wants. 问题在于JSONObject不是ElasticSearch想要的格式。 Map and HashMap are acceptable. Map和HashMap是可以接受的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.