ES 封装搜索记录

本次记录,学习参考大概。

 es 封装搜索

EsSearchService


public
interface EsSearchService {
/**
* 查询列表
* @param index
* @param from
* @param size
* @param query
* @param sort
* @param clazz
* @param <T>
* @return
* @throws IOException
*/
<T> List<T> getList(String index,//索引
int from,//分页
int size,//分页条数
QueryBuilder query,//复合条件查询
SortBuilder sort,//排序
Class<T> clazz) throws IOException;
}

EsSearchServiceImpl

@Override
public <T> List<T> getList(String index, int from, int size, QueryBuilder query, SortBuilder sort, Class<T> clazz) throws IOException {
//得到所有属性
Field[] fields = clazz.getDeclaredFields();
String[] includeFields = Stream.of(fields)
.map(Field::getName)
.collect(Collectors.collectingAndThen(Collectors.toList(),a->a.toArray(new String[fields.length])));
return getList(index, Strings.EMPTY_ARRAY,from,size,query,includeFields,null,sort,DEFAULT_TIME_VALUE,clazz);
}

@Override
    public <T> List<T> getList(String index,
                               String[] type,
                               int from,
                               int size,
                               QueryBuilder query,
                               String[] includeFields,
                               String[] excludeFields,
                               SortBuilder sort,
                               TimeValue timeValue,
                               Class<T> clazz) throws IOException {
        SearchHits searchHits = getSearchHits(index,type,from,size,query,includeFields,excludeFields,sort,timeValue);
        if(searchHits == null){
            return Collections.emptyList();
        }
        return Stream.of(searchHits.getHits())
                .map(SearchHit::getSourceAsString)
                .map(s-> {
                    try {
                        return objectMapper.readValue(s,clazz);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                })
                .collect(Collectors.toList());
    }

@Override
    public SearchHits getSearchHits(String index,
                                    String[] type,
                                    int from,
                                    int size,
                                    QueryBuilder query,
                                    String[] includeFields,
                                    String[] excludeFields,
                                    SortBuilder sort,
                                    TimeValue timeValue) throws IOException {
        SearchSourceBuilder searchSourceBuilder = SearchSourceBuilder.searchSource()
                .query(query)
                .from(from)
                .size(size)
                .timeout(timeValue)
                .fetchSource(includeFields, excludeFields);
        if(sort != null){
            searchSourceBuilder.sort(sort);
        }
        SearchRequest searchRequest = new SearchRequest(index)
                .types(type)
                .source(searchSourceBuilder);
        log.debug("es查询:{}",searchSourceBuilder.toString());
        return restHighLevelClient.search(searchRequest,
                RequestOptions.DEFAULT).getHits();
    }
原文地址:https://www.cnblogs.com/zq1003/p/14840684.html