因某些原因,我无法直接使用spring-data-elasticsearch,只能转为使用elastic search client, 结果一直各种错。

//从网上Copy过来的一段代码
Settings esSettings = Settings.builder()
        .put("cluster.name", "elasticsearch") //设置ES实例的名称
        .put("client.transport.sniff", true) //自动嗅探整个集群的状态,把集群中其他ES节点的ip添加到本地的客户端列表中
        .build();
TransportClient  client = new PreBuiltTransportClient(esSettings);//初始化client较老版本发生了变化,此方法有几个重载方法,初始化插件等。
//此步骤添加IP,至少一个,其实一个就够了,因为添加了自动嗅探配置
client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
Map<String,Object> infoMap = new HashMap<String, Object>();
infoMap.put("name", "广告信息11");
infoMap.put("title", "我的广告22");
infoMap.put("createTime", new Date());
infoMap.put("count", 1022);
IndexResponse indexResponse = client.prepareIndex("test", "info","100").setSource(infoMap).execute().actionGet();
System.out.println("id:"+indexResponse.getId());

client.close();

出现的错误:

  • java.lang.NoClassDefFoundError: org/elasticsearch/plugins/NetworkPlugin
  • java.lang.VerifyError: class org.elasticsearch.transport.Netty3Plugin overrides final method onModule.(Lorg/elasticsearch/common/network/NetworkModule;)

都是些找不到类或者是类不匹配的问题。项目中我引入的版本号是,和官网 Getting Started是一样的

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>transport</artifactId>
    <version>5.0.2</version>
</dependency>
<!--加没加下边这个,错误的类型还不一样-->
<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>5.0.2</version>
</dependency>

但是,查看dependency居然变成这样了,可以看到有些包的版本号变成了最新的5.6.12

\- org.elasticsearch.client:transport:jar:5.0.2:compile
[INFO]    +- org.elasticsearch:elasticsearch:jar:5.6.12:compile
[INFO]    |  +- org.apache.lucene:lucene-core:jar:6.6.1:compile
[INFO]    |  +- org.apache.lucene:lucene-analyzers-common:jar:6.6.1:compile
[INFO]    |  +- org.apache.lucene:lucene-backward-codecs:jar:6.6.1:compile
[INFO]    |  +- org.apache.lucene:lucene-grouping:jar:6.6.1:compile
[INFO]    |  +- org.apache.lucene:lucene-highlighter:jar:6.6.1:compile
[INFO]    |  +- org.apache.lucene:lucene-join:jar:6.6.1:compile
[INFO]    |  +- org.apache.lucene:lucene-memory:jar:6.6.1:compile
[INFO]    |  +- org.apache.lucene:lucene-misc:jar:6.6.1:compile
[INFO]    |  +- org.apache.lucene:lucene-queries:jar:6.6.1:compile
[INFO]    |  +- org.apache.lucene:lucene-queryparser:jar:6.6.1:compile
[INFO]    |  +- org.apache.lucene:lucene-sandbox:jar:6.6.1:compile
[INFO]    |  +- org.apache.lucene:lucene-spatial:jar:6.6.1:compile
[INFO]    |  +- org.apache.lucene:lucene-spatial-extras:jar:6.6.1:compile
[INFO]    |  +- org.apache.lucene:lucene-spatial3d:jar:6.6.1:compile
[INFO]    |  +- org.apache.lucene:lucene-suggest:jar:6.6.1:compile
[INFO]    |  +- org.elasticsearch:securesm:jar:1.2:compile
[INFO]    |  +- net.sf.jopt-simple:jopt-simple:jar:5.0.2:compile
[INFO]    |  +- com.carrotsearch:hppc:jar:0.7.1:compile
[INFO]    |  +- joda-time:joda-time:jar:2.9.9:compile
[INFO]    |  +- com.fasterxml.jackson.core:jackson-core:jar:2.9.7:compile
[INFO]    |  +- com.fasterxml.jackson.dataformat:jackson-dataformat-smile:jar:2.9.7:compile
[INFO]    |  +- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:jar:2.9.7:compile
[INFO]    |  +- com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:jar:2.9.7:compile
[INFO]    |  +- com.tdunning:t-digest:jar:3.0:compile
[INFO]    |  +- org.hdrhistogram:HdrHistogram:jar:2.1.9:compile
[INFO]    |  +- org.apache.logging.log4j:log4j-api:jar:2.10.0:compile
[INFO]    |  \- org.elasticsearch:jna:jar:4.4.0-1:compile
[INFO]    +- org.elasticsearch.plugin:transport-netty3-client:jar:5.0.2:compile
[INFO]    |  \- io.netty:netty:jar:3.10.6.Final:compile
[INFO]    +- org.elasticsearch.plugin:transport-netty4-client:jar:5.6.12:compile
[INFO]    |  +- io.netty:netty-buffer:jar:4.1.29.Final:compile
[INFO]    |  +- io.netty:netty-codec:jar:4.1.29.Final:compile
[INFO]    |  +- io.netty:netty-codec-http:jar:4.1.29.Final:compile
[INFO]    |  +- io.netty:netty-common:jar:4.1.29.Final:compile
[INFO]    |  +- io.netty:netty-handler:jar:4.1.29.Final:compile
[INFO]    |  +- io.netty:netty-resolver:jar:4.1.29.Final:compile
[INFO]    |  +- io.netty:netty-transport:jar:4.1.29.Final:compile
[INFO]    |  +- org.locationtech.spatial4j:spatial4j:jar:0.6:compile
[INFO]    |  +- com.vividsolutions:jts:jar:1.13:compile
[INFO]    |  \- org.apache.logging.log4j:log4j-core:jar:2.10.0:compile
[INFO]    +- org.elasticsearch.plugin:reindex-client:jar:5.0.2:compile
[INFO]    |  \- org.elasticsearch.client:rest:jar:5.0.2:compile
[INFO]    |     +- org.apache.httpcomponents:httpclient:jar:4.5.6:compile
[INFO]    |     +- org.apache.httpcomponents:httpcore:jar:4.4.10:compile
[INFO]    |     +- org.apache.httpcomponents:httpasyncclient:jar:4.1.4:compile
[INFO]    |     +- org.apache.httpcomponents:httpcore-nio:jar:4.4.10:compile
[INFO]    |     +- commons-codec:commons-codec:jar:1.11:compile
[INFO]    |     \- commons-logging:commons-logging:jar:1.1.3:compile
[INFO]    +- org.elasticsearch.plugin:lang-mustache-client:jar:5.0.2:compile
[INFO]    |  \- com.github.spullara.mustache.java:compiler:jar:0.9.3:compile
[INFO]    \- org.elasticsearch.plugin:percolator-client:jar:5.0.2:compile

后来发现,是spring boot搞的鬼,我使用了spring-boot-starter-parent,这会导致引入的elastic search的包变化。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.6.RELEASE</version>
</parent>

为啥引入spring-boot-starter-parent会导致这个问题,这个暂时还不清楚。

解决方法倒是很简单,把被替换的包直接引入即可。

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>transport</artifactId>
    <version>5.0.2</version>
    <exclusions>
        <exclusion>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.elasticsearch.plugin</groupId>
            <artifactId>transport-netty4-client</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>5.0.2</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch.plugin</groupId>
    <artifactId>transport-netty4-client</artifactId>
    <version>5.0.2</version>
</dependency>