spring boot 2.x 整合 elasticsearch 7.x rest-high-level-client 方式

背景:最近 向es 存取数据,编写一个es工具类,

           试了很多种集成方式,因为es 7.3版本太新,走了不少弯路,只写了一些简单的存储查询,有需要的可以直接拿去参考

           代码已上传

版本:

          spring boot  2.1.6.RELEASE

          elasticsearch 7.3.0

          JDK 1.8

 

1 新建一个boot 项目

  添加 pom 依赖



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

    
        org.elasticsearch.client
        elasticsearch-rest-client
        7.3.0
    

    
    
        com.alibaba
        fastjson
        1.2.58
    

    
    
        org.projectlombok
        lombok
    

    
        org.apache.directory.studio
        org.apache.commons.collections
        3.2.1
    

    
        org.apache.commons
        commons-lang3
        3.8
    

    
        org.springframework.boot
        spring-boot-starter-web
    

2 配置 application.yml文件

elasticSearch:
  # 集群地址,多个用,隔开
  hosts: 127.0.0.1
  port: 9200
  # 使用的协议
  schema: http
  client:
    # 连接超时时间
    connectTimeOut: 1000
    # 连接超时时间
    socketTimeOut: 3000
    # 获取连接的超时时间
    connectionRequestTimeOut: 500
    # 最大连接数
    maxConnectNum: 50
    # 最大路由连接数
    maxConnectPerRoute: 50

 

3 编写es配置类

package com.example.config;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: ElasticSearch 配置类
 * @Author: karma
 * @Date: 2019/11/25 3:11 下午
 */
@Configuration
public class ElasticSearchConfig {

    /**
     * 集群地址,多个用,隔开
     **/
    @Value("${elasticSearch.hosts}")
    private String hosts;

    /**
     * 端口号
     **/
    @Value("${elasticSearch.port}")
    private int port;

    /**
     * 使用的协议
     **/
    @Value("${elasticSearch.schema}")
    private String schema;

    /**
     * 连接超时时间
     **/
    @Value("${elasticSearch.client.connectTimeOut}")
    private int connectTimeOut;

    /**
     * 连接超时时间
     **/
    @Value("${elasticSearch.client.socketTimeOut}")
    private int socketTimeOut;

    /**
     * 获取连接的超时时间
     **/
    @Value("${elasticSearch.client.connectionRequestTimeOut}")
    private static int connectionRequestTimeOut;

    /**
     * 最大连接数
     **/
    @Value("${elasticSearch.client.maxConnectNum}")
    private static int maxConnectNum;

    /**
     *  最大路由连接数
     **/
    @Value("${elasticSearch.client.maxConnectPerRoute}")
    private static int maxConnectPerRoute;


    private List hostList = new ArrayList<>();

    @PostConstruct
    private  void init(){
        hostList = new ArrayList<>();
        String[] hostArray = hosts.split(",");
        for (String host : hostArray) {
            hostList.add(new HttpHost(host, port, schema));
        }
    }

    @Bean
    public RestHighLevelClient getRestHighLevelClient() {

        RestClientBuilder builder = RestClient.builder(hostList.toArray(new HttpHost[0]));
        // 异步httpclient连接延时配置
        builder.setRequestConfigCallback(requestConfigBuilder -> {
            requestConfigBuilder.setConnectTimeout(connectTimeOut);
            requestConfigBuilder.setSocketTimeout(socketTimeOut);
            requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut);
            return requestConfigBuilder;
        });
        // 异步httpclient连接数配置
        builder.setHttpClientConfigCallback(httpClientBuilder -> {
            httpClientBuilder.setMaxConnTotal(maxConnectNum);
            httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
            return httpClientBuilder;
        });
        RestHighLevelClient client = new RestHighLevelClient(builder);
        return client;
    }

}

4  编写 ElasticSearchUtils

package com.example.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.*;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryRequest;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: ElasticSearch 工具类
 * @Author: karma
 * @Date: 2019/11/25 10:34 上午
 */
