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();
        }
  • 相关阅读:
    TP5 自带封装分页查询
    TP5 接口 前面拼接 当前域名
    button按钮点击后过段时间不能点击-验证码(刷新后失效)
    关于PHP -session 的 A session had already been started – ignoring session_start() 错误
    301 Moved Permanently
    PHP实现远程下载图片与 图片接口定义
    在TP框架的前端中 想引用 PHP的函数
    流程控制语句2
    流程控制语句
    数据库查询数据2
  • 原文地址:https://www.cnblogs.com/zq1003/p/14840684.html
Copyright © 2011-2022 走看看