zoukankan      html  css  js  c++  java
  • 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();
        }
  • 相关阅读:
    清除富文本样式
    jquery--cookie应用
    Log4j 配置详解
    判断请求是否为ajax
    日期工具类
    Windows Server2012 KB2919355 补丁无法安装
    安装系统步骤:
    大白菜u盘启动盘制作工具取消赞助商方法详解
    视频使用教程
    检查网络是否正常的几种命令
  • 原文地址:https://www.cnblogs.com/zq1003/p/14840684.html
Copyright © 2011-2022 走看看