我传递了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" ]
}
}
}
}]
答案 0 :(得分:2)
您需要使用termsFilter()
代替termFilter()