簡體   English   中英

如何使用Java傳輸客戶端連接到ElasticSearch?

[英]How to connect to ElasticSearch with Java transport client?

我正在關注Java Client上的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的連接。 但是在“准備索引”命令之后,我沒有看到任何錯誤,也沒有打印任何內容(我有一些系統輸出命令)。 在ElasticSearch日志中也沒有相對關系。 當我使用Rest API創建索引時,我可以在日志中看到它。

好的,正如@Val提到的,我忘記了打印錯誤。 問題在於JSONObject不是ElasticSearch想要的格式。 Map和HashMap是可以接受的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM