我正在尝试为Elasticsearch 5.1编写客户端,并尝试使用TransportClient对象,如此处所述(https://www.elastic.co/guide/en/elasticsearch/client/java-api/5.1/transport-client.html)。
为了测试这个客户端,我有一个简单的get函数,它使用ES站点(https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-get.html)上记录的Get API。
但是,我的代码甚至不执行我的get函数,而是在创建我在客户端类的构造函数中创建的PreBuiltTransportClient对象时抛出错误。下面是代码片段。从第一行抛出错误。
PreBuiltTransportClient preBuiltTransportClient = new PreBuiltTransportClient(Settings.EMPTY);
try{
this.esClient = (TransportClient) preBuiltTransportClient.addTransportAddress(
new InetSocketTransportAddress(InetAddress.getByName(esHost), esPort));
} catch (UnknownHostException ex) {
LOGGER.error("Host cannot be reached: ", ex);
preBuiltTransportClient.close();
this.esClient.close();
}
以下是我使用get API测试客户端的方法。搜索值来自elasticsearch github示例(https://github.com/elastic/elasticsearch)。
//for testing purposes
public void testGet(String index, String type, String id) {
GetResponse response = esClient.prepareGet(index, type, id).get();
System.out.println("testGet response: " + response);
}
public static void main(String[] args) {
ESClient = new ESClient("localhost", 9200);
String index = "twitter";
String type = "tweet";
String id = "kimchy";
client.testGet(index, type, id);
}
最后,下面是错误的完整堆栈跟踪:
Exception in thread "main" java.lang.NoSuchFieldError: LUCENE_5_4_1
at org.elasticsearch.Version.<clinit>(Version.java:57)
at org.elasticsearch.common.io.stream.StreamOutput.<init>(StreamOutput.java:74)
at org.elasticsearch.common.io.stream.BytesStreamOutput.<init>(BytesStreamOutput.java:60)
at org.elasticsearch.common.io.stream.BytesStreamOutput.<init>(BytesStreamOutput.java:57)
at org.elasticsearch.common.io.stream.BytesStreamOutput.<init>(BytesStreamOutput.java:47)
at org.elasticsearch.common.xcontent.XContentBuilder.builder(XContentBuilder.java:69)
at org.elasticsearch.common.settings.Setting.arrayToParsableString(Setting.java:726)
at org.elasticsearch.common.settings.Setting.lambda$listSetting$26(Setting.java:672)
at org.elasticsearch.common.settings.Setting$2.getRaw(Setting.java:676)
at org.elasticsearch.common.settings.Setting.lambda$listSetting$24(Setting.java:660)
at org.elasticsearch.common.settings.Setting.listSetting(Setting.java:665)
at org.elasticsearch.common.settings.Setting.listSetting(Setting.java:660)
at org.elasticsearch.common.network.NetworkService.<clinit>(NetworkService.java:50)
at org.elasticsearch.client.transport.TransportClient.newPluginService(TransportClient.java:91)
at org.elasticsearch.client.transport.TransportClient.buildTemplate(TransportClient.java:119)
at org.elasticsearch.client.transport.TransportClient.<init>(TransportClient.java:247)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:92)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:81)
at org.elasticsearch.transport.client.PreBuiltTransportClient.<init>(PreBuiltTransportClient.java:71)
at customclient.<init>(ESClient.java:43)
at customclient.main(ESClient.java:112)
我不确定这个错误是由什么造成的,或者我可能做错了什么,并希望得到一些帮助!谢谢!