elasticsearch使用restHighLevelClient查询所有数据

elasticsearch查询数据时会默认分查询全量数据不太方便,所以写了这篇适配restHighLevelClient框架的查询文章

1.maven

        <dependency>
			<groupId>org.elasticsearch.client</groupId>
			<artifactId>elasticsearch-rest-high-level-client</artifactId>
			<version>6.4.3</version>
		</dependency>

2. SearchAllBuilder.class

import org.elasticsearch.search.builder.SearchSourceBuilder;


public class SearchAllBuilder {
    private String indices;
    private String types;
    private SearchSourceBuilder searchSourceBuilder;
    private RequestOptions options = RequestOptions.DEFAULT;
    private Integer propertyNamingStrategy = PropertyNamingStrategyConstant.ORIGINAL;

    public SearchAllBuilder() {

    }

    public SearchAllBuilder indices(String indices) {
        this.indices = indices;
        return this;
    }

    public SearchAllBuilder types(String types) {
        this.types = types;
        return this;
    }

    public SearchAllBuilder searchSourceBuilder(SearchSourceBuilder searchSourceBuilder) {
        this.searchSourceBuilder = searchSourceBuilder;
        return this;
    }

    public SearchAllBuilder options(RequestOptions options) {
        this.options = options;
        return this;
    }

    public SearchAllBuilder propertyNamingStrategy(Integer propertyNamingStrategy) {
        this.propertyNamingStrategy = propertyNamingStrategy;
        return this;
    }

    String getIndices() {
        return indices;
    }

    String getTypes() {
        return types;
    }

    SearchSourceBuilder getSearchSourceBuilder() {
        return searchSourceBuilder;
    }

    RequestOptions getOptions() {
        return options;
    }

    Integer getPropertyNamingStrategy() {
        return propertyNamingStrategy;
    }
}

3.EsSearchApiService.class

import java.io.IOException;
import java.util.List;

import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.stereotype.Service;

@Service
public interface EsSearchApiService {

     /**
     * 查询全量(其实也是分页循环查询)
     * 
     * @param searchAllBuilder
     *            查询所有的builder
     * @param rClass
     *            返回对象的类
     * @param <T>
     *            泛型
     * @return 返回查询出来的全量数据
     * @throws IOException
     *             抛出io异常
     */
    <T> List<T> searchAll(SearchAllBuilder searchAllBuilder, Class<T> rClass) throws IOException;

}

