如何使用 Java 传输客户端连接到 ElasticSearch?

How to connect to ElasticSearch with Java transport client?

我正在关注 Java 客户端上的 ElasticSearch 文档。我已经启动了 ElasticSearch,我可以与 Rest API 进行交互。我想使用 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();
    }
}

}

在 Java 日志中,我可以看到与 127.0.0.1:9300 有连接。但是在 "prepare index" 命令之后,我没有看到任何错误,也没有打印任何内容(我有一些系统输出命令)。在 ElasticSearch 日志中也没有关系。当我使用 Rest API 创建索引时,我可以在日志中看到这一点。

好的,正如@Val 提到的,我忘记打印错误了。问题是 JSONObject 不是 ElasticSearch 想要的格式。 Map和HashMap是可以接受的。