如何在弹性搜索java中基于多个字段值进行过滤

时间:2017-04-11 05:38:46

标签: java elasticsearch playframework

我尝试在弹性搜索中为多个字段设置后置过滤器。但它不起作用。请告诉我如何设置?

SearchResponse searchResponse = client.prepareSearch("idx_articles").setTypes("articles")
                .setPostFilter(QueryBuilders.matchQuery("pubish", true))
                .setPostFilter(QueryBuilders.matchQuery("status", "active"))
                .setQuery(QueryBuilders.wildcardQuery("title", "pub"+"*"))
                .addSort("articleVersion", SortOrder.DESC) // Filter
                .execute().actionGet();

它只考虑.setPostFilter(QueryBuilders.matchQuery(“status”,“active”))

它没有考虑过滤器.setPostFilter(QueryBuilders.matchQuery(“pubish”,true))

请告诉我如何设置多个场滤波器?

1 个答案:

答案 0 :(得分:0)

你可以使用Bool过滤器Inside Post过滤器,如下所示

setPostFilter(FilterBuilders.boolFilter()
                .must(FilterBuilders.termFilter("pubish", true))
                .must(FilterBuilders.termFilter("status", "active")))

供参考

https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.3/bool-filter.html