我在Java中有一个简单的弹性搜索代码,如下所示:
public class TryElastic {
public static void main(String[] args) throws UnknownHostException {
Map<String, Object> json = new HashMap<String, Object>();
json.put("user","kimchy");
json.put("postDate",new Date());
json.put("message","trying out Elasticsearch");
try {
Settings settings = Settings.settingsBuilder()
.put("cluster.name", "elasticsearch")
.put("client.transport.sniff", true).build();
TransportClient client = TransportClient.builder().settings(settings).build();
//client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
} catch (NoNodeAvailableException e) {
System.out.println(e.toString());
}
System.out.println("test");
}
}
这很简单,但我在下一行中收到错误:
TransportClient client = TransportClient.builder().settings(settings).build();
它会显示如下错误消息:
Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:113)
at org.elasticsearch.node.internal.InternalSettingsPreparer.randomNodeName(InternalSettingsPreparer.java:198)
at org.elasticsearch.node.internal.InternalSettingsPreparer.finalizeSettings(InternalSettingsPreparer.java:177)
at org.elasticsearch.node.internal.InternalSettingsPreparer.prepareSettings(InternalSettingsPreparer.java:64)
at org.elasticsearch.client.transport.TransportClient$Builder.build(TransportClient.java:119)
at TryElastic.main(TryElastic.java:64)
那么,你能给我一些建议吗,我一直在寻找解决方案,但我无法找到。我是否有任何配置要实施?我的elasticsearch服务器已正确运行,它可以执行索引并从命令提示符获取查询。感谢...
答案 0 :(得分:3)
此代码适用于我。这是版本问题。 TransportClient的默认端口为9300,代码无法使用Elasticsearch 2.0进行编译
使用此:
设置设置= Settings.settingsBuilder() .put(“cluster.name”,clusterName).build();
客户端客户端= TransportClient.builder()。settings(settings).build() .addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress(“127.0.0.1”,9300)));