elasticsearch java

作者: yangyangrenren | 来源:发表于2018-09-15 18:21 被阅读34次

    已经安装好了elasticsearch服务,使用java开发语言,还在纠结于使用哪一种client去连接操作elasticsearch么?
    目前有flummiJest、还有国人开发的bboss。我推荐使用官方支持的 Java High Level REST Client、或者spring-data-elasticsearch

    • 推荐使用官方支持的 Java High Level REST Client理由:elasticsearch官方在5.6.0版本(2017年9月11日)之后,提供了Java High Level REST Client的支持。而jest、flummi是从两三年之前就有的,那时候官方还没有提供REST Client的支持。具体操作,直接参考官网的api即可完成。

    The first release of the Java high-level REST client went out with the 6.0.0-beta1 release of the Elastic Stack. It allows you to communicate via REST with Elasticsearch nodes running 6.0.0-beta1, but it is not fully compatible with 5.x nodes. The 5.6.0 release of the Elastic Stack recently went out and includes the 5.6.0 version of the high-level client, which works against Elasticsearch nodes running version 5.6. It is unfortunately not compatible with previous 5.x versions of Elasticsearch.

    • 推荐使用spring-data-elasticsearch理由:复杂的mapping可以自动完成,无需额外创建mapping。

    当然,还有spring-data-elasticsearch的方式,可以连接操作elasticsearch。spring data elasticsearch方式,如果mapping结构是复杂的,这种方式还是可以省事很多。如果使用的elasticsearch服务,是比较新的;或者是需要将目前的项目中集成elasticsearch操作,但是目前项目中使用的spring版本比较低,集成时候出现了版本冲突异常,那么还是推荐使用Java High Level REST Client。

    spring data elasticsearch

    spring data elasticsearch elasticsearch
    3.1.x 6.2.2
    3.0.x 5.5.0
    2.1.x 2.4.0
    2.0.x 2.2.0
    1.3.x 1.5.2

    参考spring-projects,而且这里提供了一个demo,可以使用spring data elasticsearch。

    如果要引用3.1.0 RC2版本,需要参考官网spring-data-elasticsearch,加入maven repositories,因为目前maven官方,只支持到3.0.10.RELEASE(2018年9月10日更新)

    search.maven.org
    dependencies {
        compile 'org.springframework.data:spring-data-elasticsearch:3.1.0.RC2'
    }repositories {
        maven {
            url 'https://repo.spring.io/libs-milestone'
        }
    }
    

    目前即使使用这种最新的spring-data-elasticsearch:3.1.0.RC2,其实spring data elasticsearch依然还只是用的elasticsearch 官方Java Low Level REST Client Java API的6.2.2版本,并没有支持到elasticsearch 6.3以上的版本,目前elasticsearch官方(August 23, 2018)已经有6.4.0版本了。

    elasticsearch.jpg

    spring data elasticsearch官方没说支持到6.3以上版本,至少还可以用,不会像elasticsearch-analysis-ik那样因为版本不匹配而无法启动elasticsearch。我在本地安装启动的elasticsearch6.3.1版本,使用下面这种gradle配置,spring data elasticsearch是完全可以运行的,基本的CRUD也是ok的。

    repositories {
        mavenCentral()
        maven {
            url 'https://repo.spring.io/libs-milestone'
        }
    }
    
    dependencies {
        compile 'org.springframework.data:spring-data-elasticsearch:3.1.0.RC2'
        compile 'org.springframework.boot:spring-boot:1.5.9.RELEASE'
        compile 'org.springframework.boot:spring-boot-starter-parent:1.5.9.RELEASE'
        compile 'org.springframework.boot:spring-boot-starter-web-services:1.5.9.RELEASE'
        compile 'org.projectlombok:lombok:1.18.2'
        testCompile group: 'junit', name: 'junit', version: '4.12'
    }
    

    注意到,我这里使用的是spring-boot是1.5.9.RELEASE版本,并没有像Spring Data Elasticsearch Spring Boot version matrix这样无法集成使用。

    https://github.com/spring-projects/spring-data-elasticsearch/wiki/Spring-Data-Elasticsearch---Spring-Boot---version-matrix

    另外,如果在elasticsearch官方已经更新了,但是elasticsearch-analysis-ik分词还没有更新,导致最新的elasticsearch无法使用旧版本ik分词而启动报错,也是可以有办法修改ik分词重新打包jar,使得最新的elasticsearch可以用上ik分词。

    elasticsearch官方,Java Low Level REST Client Java API将在elasticsearch7版本之后废弃,推荐使用Java High Level REST Client。

    We plan on deprecating the TransportClient in Elasticsearch 7.0 and removing it completely in 8.0. Instead, you should be using the Java High Level REST Client, which executes HTTP requests rather than serialized Java requests. The migration guidedescribes all the steps needed to migrate.

    来源于https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/client.html

    所以,elasticsearch更新这么频繁,spring data更新又这么慢,期待elasticsearch7发布之后,spring data又会在什么时候支持呢?毕竟目前spring data elasticsearch 3.1.*版本才只是支持到elasticsearch6.2.2版本,封装的还只是Java Low Level REST Client Java API。

    相关文章

      网友评论

        本文标题:elasticsearch java

        本文链接:https://www.haomeiwen.com/subject/ykdtnftx.html