# How to print all the hits from a bucket array of results

**URL:** https://discuss.elastic.co/t/how-to-print-all-the-hits-from-a-bucket-array-of-results/220044
**Category:** Elasticsearch
**Tags:** elastic-stack-alerting
**Created:** [February 19, 2020, 6:31pm UTC](https://discuss.elastic.co/t/how-to-print-all-the-hits-from-a-bucket-array-of-results/220044 "2020-02-19T18:31:55Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Chop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chop/32/61665_2.png) [@Chop](https://discuss.elastic.co/u/Chop)
#### Post date: [February 19, 2020, 6:31pm UTC](https://discuss.elastic.co/t/how-to-print-all-the-hits-from-a-bucket-array-of-results/220044/1 "2020-02-19T18:31:55Z")

</div>

I am using Watcher/Alerts to send some emails but it dont seem to find out how to get all keys from an array with out all the array:

> {{ctx.payload.aggregations.host.buckets.\*.key}}

\<-- this is what my logic said it should work but it didnt 😃

"host" : {  
"doc\_count\_error\_upper\_bound" : 0,  
"sum\_other\_doc\_count" : 0,  
"buckets" : [  
{  
"doc\_count" : 3,  
"key" : "[someservername-02.com](http://someservername-02.com)"  
},  
{  
"doc\_count" : 3,  
"key" : "[someservername-03.com](http://someservername-03.com)"  
}  
]  
},

I have to specify each but i am not sure how to find out how many to iterate but i ithink iterations are not possible here so is there a way just to print the keys? i want to avoid the doc\_count?

i know this works

> {{ctx.payload.aggregations.host.buckets}}

but can i just filter a specific field?

I tried adding a bunch of

> {{ctx.payload.aggregations.host.buckets.1.key}} {{ctx.payload.aggregations.host.buckets.2.key}} {{ctx.payload.aggregations.host.buckets.3.key}}

but ill get an out of index array because there isnt 3 every time.

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [February 20, 2020, 7:50am UTC](https://discuss.elastic.co/t/how-to-print-all-the-hits-from-a-bucket-array-of-results/220044/2 "2020-02-20T07:50:13Z")

</div>

Hey,

try looping like this

```auto
{{#ctx.payload.aggregations.host.buckets}}
{{key}}
{{/ctx.payload.aggregations.host.buckets}}

```

---

<div class="post-metadata">

### Author: ![Chop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chop/32/61665_2.png) [@Chop](https://discuss.elastic.co/u/Chop)
#### Post date: [February 20, 2020, 2:52pm UTC](https://discuss.elastic.co/t/how-to-print-all-the-hits-from-a-bucket-array-of-results/220044/3 "2020-02-20T14:52:04Z")

</div>

Thanks, i think i found this in another post from you and i wonder if its possible to go a bit higher on the object for example:

```
{{#ctx.payload.aggregations}}
{{host.buckets.key}}
{{/ctx.payload.aggregations}}

```

This didnt work for me, the idea is to collect diffent subobjects while i am looping.

or what about

```
{{#ctx.payload.hits.hits}}
{{host.buckets.key}}
{{/ctx.payload.hits.hits}}

```

i dont seem to find a way to loop like that.

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [February 20, 2020, 3:15pm UTC](https://discuss.elastic.co/t/how-to-print-all-the-hits-from-a-bucket-array-of-results/220044/4 "2020-02-20T15:15:39Z")

</div>

that looping only works on lists. `ctx.payload.aggregations` is not a list.

`ctx.payload.hits.hits` is a list and you can then access the elements within like `{{_source.foo}}` or `{{_index}}`. You can only access fields within that single list element.

---

<div class="post-metadata">

### Author: ![Chop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/chop/32/61665_2.png) [@Chop](https://discuss.elastic.co/u/Chop)
#### Post date: [February 20, 2020, 8:32pm UTC](https://discuss.elastic.co/t/how-to-print-all-the-hits-from-a-bucket-array-of-results/220044/5 "2020-02-20T20:32:55Z")

</div>

Thanks 🙂

---

<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: [March 19, 2020, 8:32pm UTC](https://discuss.elastic.co/t/how-to-print-all-the-hits-from-a-bucket-array-of-results/220044/6 "2020-03-19T20:32:55Z")

</div>

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