【问题标题】:How to connect to ElasticSearch with Java transport client?如何使用 Java 传输客户端连接到 ElasticSearch?
【发布时间】:2016-09-30 21:49:48
【问题描述】:

我正在关注 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 的连接。但是在“准备索引”命令之后,我没有看到任何错误,也没有打印任何内容(我有一些系统输出命令)。在 ElasticSearch 日志中也没有任何关系。当我使用 Rest API 创建索引时,我可以在日志中看到这一点。

【问题讨论】:

  • 能否在 catch 块中添加ex.printStackTrace(); 以便我们查看是否有异常?你正在默默地吞下它;-)
  • 你是对的。我怎么能错过这个?

标签: java elasticsearch elasticsearch-java-api


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2017-01-02
    • 2017-07-05
    • 2015-07-30
    • 2016-11-21
    • 2016-09-28
    • 1970-01-01
    • 2017-04-20
    • 2015-09-13
    • 2011-12-28
    相关资源
    最近更新 更多