如何使用 spring 数据在 elasticsearch 多过滤器中查找

How find in elasticsearch multi filter using spring data

我通过 URL 卖家和猫喜欢:

卖家=1,2,3,4,5&猫=6,7,8,9

我在方法中拆分它并尝试向弹性添加条件:

Optional.ofNullable(searchParams.getSellers().split(",")).ifPresent(v -> {
            filters.add(boolFilter().must(termFilter("seller", v)));
        });

    Optional.ofNullable(searchParams.getCats().split(",")).ifPresent(v -> {
        filters.add(boolFilter().must(termFilter("cat", v)));
    });

但结果并不是我所期望的那样。它只能通过最后一个卖家和最后一个类别找到。我想通过 URL 中的所有特定过滤器进行搜索。有什么想法我应该怎么做?

public FacetedPage<TransIndex> searchByIndexParams(TransIndexSearchParams searchParams, Pageable pageable) {
        final List<FilterBuilder> filters = Lists.newArrayList();
        final NativeSearchQueryBuilder searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery());

        Optional.ofNullable(searchParams.getBuyer()).ifPresent(v -> filters.add(boolFilter().must(termFilter("buyer", v))));
        Optional.ofNullable(searchParams.getCat()).ifPresent(v -> filters.add(boolFilter().must(termFilter("cat", v))));
        Optional.ofNullable(searchParams.getComment_type()).ifPresent(v -> filters.add(boolFilter().must(termFilter("comment_type", v))));
        Optional.ofNullable(searchParams.getItem()).ifPresent(v -> filters.add(boolFilter().must(termFilter("item", v))));
        Optional.ofNullable(searchParams.getSeller()).ifPresent(v -> filters.add(boolFilter().must(termFilter("seller", v))));
        Optional.ofNullable(searchParams.getTree_cat()).ifPresent(v -> filters.add(boolFilter().must(termFilter("tree_cat", v))));

        final BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();


        if(searchParams.getSellers() != null){
            Optional.ofNullable(searchParams.getSellers().split(",")).ifPresent(v -> {
                filters.add(boolFilter().must(termFilter("seller", v)));
            });
        }



        if(searchParams.getCats() != null){
            Optional.ofNullable(searchParams.getCats().split(",")).ifPresent(v -> {
                filters.add(boolFilter().must(termFilter("cat", v)));
            });
        }


        if (Optional.ofNullable(searchParams.getTitle()).isPresent()) {
            boolQueryBuilder.should(queryStringQuery(searchParams.getTitle()).analyzeWildcard(true).field("title"));
        }

        if (Optional.ofNullable(searchParams.getComment_text()).isPresent()) {
            boolQueryBuilder.should(queryStringQuery(searchParams.getComment_text()).analyzeWildcard(true).field("comment_text"));
        }

        if (Optional.ofNullable(searchParams.getPriceFrom()).isPresent()
                && Optional.ofNullable(searchParams.getPriceTo()).isPresent()) {
            filters.add(rangeFilter("price").from(searchParams.getPriceFrom()).to(searchParams.getPriceTo()));
        }

        if (Optional.ofNullable(searchParams.getTsFrom()).isPresent()
                && Optional.ofNullable(searchParams.getTsTo()).isPresent()) {
            filters.add(rangeFilter("ts").from(searchParams.getTsFrom()).to(searchParams.getTsTo()));
        }

        searchQuery.withPageable(pageable);
        searchQuery.withQuery(boolQueryBuilder);
        FilterBuilder[] filterArr = new FilterBuilder[filters.size()];
        filterArr = filters.toArray(filterArr);

        searchQuery.withFilter(andFilter(filterArr));

        final FacetedPage<TransIndex> response = transactionIndexRepository.search(searchQuery.build())



        return response;
    }

如果我尝试转储过滤器,它看起来像:

System.out.println(filters);

[{
  "bool" : {
    "must" : {
      "term" : {
        "seller" : [ "1", "2", "3", "4", "5" ]
      }
    }
  }
}, {
  "bool" : {
    "must" : {
      "term" : {
        "cat" : [ "6", "7", "8', "9" ]
      }
    }
  }
}]

您需要使用 termsFilter() 而不是 termFilter()