简体   繁体   中英

ElasticSearch - Aggregations: filtering and sorting buckets

Using ElasticSearch 5.2

As an example I'll be using a database representing all people in the world.

Example request: Get the average salary of a person, grouped by country, for everyone older than 30 years old. (Response should contain the top 10 countries with their average salary)

Steps i took to build my aggregation query:

  1. Filter the raw dataset ( filter: age > 30 )
  2. Aggregate on ' country '
  3. Use metric Avg on field ' salary '

The problem that I'm facing is that I can either apply a filter before aggregating, or sort the buckets, but it seems like ES does not allow me to do both?
In other words: I cannot apply my " order " query on " myInnerAggregation " because it's preceded by " myFilterAggregation "

GET myIndex/_search
{
  "size":0,
  "aggs": {
  "myAggregation": {
  "terms": {
    "size":10,
    "field": "country",
    "order": [
      {
        "myInnerAggregation": "desc"
          // I cannot specify "myInnerAggregation" here, only 
          // "myFilterAggregation" (because it's out of scope I guess)
      }
    ]
  },
  "aggs": {
    "myFilterAggregation": {
      "filter": { /* age > 30 (filter syntax is not the problem)*/},
      "aggs": {
        "myInnerAggergation": {
          "avg": {
            "field": "salary"
          }
        }
      }
    }
  }
}

} }

I've seen that ES 6.1+ has support for " bucket_sort ", which would probably solve this problem, but I cannot believe a simple aggregation like this cannot be handled by ES 5.2?

Try moving the filter to the query part of the request. You don't have to declare your filter as an aggregation, aggregations respect filters defined in the query part of your request. Hopefully this way you can get around your issue.

This should do the trick:

"order": [
      {
        "MyFilterAggregation>MyInnerAggregation": "desc"
      }
    ]

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