如何使用弹簧数据在弹性搜索多过滤器中查找

Posted

技术标签:

【中文标题】如何使用弹簧数据在弹性搜索多过滤器中查找【英文标题】:How find in elasticsearch multi filter using spring data 【发布时间】:2016-06-22 00:13:21 【问题描述】:

我通过网址卖家和猫喜欢:

sellers=1,2,3,4,5&cats=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() 我认为现在很好,谢谢。 【参考方案1】:

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

【讨论】:

以上是关于如何使用弹簧数据在弹性搜索多过滤器中查找的主要内容,如果未能解决你的问题,请参考以下文章

如何在弹性搜索的过滤器聚合中引用多个嵌套级别?

如何过滤掉弹性搜索中不存在的字段?

使用弹性过滤器的工作

如何在 Grafana 中查找内存使用差异

使用弹性搜索查询查找多个精确值

如何为mongodb数据库创建一个平面文件,以及如何在弹性搜索和kibana中使用该平面文件来查询数据?