简体   繁体   中英

Elasticsearch aggregations

I'm trying to build category drilldown with elasticsearch.

I've problem with showing category counts.

If I run this query (Red, casual pants) , category aggregations will show up only casual's count. Can i retrieve other categories count in a single query (such as shoes,jeans,dresses)

Example Filters:

Colors:

-Red Color (selected as a bool query term)

Categories:

Shoes
Pants
    -Jeans
    - Casual (selected as a bool query term)
Dresses

If I understand well, you seem to need to calculate your aggregations on all categories, and then filter it to show only one categorie at a time.

This can be achieved using post_filter (see documentation ). It's a filter which is applied after the aggregations have been computed.

In your case, instead of querying like this :

{
  "query": {
    "bool": {
      "must": [
        {"term": {"color": "red"} },
        {"term": {"category": "casual"}}
        ....
      ]
    }
  }, 
  "aggs": {
    ...
  }
}

You use this :

{
  "query": {
    "bool": {
      "must": [
        {"term": {"color": "red"} }
        ...
      ]
    }
  }, 
  "aggs": {
    ...
  },
  "post_filter": {
    {"term": {"category": "casual"}}
  }
}

And the term filter on category won't be taken into account when computing the aggregations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM