# Get the count in a tree like structure of items

**URL:** <https://discuss.elastic.co/t/get-the-count-in-a-tree-like-structure-of-items/139532>\
**Category:** Kibana\
**Created:** [July 11, 2018, 10:10am UTC](https://discuss.elastic.co/t/get-the-count-in-a-tree-like-structure-of-items/139532 "2018-07-11T10:10:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nikhilkvn](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikhilkvn/32/85872_2.png) [@nikhilkvn](https://discuss.elastic.co/u/nikhilkvn)\
**Post date:** [July 11, 2018, 10:10am UTC](https://discuss.elastic.co/t/get-the-count-in-a-tree-like-structure-of-items/139532/1 "2018-07-11T10:10:05Z")

</div>

Example data:  
{  
"userName": "spyderman",  
"phoneNumber": "123ertre67890",  
"devicegroup": "Globe/India/Delhi/Parkstreet/building0/401B",  
"address": "#complete address with postal codes",  
},  
{  
"userName": "batman",  
"phoneNumber": "12345fas890",  
"devicegroup": "Globe/India/delhi/Parkstreet/building0/402B",  
"address": "#complete address with postal codes",  
},  
{  
"userName": "superman",  
"phoneNumber": "123456asf0",  
"devicegroup": "Globe/India/delhi/gandhistreet/building0/401A",  
"address": "#complete address with postal codes",  
},

I wish to get the counts at each level:  
The documents will not be duplicated (generates one per user registration into the system)  
output desired:

no. of countries @global level  
no. of cities @ country level  
no. of buildings @ street level  
no. of users @ street level and # users @ building level

I was thinking to use path tokenizers but then not sure how i can get the counts in the way i desire. can someone guide in this regard.

---

<div class="post-metadata">

**Author:** ![tylersmalley](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tylersmalley/32/8833_2.png) [@tylersmalley](https://discuss.elastic.co/u/tylersmalley)\
**Post date:** [July 12, 2018, 3:56am UTC](https://discuss.elastic.co/t/get-the-count-in-a-tree-like-structure-of-items/139532/2 "2018-07-12T03:56:49Z")

</div>

It appears they are in a logical order - I would recommend splitting them and index the country/city/etc as individual properties on the document. You can do this in within your software, or if that is not possible with Logstash or [ingest](https://www.elastic.co/guide/en/elasticsearch/reference/master/ingest.html) with the [Grok processor](https://www.elastic.co/guide/en/elasticsearch/reference/master/grok-processor.html).

---

<div class="post-metadata">

**Author:** ![nikhilkvn](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikhilkvn/32/85872_2.png) [@nikhilkvn](https://discuss.elastic.co/u/nikhilkvn)\
**Post date:** [July 12, 2018, 6:52am UTC](https://discuss.elastic.co/t/get-the-count-in-a-tree-like-structure-of-items/139532/3 "2018-07-12T06:52:48Z")

</div>

Hi tyler,

Yes, splitting them is the easy way to go. but i was wondering if there is a way inside elastic search DSL, so i can use that filer query and show the counts in Kibana? No 3rd parties involved.

I am imagining this as:  
All i have is a tree like structure,  
so is there a way i get the **level-1 child counts from any given node.**?

---

<div class="post-metadata">

**Author:** ![tylersmalley](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tylersmalley/32/8833_2.png) [@tylersmalley](https://discuss.elastic.co/u/tylersmalley)\
**Post date:** [July 12, 2018, 2:44pm UTC](https://discuss.elastic.co/t/get-the-count-in-a-tree-like-structure-of-items/139532/4 "2018-07-12T14:44:00Z")

</div>

I am not aware of a way to dynamically do this at query time - but that's not to say it isn't possible. Even if it was, it would be extremely inefficient as it would need to be performed on the entire data set to query/aggregate.

---

<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:** [August 9, 2018, 2:44pm UTC](https://discuss.elastic.co/t/get-the-count-in-a-tree-like-structure-of-items/139532/5 "2018-08-09T14:44:03Z")

</div>

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