4.EsSearchApiServiceImpl.class

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import org.springframework.util.CollectionUtils;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class EsSearchApiServiceImpl implements EsSearchApiService {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    private static final String COMMA_SEPARATE = ",";

    /**
     * 查询全量(其实也是分页循环查询)
     *
     * @param indices
     *            索引名称,多个时逗号分隔
     * @param types
     *            索引类型名称,多个时逗号分隔
     * @param searchSourceBuilder
     *            查询的builder
     * @param options
     *            请求类型
     * @param tClass
     *            返回对象的类
     * @param propertyNamingStrategy
     *            转换策略(PropertyNamingStrategyConstant.class)
     * @param <T>
     *            泛型
     * @return 返回查询出来的全量数据
     * @throws IOException
     *             抛出io异常
     */
    public <T> List<T> searchAll(@NonNull String indices, @NonNull String types,
        @NonNull SearchSourceBuilder searchSourceBuilder, @NonNull RequestOptions options, @NonNull Class<T> tClass,
        Integer propertyNamingStrategy) throws IOException {

        // 校验索引
        String[] indexArray = indices.split(COMMA_SEPARATE);
        Objects.requireNonNull(indices, "indices must not be null");
        for (String index : indexArray) {
            Objects.requireNonNull(index, "index must not be null");
        }

        // 校验类型
        Objects.requireNonNull(types, "types must not be null");
        String[] typeArray = types.split(COMMA_SEPARATE);
        for (String type : typeArray) {
            Objects.requireNonNull(type, "type must not be null");
        }

        if (PropertyNamingStrategyConstant.ORIGINAL.equals(propertyNamingStrategy)) {
        } else if (PropertyNamingStrategyConstant.UNDERSCORE_TO_CAMEL.equals(propertyNamingStrategy)) {
        } else {
            throw new RuntimeException("propertyNamingStrategy is not found");
        }

        return searchAll(indexArray, typeArray, searchSourceBuilder, options, tClass, propertyNamingStrategy);
    }

    /**
     * 查询全量(其实也是分页循环查询)
     *
     * @param indices
     *            索引名称数组
     * @param types
     *            索引类型名称数组
     * @param searchSourceBuilder
     *            查询的builder
     * @param options
     *            请求类型
     * @param tClass
     *            返回对象的类
     * @param propertyNamingStrategy
     *            转换策略(PropertyNamingStrategyConstant.class)
     * @param <T>
     *            泛型
     * @return 返回查询出来的全量数据
     * @throws IOException
     *             抛出io异常
     */
    private <T> List<T> searchAll(String[] indices, String[] types, SearchSourceBuilder searchSourceBuilder,
        RequestOptions options, Class<T> tClass, Integer propertyNamingStrategy) throws IOException {
        List<T> allList = new ArrayList<>();
        List<T> list;
        // 设置分页尺寸
        int from = -1 == searchSourceBuilder.from() ? 0 : searchSourceBuilder.from(),
            size = -1 == searchSourceBuilder.size() ? 5000 : searchSourceBuilder.size(), page = 0;
        do {
            // 组装查询条件
            SearchRequest searchRequest = new SearchRequest().indices(indices);
            // 类型名称不为空时,才组装type
            if (types != null && types.length > 0) {
                searchRequest.types(types);
            }
            searchRequest.source(searchSourceBuilder.from(from).size(size));

            SearchResponse searchResponse = restHighLevelClient.search(searchRequest, options);
            SearchHits searchHits = searchResponse.getHits();
            SearchHit[] searchHitsHits = searchHits.getHits();
            long totalHits = searchHits.getTotalHits();

            // 查询结果
            list = Arrays.stream(searchHitsHits).map(searchHit -> {
                if (ORIGINAL.equals(propertyNamingStrategy)) {
                    return MapObjectUtil.originalMapParseObject(searchHit.getSourceAsMap(), tClass);
                } else if (PropertyNamingStrategyConstant.UNDERSCORE_TO_CAMEL.equals(propertyNamingStrategy)) {
                    return MapObjectUtil.underscoreToCamelMapParseObject(searchHit.getSourceAsMap(), tClass);
                } else {
                    throw new RuntimeException("propertyNamingStrategy is not found");
                }

            }).collect(Collectors.toList());
            // 将查询结果投入allList
            allList.addAll(list);

            // 当命中小于分页尺寸时直接跳出
            if (totalHits < size) {
                break;
            }
            // 翻页
            page++;
            from = (page + 1) * size;
            // 查询直到分页的数据为null
        } while (!CollectionUtils.isEmpty(list));

        return allList;
    }

    /**
     * 查询全量(其实也是分页循环查询)
     *
     * @param indices
     *            索引名称,多个时逗号分隔
     * @param searchSourceBuilder
     *            查询的builder
     * @param options
     *            请求类型
     * @param tClass
     *            返回对象的类
     * @param propertyNamingStrategy
     *            转换策略(PropertyNamingStrategyConstant.class)
     * @param <T>
     *            泛型
     * @return 返回查询出来的全量数据
     * @throws IOException
     *             抛出io异常
     */
    public <T> List<T> searchAll(@NonNull String indices, @NonNull SearchSourceBuilder searchSourceBuilder,
        @NonNull RequestOptions options, @NonNull Class<T> tClass, Integer propertyNamingStrategy) throws IOException {
        String[] indexArray = indices.split(COMMA_SEPARATE);
        Objects.requireNonNull(indices, "indices must not be null");
        for (String index : indexArray) {
            Objects.requireNonNull(index, "index must not be null");
        }

        if (ORIGINAL.equals(propertyNamingStrategy)) {
        } else if (PropertyNamingStrategyConstant.UNDERSCORE_TO_CAMEL.equals(propertyNamingStrategy)) {
        } else {
            throw new RuntimeException("propertyNamingStrategy is not found");
        }

        return searchAll(indexArray, null, searchSourceBuilder, options, tClass, propertyNamingStrategy);
    }

/**
     * 查询全量(其实也是分页循环查询)
     *
     * @param searchAllBuilder
     *            查询所有的builder
     * @param rClass
     *            返回对象的类
     * @param <T>
     *            泛型
     * @return 返回查询出来的全量数据
     * @throws IOException
     *             抛出io异常
     */
    @Override
    public <T> List<T> searchAll(SearchAllBuilder searchAllBuilder, Class<T> rClass) throws IOException {

        // 无types的查询
        if (Objects.isNull(searchAllBuilder.getTypes())) {
            return this.searchAll(searchAllBuilder.getIndices(), searchAllBuilder.getSearchSourceBuilder(),
                searchAllBuilder.getOptions(), rClass, searchAllBuilder.getPropertyNamingStrategy());
        }
        return this.searchAll(searchAllBuilder.getIndices(), searchAllBuilder.getTypes(),
            searchAllBuilder.getSearchSourceBuilder(), searchAllBuilder.getOptions(), rClass,
            searchAllBuilder.getPropertyNamingStrategy());
    }
}

