# Getting the keys array from max\_bucket output

**URL:** <https://discuss.elastic.co/t/getting-the-keys-array-from-max-bucket-output/162476>\
**Category:** Elasticsearch\
**Created:** [December 31, 2018, 6:52am UTC](https://discuss.elastic.co/t/getting-the-keys-array-from-max-bucket-output/162476 "2018-12-31T06:52:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![genesis\_en](https://avatars.discourse-cdn.com/v4/letter/g/58f4c7/32.png) [@genesis\_en](https://discuss.elastic.co/u/genesis_en)\
**Post date:** [December 31, 2018, 6:52am UTC](https://discuss.elastic.co/t/getting-the-keys-array-from-max-bucket-output/162476/1 "2018-12-31T06:52:34Z")

</div>

Hi,

I'm have the following query to get the max\_bucket and it's time (as keys). Is there a way to have additional bucket\_script that uses the keys from max\_bucket.

In the example script below, I have the keys from max\_card and I would like to use the keys in info\_2 bucket script. Does Elasticsearch have a method to get the keys?

"aggs": {  
"day": {  
"date\_histogram": {  
"field": "st",  
"interval": "day",  
"format": "yyyy-MM-dd HH:mm"  
},  
"aggs": {  
"play\_bh": {  
"terms": {  
"field": "player",  
"size": 100  
},  
"aggs": {  
"maxim\_hour": {  
"date\_histogram": {  
"field": "st",  
"interval": "hour",  
},  
"aggs": {  
"n1": { "sum": { "field": "countA" }},  
"d1": { "sum": { "field": "div\_2b" }},  
"score\_card": {  
"bucket\_script": {  
"buckets\_path": {  
"n1":"kpi\_n1",  
"d1":"kpi\_d1"  
},  
"script": {  
"lang": "painless",  
"source": "def card; if (params.d1 == 0) {card = 0.0} else { card = (params.n1/params.d1)} return card"  
}  
}  
}  
}  
},

```
        "max_card": {
          "max_bucket": {
            "buckets_path": "maxim_hour>score_card"
          }
        },
        
        "info_2": {
          "bucket_script": {
            "buckets_path": {
              "hr": "max_card"
            },
            "script": {
              "lang": "painless",
              "source": "???"
            }
          }
        }
      }
    }
  }
}

```

}

Thank you,

Cheers,  
Max

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 28, 2019, 7:06am UTC](https://discuss.elastic.co/t/getting-the-keys-array-from-max-bucket-output/162476/2 "2019-01-28T07:06:56Z")

</div>

Closed on January 28, 2019, 7:06am UTC

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