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();
        }
  • 相关阅读:
    09-2:跳台阶
    09:菲波那切数列
    08:旋转数组的最小值
    07:用两个栈实现队列
    06:重建二叉树
    05:从尾到头打印链表
    04:替换字符
    centos7安装Jenkins更改默认端口并配置Ldap服务器进行用户认证
    Jira配置openLdap服务器进行用户认证
    定时自动从FTP服务器取数据脚本
  • 原文地址:https://www.cnblogs.com/zq1003/p/14840684.html
Copyright © 2011-2022 走看看