5.PropertyNamingStrategyConstant

public interface PropertyNamingStrategyConstant {
    /**
     * 原生转换
     */
    Integer ORIGINAL = 1;
    /**
     * 下划线转驼峰
     */
    Integer UNDERSCORE_TO_CAMEL = 2;
}

6.MapObjectUtil.class

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.PropertyNamingStrategy;
import com.alibaba.fastjson.serializer.SerializeConfig;

public class MapObjectUtil {
    /**
     * 将下划线key驼峰化的map转为原生object对象
     * 
     * @param map
     * @param beanClass
     * @param <T>
     * @return
     */
    public static <T> T underscoreToCamelMapParseObject(Map<String, Object> map, Class<T> beanClass) {
        if (map == null) {
            return null;
        }
        try {
            // 下划线转驼峰
            SerializeConfig config = new SerializeConfig();
            config.propertyNamingStrategy = PropertyNamingStrategy.SnakeCase;
            return JSON.parseObject(JSON.toJSONString(map, config), beanClass);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 将es原始key的map转为原生object对象
     *
     * @param map
     * @param beanClass
     * @param <T>
     * @return
     */
    public static <T> T originalMapParseObject(Map<String, Object> map, Class<T> beanClass) {
        if (map == null) {
            return null;
        }
        try {
            return JSON.parseObject(JSON.toJSONString(map), beanClass);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 将对象转为map
     * 
     * @param obj
     * @return
     */
    public static Map<String, Object> objectParseMap(Object obj) {
        if (obj == null) {
            return null;
        }

        return ((JSONObject)JSON.toJSON(obj)).getInnerMap();
    }
}

7.demo

 public List<ProductDTO> queryProduct(Integer productId) {

        try {
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder()
                    .query(QueryBuilders.termQuery("product_id", productId)).sort("price", SortOrder.DESC);

            SearchAllBuilder searchAllBuilder = new SearchAllBuilder()
                    .indices("productIndex")
                    .searchSourceBuilder(searchSourceBuilder)
                    .propertyNamingStrategy(PropertyNamingStrategyConstant.UNDERSCORE_TO_CAMEL);
            List<ProductDTO> productList = esSearchApiService.searchAll(searchAllBuilder,ProductDTO.class);
            log.info("查询数据:{}",productList);
            return productList;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Collections.emptyList();
    }

QQ交流群: 132312549

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值