数据库
首页 > 数据库> > elasticsearch实战-复合查询(复杂sql的es转换实现)

elasticsearch实战-复合查询(复杂sql的es转换实现)

作者:互联网

 海量订单数据用搜索引擎来实现检索

 基础篇请查看这里

demo展示了bool查询构建的嵌套查询


    public PageResponse<List<OrderNewDto>> getOrderNewList(OrderQryReq searchOrgReq) {
        String keyword = searchOrgReq.getKeyword().toLowerCase();
        BoolQueryBuilder conditionQueryBuilder = QueryBuilders.boolQuery();
        //兼容空格输入多个关键字的检索
        Iterator<String> kws = Splitter.on(" ").split(keyword).iterator();
        while (kws.hasNext()) {
            String kw = kws.next().trim();
            BoolQueryBuilder keywordQueryBuilder = QueryBuilders.boolQuery();
            //构建关键字查询
            keywordQueryBuilder.should(QueryBuilders.prefixQuery("order_no", kw));
            keywordQueryBuilder.should(QueryBuilders.prefixQuery("order_code", kw));
            keywordQueryBuilder.should(QueryBuilders.termQuery("username.pinyin", kw));
            keywordQueryBuilder.should(QueryBuilders.prefixQuery("username.pinyin", kw));
            keywordQueryBuilder.should(QueryBuilders.termQuery("username.ik", kw));
            keywordQueryBuilder.should(QueryBuilders.prefixQuery("phone.keyword", kw));
            conditionQueryBuilder.must(keywordQueryBuilder);
        }
        conditionQueryBuilder.must(QueryBuilders.termQuery("order_type", 1));
        //构建条件查询
        if (searchOrgReq.getCreateUserId() != null)
            conditionQueryBuilder.must(QueryBuilders.termQuery("xiaosr_id", searchOrgReq.getCreateUserId()));
        if (searchOrgReq.getUserId() != null)
            conditionQueryBuilder.must(QueryBuilders.termQuery("user_id", searchOrgReq.getUserId()));
        if (searchOrgReq.getOrderStatus() != null)
            conditionQueryBuilder.must(QueryBuilders.termQuery("order_status", searchOrgReq.getOrderStatus()));
        if (searchOrgReq.getIsEnter() != null && searchOrgReq.getIsEnter() == 1)
            conditionQueryBuilder.must(QueryBuilders.termQuery("payed_price", 0));
        if (searchOrgReq.getOrderStatus() != null)
            conditionQueryBuilder.must(QueryBuilders.termQuery("order_status", searchOrgReq.getOrderStatus()));
        if (searchOrgReq.getStartTime() != null)
            conditionQueryBuilder.must(QueryBuilders.rangeQuery("order_time").gte(LocalDateTime.parse(searchOrgReq.getStartTime(),
                    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).minusHours(8).toString()));
        if (searchOrgReq.getEndTime() != null)
            conditionQueryBuilder.must(QueryBuilders.rangeQuery("order_time").lte(LocalDateTime.parse(searchOrgReq.getEndTime(),
                    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")).minusHours(8).toString()));

        NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(StringUtils.isBlank(keyword) ? QueryBuilders.matchAllQuery() : conditionQueryBuilder);
        nativeSearchQuery.addIndices("order_new");
        nativeSearchQuery.addTypes("order");
        nativeSearchQuery.setPageable(PageRequest.of(searchOrgReq.getPage(), searchOrgReq.getPageSize()));
        nativeSearchQuery.addSort(Sort.by("order_time").descending());
        SearchHits searchHits = elasticsearchTemplate.query(nativeSearchQuery, SearchResponse::getHits);
        List<OrderNewDto> categories = new ArrayList<>();
        for (SearchHit searchHit : searchHits) {
            JSONObject jsonObject = new JSONObject(searchHit.getSourceAsMap());
            categories.add(jsonObject.toJavaObject(OrderNewDto.class));
        }
        return PageResponse.<List<OrderNewDto>>builder()
                .totalSize((int) searchHits.getTotalHits())
                .currentPage(searchOrgReq.getPage())
                .pageSize(searchOrgReq.getPageSize())
                .data(categories)
                .result(ResponseCodeEnum.SUCCESS.getCode())
                .build();
    }

 

碧海潮生吹玉箫 发布了22 篇原创文章 · 获赞 12 · 访问量 1万+ 私信 关注

标签:searchOrgReq,conditionQueryBuilder,order,termQuery,elasticsearch,sql,QueryBuilde
来源: https://blog.csdn.net/lmx1989219/article/details/104050264