最新elasticsearch7.16 javaapi复杂查询实例

最新elasticsearch7.16 javaapi复杂查询实例,第1张

最新elasticsearch7.16 javaapi复杂查询实例
 @Override
    public IPage healthManagerSearchByEs(PageParams healthManagerSearchVO) throws IOException {
        SearchRequest searchRequest = new SearchRequest(TenantConstants.DOCTOR);
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 分页
        searchSourceBuilder.from((int) healthManagerSearchVO.getCurrent() - 1);
        searchSourceBuilder.size((int) healthManagerSearchVO.getSize());
		// 过滤返回的字段
        String[] fields = {"id", "doctorName", "avatar", "title", "workUnit",
                "department", "isEnable", "approvalStatus", "doctorType", "genius", "describe", "isOnline", "createTime"};
        searchSourceBuilder.fetchSource(fields, null);
        // 排序
        searchSourceBuilder.sort(new FieldSortBuilder("createTime").order(SortOrder.DESC));
        String keyword = healthManagerSearchVO.getModel().getKeyword();
        // 组合查询 must相当于sql中的and
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
                .must(QueryBuilders.matchQuery("isOnline", true))
                .must(QueryBuilders.matchQuery("isEnable", true))
                .must(QueryBuilders.matchQuery("approvalStatus", DoctorApprovalStatus.PASS));
        if (StrUtil.isNotBlank(keyword)) {
              // 多字段查询 multiMatchQuery
            boolQueryBuilder.must(QueryBuilders.multiMatchQuery(keyword, "doctorName", "genius"));
        }
        searchSourceBuilder.query(boolQueryBuilder);
        searchRequest.source(searchSourceBuilder);
        SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
		// 封装返参
        IPage page = new Page<>();
        page.setCurrent(healthManagerSearchVO.getCurrent());
        page.setSize(healthManagerSearchVO.getSize());
        page.setTotal(response.getHits().getHits().length);
        page.setPages((response.getHits().getHits().length + healthManagerSearchVO.getSize() - 1)
                / healthManagerSearchVO.getSize());
        List records = new ArrayList<>();
        for (SearchHit hit : response.getHits().getHits()) {
            Map sourceAsMap = hit.getSourceAsMap();
            DoctorSearchResponseDTO doctorSearchResponseDTO = new DoctorSearchResponseDTO();
            DoctorSearchResponseDTO newDTO = BeanUtil.fillBeanWithMap(sourceAsMap, doctorSearchResponseDTO, false);
            records.add(newDTO);
        }
        return page.setRecords(records);
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5676430.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存