Unique count and bucket aggregation with top hits

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


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

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?

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.

Hi @warkolm ,

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

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