es java 实现should must filter组合查询

一,三种属于一个层级,一般must filter放在一个boolQuery中,should放在另外一个新的boolQuery中,然后把should boolQuery作为前一个boolQuery条件。且关注一下filter用法,这个可以用于多参数查询,类似in,即参数是一个集合。

二,其实filter should也可以放在同级,如图2

如下图一:

BoolQueryBuilder multiFiledQuery = QueryBuilders.boolQuery();
            multiFiledQuery.should(QueryBuilders.termsQuery(convertToUnderScore(ResPurchaseWideInfo::getProductCode), req.getSearchKey()));
            multiFiledQuery.should(new WildcardQueryBuilder(convertToUnderScore(ResPurchaseWideInfo::getProductCode), "*".concat(req.getSearchKey()).concat("*")));
            multiFiledQuery.should(new WildcardQueryBuilder(convertToUnderScore(ResPurchaseWideInfo::getProductName).concat(".keyword"), "*".concat(req.getSearchKey().concat("*"))));
            multiFiledQuery.should(QueryBuilders.termsQuery(convertToUnderScore(ResPurchaseWideInfo::getProductName).concat(".keyword"), req.getSearchKey()));
            multiFiledQuery.should(QueryBuilders.termsQuery(convertToUnderScore(ResPurchaseWideInfo::getBarCode), req.getSearchKey()));
            multiFiledQuery.minimumShouldMatch(1);
            boolQuery.must(multiFiledQuery);
 @Override
    @SneakyThrows
    public List getDetailInfoByGroupAndBigMidCategoryCode(ReqProductInfoVO req) {
        SearchRequest searchRequest = new SearchRequest(EsConfigEnum.PURCHASE_WIDE_INFO_INDEX.getAlias());
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        sourceBuilder.query(boolQuery);

        sourceBuilder.fetchSource(RES_FUZZY_PRODUCT_INFO_VO_FIELDS, null);
        // 分页
        sourceBuilder.from(req.getPage() * req.getSize() - req.getSize());
        sourceBuilder.size(req.getSize());
        if (Objects.nonNull(req.getIsSortByProductCode()) && req.getIsSortByProductCode()) {
            sourceBuilder.sort(convertToUnderScore(ResPurchaseWideInfo::getProductCode));
        }
        searchRequest.source(sourceBuilder);

        // 条码精确查询,名称,编码模糊查询*x*;且名称查询必须加.keyword,否则查询不到数据
        if (org.apache.commons.lang3.StringUtils.isNotBlank(req.getSearchKey())) {
            BoolQueryBuilder multiFiledQuery = QueryBuilders.boolQuery();
            multiFiledQuery.should(QueryBuilders.termsQuery(convertToUnderScore(ResPurchaseWideInfo::getProductCode), req.getSearchKey()));
            multiFiledQuery.should(new WildcardQueryBuilder(convertToUnderScore(ResPurchaseWideInfo::getProductCode), "*".concat(req.getSearchKey()).concat("*")));
            multiFiledQuery.should(new WildcardQueryBuilder(convertToUnderScore(ResPurchaseWideInfo::getProductName).concat(".keyword"), "*".concat(req.getSearchKey().concat("*"))));
            multiFiledQuery.should(QueryBuilders.termsQuery(convertToUnderScore(ResPurchaseWideInfo::getProductName).concat(".keyword"), req.getSearchKey()));
            multiFiledQuery.should(QueryBuilders.termsQuery(convertToUnderScore(ResPurchaseWideInfo::getBarCode), req.getSearchKey()));
            multiFiledQuery.minimumShouldMatch(1);
            boolQuery.must(multiFiledQuery);
        }

        //采购组
        if (CollectionUtils.isNotEmpty(req.getPurchaseGroupCodes())) {
            List purchaseGroups = req.getPurchaseGroupCodes().stream().map(org.apache.commons.lang3.StringUtils::lowerCase).distinct().collect(Collectors.toList());
            purchaseGroups.addAll(req.getPurchaseGroupCodes());
            purchaseGroups = purchaseGroups.stream().distinct().collect(Collectors.toList());
            boolQuery.filter(QueryBuilders.termsQuery(convertToUnderScore(ResPurchaseWideInfo::getPurchaseGroupCode), purchaseGroups));
        }

        // 大类编码(精确)
        if (StringUtils.isNotBlank(req.getBigCategoryCode())) {
            boolQuery.filter(QueryBuilders.termQuery(convertToUnderScore(ResPurchaseWideInfo::getBigCategoryCode), req.getBigCategoryCode()));
        }
        // 中类编码(精确)
        if (StringUtils.isNotBlank(req.getMidCategoryCode())) {
            boolQuery.must(QueryBuilders.termQuery(convertToUnderScore(ResPurchaseWideInfo::getMidCategoryCode), req.getMidCategoryCode()));
        }

        SearchResponse response = null;
        response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        if (Objects.isNull(response)) {
            return Collections.emptyList();
        }
        List> list = toRes(response.getHits().getHits());
        return JSON.parseArray(JSON.toJSONString(list), ResProductInfoVOPage.class);
    }

图二

  SearchRequest searchRequest = new SearchRequest(EsConfigEnum.PURCHASE_WIDE_INFO_INDEX.getAlias());
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.fetchSource(SHOP_PRODUCT_PURCHASE_FIELDS, null);
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.filter(QueryBuilders.termQuery(convertToUnderScore(ResPurchaseWideInfo::getShopCode), reqVO.getShopCode()))
                .should(QueryBuilders.termQuery(convertToUnderScore(ResPurchaseWideInfo::getProductCode), reqVO.getProductCode()))
                .should(new WildcardQueryBuilder(convertToUnderScore(ResPurchaseWideInfo::getProductCode), "*".concat(reqVO.getProductCode()).concat("*")))
                .should(QueryBuilders.termQuery(convertToUnderScore(ResPurchaseWideInfo::getBarCode), reqVO.getProductCode()))
                .should(new WildcardQueryBuilder(convertToUnderScore(ResPurchaseWideInfo::getBarCode), "*".concat(reqVO.getProductCode()).concat("*")))
                .minimumShouldMatch(1);
        if (CollectionUtils.isNotEmpty(reqVO.getPurchaseGroupCodes())) {
            boolQueryBuilder.filter(QueryBuilders.termsQuery(convertToUnderScore(ResPurchaseWideInfo::getPurchaseGroupCode), reqVO.getPurchaseGroupCodes()));
        }

 

你可能感兴趣的:(elasticsearch,es)