Aggregate over bucket result

I want to aggregate or count the result in the bucket. for example:

    {
     ID: 1,
     customer_name: a,
     age: 21,
     other_field: x
    },
    {
     ID: 2,
     customer_name: a,
     age: 25,
     other_field: x
    }
    {
     ID: 3,
     customer_name: a,
     age: 32,
     other_field: x
    }
    {
     ID: 4,
     customer_name: b,
     age: 24,
     other_field: x
    }
    {
     ID: 5,
     customer_name: b,
     age: 33,
     other_field: x
    }
    {
     ID: 6,
     customer_name: b,
     age: 17,
     other_field: y
    },
    {
     ID: 7,
     customer_name: c,
     age: 34,
     other_field: x
    },
    {
     ID: 8,
     customer_name: c,
     age: 26,
     other_field: y
    }

my query is:

"query": {
  "bool": {
    "must": { "match": { "other_field": "x" }},
    }
  }

hit doc's ID are [1,2,3,4,5,7]

What I want to do is find out every customer's youngest hit docs

my aggregate query is

    "aggs": {
        "distinct_user": {
            "terms": {
                "field": "customer_name",
                "size": 100
            },
            "aggs": {
                "youngest": {
                    "min": {
                        "field": "AGE"
                    }
                }
            }
        }
    }

result:

bucket: [
 {
  "key": "a",
  "doc_count": 3,
  "youngest": {
  "value": 21
   }
 },
 {
  "key": "b",
  "doc_count": 2,
  "youngest": {
  "value": 24
   }
 },
 {
  "key": "c",
  "doc_count": 1,
  "youngest": {
  "value": 34
   }
 }
]

than using range aggregate to calculate age distribution

21~30: 2 31~40: 1

Is there any way to aggregate over bucket result? Or something work around?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.