springboot集成Elasticsearch6.X

springboot官方提供的集成elasticsearch版本目前不支持ES6,https://github.com/spring-projects/spring-data-elasticsearch,所以只能自己集成了。

本来使用的Java Low Level REST Client,但是无奈各种问题。最后发现官方推荐使用Java High Level REST Client。然后使用这种方式集成成功。下面将步骤:

1.elasticsearch配置类

package com.zc.docsearch.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;


/**
 * @author zengchao
 *         elasticsearch spring-data 目前支持的最高版本为5.5 所以需要自己注入生成客户端
 *         这个三个接口可以使用  AbstractFactoryBean  通过继承重写方法代替
 *         Created by zengchao on 2018/4/19.
 */
@Configuration
public class ElasticsearchConfiguration implements FactoryBean, InitializingBean, DisposableBean {
    private static final Logger LOG = LoggerFactory.getLogger(ElasticsearchConfiguration.class);

    @Value("${spring.data.elasticsearch.cluster-nodes}")
    private String clusterNodes;

    private RestHighLevelClient restHighLevelClient;

    /**
     * 控制Bean的实例化过程
     *
     * @return
     * @throws Exception
     */
    @Override
    public RestHighLevelClient getObject() throws Exception {
        return restHighLevelClient;
    }

    /**
     * 获取接口返回的实例的class
     *
     * @return
     */
    @Override
    public Class getObjectType() {
        return RestHighLevelClient.class;
    }

    @Override
    public void destroy() throws Exception {
        try {
            if (restHighLevelClient != null) {
                restHighLevelClient.close();
            }
        } catch (final Exception e) {
            LOG.error("Error closing ElasticSearch client: ", e);
        }
    }

    @Override
    public boolean isSingleton() {
        return false;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        restHighLevelClient = buildClient();
    }

    private RestHighLevelClient buildClient() {
        try {
            restHighLevelClient = new RestHighLevelClient(
                    RestClient.builder(
                            new HttpHost(
                                    clusterNodes.split(":")[0],
                                    Integer.parseInt(clusterNodes.split(":")[1]),
                                    "http")));
        } catch (Exception e) {
            LOG.error(e.getMessage());
        }
        return restHighLevelClient;
    }
}
2.application.properties

spring.data.elasticsearch.cluster-nodes=112.74.63.235:9200

3.测试

@Test
public void getIndexTest(){
   GetRequest request=new GetRequest("store","product","4");
   try {
           GetResponse response = client.get(request);
           System.out.println(response);
       } catch (IOException e) {
      e.printStackTrace();
   }
}

4.pom.xml 请根据自己版本修改



   org.elasticsearch
   elasticsearch
   6.2.3


   org.elasticsearch.client
   elasticsearch-rest-high-level-client
   6.2.3


   org.apache.logging.log4j
   log4j-api
   2.8.2


   org.apache.logging.log4j
   log4j-core
   2.8.2


   org.elasticsearch.client
   elasticsearch-rest-client
   6.2.3


   org.elasticsearch.client
   elasticsearch-rest-client-sniffer
   6.2.3

5.官方链接

官方建议:https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.2/java-api.html

Java High Level REST Client API文档https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.2/java-rest-high.html


你可能感兴趣的:(springboot)