# Unique count and bucket aggregation with top hits

**URL:** https://discuss.elastic.co/t/unique-count-and-bucket-aggregation-with-top-hits/254698
**Category:** Kibana
**Created:** [November 9, 2020, 1:00am UTC](https://discuss.elastic.co/t/unique-count-and-bucket-aggregation-with-top-hits/254698 "2020-11-09T01:00:02Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![smruthip](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@smruthip](https://discuss.elastic.co/u/smruthip)
#### Post date: [November 9, 2020, 1:00am UTC](https://discuss.elastic.co/t/unique-count-and-bucket-aggregation-with-top-hits/254698/1 "2020-11-09T01:00:03Z")

</div>

Hi all,

I want to display unique count of container\_id's for each state. I want to do a bucket aggregation to get the latest state of the container and then do a unique count of the containers for each state

to get the latest state of the container I have this

```auto

GET /my_index/_search
{
   "aggs":{
      "top_tags":{
         "terms":{
            "field":"container_id.keyword"
         },
         "aggs":{
            "top_sales_hits":{
               "top_hits":{
                  "sort":[
                     {
                        "actionTime":{
                           "order":"desc"
                        }
                     }
                  ],
                  "_source":{
                     "includes":[
                        "container_id",
                        "state",
                        "actionTime"
                     ]
                  },
                  "size":1
               }
            }
         }
      }
   }
}

```

I am new to Vega , if there is any other way to display this also I am open to suggestions.

Thank you

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [November 9, 2020, 3:51am UTC](https://discuss.elastic.co/t/unique-count-and-bucket-aggregation-with-top-hits/254698/2 "2020-11-09T03:51:17Z")

</div>

> [@smruthip](#):
>
> I want to do a bucket aggregation to get the latest state of the container and then do a unique count of the containers for each state

If you get the latest state of a container, then get the count, aren't you just getting a count of a single state? Or do you want a count for all states, but only the latest state for a given container?

---

<div class="post-metadata">

### Author: ![smruthip](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@smruthip](https://discuss.elastic.co/u/smruthip)
#### Post date: [November 9, 2020, 4:41am UTC](https://discuss.elastic.co/t/unique-count-and-bucket-aggregation-with-top-hits/254698/3 "2020-11-09T04:41:06Z")

</div>

Hi Mark,

I want the count for all states, but only the latest state for a given container.

example

container\_id, container\_state, timestamp  
1, loaded, 2pm  
**1, received, 4pm**

here the count of container\_id 1 should be only for received state because container is no longer in loaded state

**2, received, 5pm**  
**3, loaded , 2pm**  
4, received, 2pm  
**4, loaded , 5pm**  
**5, loaded, 3pm**

container\_state count  
Loaded 3  
Received 2

so total there are 5 containers out of which 3 are currently in loaded state and 2 are in received state.

---

<div class="post-metadata">

### Author: ![smruthip](https://avatars.discourse-cdn.com/v4/letter/s/c5a1d2/32.png) [@smruthip](https://discuss.elastic.co/u/smruthip)
#### Post date: [November 9, 2020, 5:28pm UTC](https://discuss.elastic.co/t/unique-count-and-bucket-aggregation-with-top-hits/254698/4 "2020-11-09T17:28:48Z")

</div>

Hi @warkolm ,

Wanted to follow up and see how I can achieve this? Appreciate your help!

---

<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: [December 7, 2020, 5:28pm UTC](https://discuss.elastic.co/t/unique-count-and-bucket-aggregation-with-top-hits/254698/5 "2020-12-07T17:28:54Z")

</div>

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