@Component
public class ElasticSearchUtils {

    @Resource
    private RestHighLevelClient restHighLevelClient ;

   ============查询======================================

    /**
     * description: 多字段匹配查询
     * @author karma
     * @param index 索引
     * @param fieldMap 字段map集合
     * @return java.lang.String
     * @date 2019/11/27 3:47 下午
     **/
    public String getByMultiFieldNames(String index, Map fieldMap) throws IOException{
        if(StringUtils.isBlank(index) || MapUtils.isEmpty(fieldMap)){
            return null;
        }
        SearchRequest searchRequest = new SearchRequest(index);

        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //循环传入搜索参数
        fieldMap.forEach((key, value) ->{
            boolQueryBuilder.must(QueryBuilders.termQuery(key,value));
        });

        sourceBuilder.query(boolQueryBuilder);
        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);
        return handleSearchResponse2Json(searchResponse);
    }


    /**
     * description: 根据id 查询
     * @author karma
     * @param index 索引
     * @param id (es数据id  _id)
     * @return java.lang.String
     * @date 2019/11/25 1:53 下午
     **/
    public String getByIndexAndId(String index, String id)throws IOException{
        if(StringUtils.isBlank(index) || StringUtils.isBlank(id)){
            return null;
        }

        GetRequest getRequest = new GetRequest(index,id);
        GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
        return getResponse.getSourceAsString();
    }

    /**
     * description: 根据索引查询
     * @author karma
     * @param index 索引
     * @param pageNum 第几页
     * @param pageSize 每页条数
     * @return java.lang.String
     * @date 2019/11/25 1:54 下午
     **/
    public String getByIndex(String index, int pageNum, int pageSize)throws IOException{
        if(StringUtils.isBlank(index)){
            return null;
        }

        // 搜索请求
        SearchRequest searchRequest = new SearchRequest(index);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        // 分页
        if(pageNum >= 0 && pageSize >= 0){
            searchSourceBuilder.from(pageSize*(pageNum - 1));
            searchSourceBuilder.size(pageSize);
        }else {
            // 如果不传分页参数 默认给20条数据
            searchSourceBuilder.from(0);
            searchSourceBuilder.size(19);
        }

        // 查询条件
        MatchAllQueryBuilder queryBuilder = QueryBuilders.matchAllQuery();

        // 传入搜索条件
        searchSourceBuilder.query(queryBuilder);
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);
        return handleSearchResponse2Json(searchResponse);
    }


    /**
     * description: 根据字段查询
     * @author karma
     * @param index 索引
     * @param fileName 字段名
     * @param value 字段值
     * @return java.lang.String
     * @date 2019/11/27 4:00 下午
     **/
    public String getByFieldName(String index, String fileName,String value) throws IOException{

        if(StringUtils.isBlank(index) || StringUtils.isBlank(fileName) || StringUtils.isBlank(value)){
            return null;
        }

        SearchRequest searchRequest = new SearchRequest(index);

        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        QueryBuilder queryBuilder = QueryBuilders.matchQuery(fileName,value);
        sourceBuilder.query(queryBuilder);

        searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);
        return handleSearchResponse2Json(searchResponse);
    }

  

    /**
     * description: 将SearchResponse 取出数据 转换成json
     * @author karma
     * @param searchResponse
     * @return java.lang.String
     * @date 2019/11/27 2:33 下午
     **/
    private String handleSearchResponse2Json(SearchResponse searchResponse){

        SearchHit[] hits = searchResponse.getHits().getHits();

        if(hits.length == 0){
            return null;
        }

        List> dataList = new ArrayList<>(hits.length);
        for(int i=0; i< hits.length; i++){
            dataList.add(hits[i].getSourceAsMap());
        }
        return JSONObject.toJSONString(dataList);
    }

}

你可能感兴趣的:(spring boot 2.x 整合 elasticsearch 7.x rest-high-level-client 方式)