ElasticSearch - 如何从聚合中排除过滤器?

时间:2014-10-09 03:01:52

标签: elasticsearch facets

我使用3个过滤器过滤了查询:"query": "iphone", color:5, category:10, brand:25

如何获得每个品牌中有color:5category:10的产品数量?

在索尔我做到了:

fq={!tag=tag_facet_brand}facet_brand:"564"&facet.field={!ex=tag_facet_brand}facet_brand

如何从聚合上下文中排除1个过滤器? (我不能使用全局,因为我松散query:iphone,我不能使用post_filter - 因为性能)。

2 个答案:

答案 0 :(得分:5)

我们刚刚离开SOLR,我们面临同样的问题。

您可以尝试使用全局存储桶,然后为每个存储桶添加query filter

"filter" : {
            "query" : {
                "query_string" : {
                    "query" : "iPhone"
                }
            }
        }

你应该以:

之类的结尾
{
    "size": 10,
    "query": {
        "query_string": {
            "query": "iphone"
        }
    },
    "filter": {
        "bool": {
            "must": [
                {
                    "term": {
                        "brand": "564"
                    }
                },
                {
                    "term": {
                        "color": "5"
                    }
                },
                {
                    "term": {
                        "category": "10"
                    }
                }
            ]
        }
    },
    "aggs": {
        "all": {
            "global": {},
            "aggs": {
                "keyword": {
                    "filter": {
                        "bool": {
                            "must": [
                                {
                                    "query": {
                                        "query_string": {
                                            "query": "iphone"
                                        }
                                    }
                                },
                                {
                                    "term": {
                                        "color": "5"
                                    }
                                },
                                {
                                    "term": {
                                        "category": "10"
                                    }
                                }
                            ]
                        }
                    },
                    "aggs": {
                        "brand": {
                            "terms": {
                                "field": "brand"
                            }
                        }
                    }
                }
            }
        }
    }
}

答案 1 :(得分:2)

除了@Lionel Sengkouvanh的回答,这里有与java API等效的内容:

SearchRequestBuilder request = client
  .prepareSearch(INDEX_NAME)
  .setQuery(theQuery)
  ...
  .addAggregation(
     AggregationBuilders.global("all").subAggregation(      
       AggregationBuilders.filter("keyword").filter(filter).subAggregation(
         AggregationBuilders.terms("brand").field("brand")  
       )
     )
  );