Elasticsearch本地节点客户端NPE在java测试中创建索引时

时间:2016-07-29 16:21:21

标签: java elasticsearch spring-boot

我正在使用elasticsearch(v2.3.2 / Windows 7)本地节点在java测试中创建索引,该测试抛出空指针异常。

测试代码如下:

CreateIndexResponse response =  esClient.admin().indices().prepareCreate("mytestindex")
       .setSettings(Settings.builder()
               .put("number_of_replicas", 0)
               .put("number_of_shards", 1))
       .execute().actionGet();

我已经验证esClient是由spring创建的,不是null。创建客户端的代码是:

@Bean
public Client localElasticsearchClient() {

    Settings settings = Settings.settingsBuilder()
            .put("path.home", "/tmp/elasticsearch")
            .put("path.data", "/tmp/elasticsearch")
            .build();

    return NodeBuilder.nodeBuilder()
            .local(true)
            .settings(settings)
            .build()
            .client();

}

堆栈跟踪如下:

java.lang.NullPointerException
at org.elasticsearch.action.support.master.TransportMasterNodeAction$AsyncSingleAction.<init>(TransportMasterNodeAction.java:129)
at org.elasticsearch.action.support.master.TransportMasterNodeAction.doExecute(TransportMasterNodeAction.java:107)
at org.elasticsearch.action.support.master.TransportMasterNodeAction.doExecute(TransportMasterNodeAction.java:51)
at org.elasticsearch.action.support.TransportAction.execute(TransportAction.java:137)
at org.elasticsearch.action.support.TransportAction.execute(TransportAction.java:85)
at org.elasticsearch.client.node.NodeClient.doExecute(NodeClient.java:58)

我看到了关于它的here,但没有关于如何纠正它的信息。

1 个答案:

答案 0 :(得分:0)

实际上答案是隐式在此link

中给出

我没有调用node.start()方法,即

return NodeBuilder.nodeBuilder()
        .local(true)
        .settings(settings)
        .build()
        .start()
        .client();