我尝试使用java API对弹性搜索进行索引,然后在Kibana中使用该数据进行可视化,如下所示:
package elasticSearchTest;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import java.net.InetAddress;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.testng.annotations.Test;
public class ES_Test_Class {
@Test
public void f() {
try{
Client client = TransportClient.builder().build()
.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
.setSource(jsonBuilder()
.startObject()
.field("user", "kimchy")
.field("postDate", "18/May/2011:01:48:10")
.field("message", "trying out Elasticsearch")
.endObject()
)
.get();
// Document ID (generated or not)
String _id = response.getId();
// Version (if it's the first time you index this document, you will get: 1)
long _version = response.getVersion();
System.out.println("Document id is: "+_id);
System.out.println("Document version is: "+_version);
}
catch (Exception e){
e.printStackTrace();
}
}
}
以下是更新的依赖项:
然而,当我运行代码时,我得到以下错误:
[TestNG]正在运行: C:\ Users \用户vinbhask \应用程序数据\本地的\ Temp \ TestNG的月食 - 931338640 \ TestNG的-customsuite.xml 2016年11月4日下午9:03:52 org.elasticsearch.plugins.PluginsService 信息:[Kismet]模块[],插件[],网站[] 失败:f java.lang.NoSuchFieldError:LUCENE_5_2_1 在org.elasticsearch.Version。(Version.java:265) at org.elasticsearch.client.transport.TransportClient $ Builder.build(TransportClient.java:129) 在elasticSearchTest.ES_Test_Class.f(ES_Test_Class.java:17) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:497) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84) 在org.testng.internal.Invoker.invokeMethod(Invoker.java:714) 在org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127) 在org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
我尝试使用谷歌搜索解决方案并遇到了this link和this link,但无法理解如何解决问题,我甚至通过一次添加一个JAR重建项目但没有帮助
[更新]: 添加lucene 5.5.2 jar后得到以下错误:
[TestNG]正在运行: C:\ Users \用户vinbhask \应用程序数据\本地的\ Temp \ TestNG的月食 - 545647994 \ TestNG的-customsuite.xml 2016年11月6日上午9:18:20 org.elasticsearch.plugins.PluginsService 信息:[Locus] modules [],plugins [],sites [] 失败:f java.lang.NoSuchMethodError:org.jboss.netty.channel.socket.nio.NioWorkerPool。(Ljava / util / concurrent / Executor; I)V 在org.elasticsearch.transport.netty.NettyTransport.createClientBootstrap(NettyTransport.java:354) 在org.elasticsearch.transport.netty.NettyTransport.doStart(NettyTransport.java:290) 在org.elasticsearch.common.component.AbstractLifecycleComponent.start(AbstractLifecycleComponent.java:68) at org.elasticsearch.transport.TransportService.doStart(TransportService.java:182) 在org.elasticsearch.common.component.AbstractLifecycleComponent.start(AbstractLifecycleComponent.java:68) at org.elasticsearch.client.transport.TransportClient $ Builder.build(TransportClient.java:162) 在elasticSearchTest.ES_Test_Class.f(ES_Test_Class.java:17) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:497) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
答案 0 :(得分:2)
您正在使用Lucene 4.10.4,但Elasticsearch 2.4.1基于Lucene 5.5.2。升级您的Lucene版本(5.5.2 is here)。