# Best way to parse deeply nested aggregations using client API

**URL:** <https://discuss.elastic.co/t/best-way-to-parse-deeply-nested-aggregations-using-client-api/18979>\
**Category:** Elasticsearch\
**Created:** [July 30, 2014, 3:10pm UTC](https://discuss.elastic.co/t/best-way-to-parse-deeply-nested-aggregations-using-client-api/18979 "2014-07-30T15:10:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![birjupat](https://avatars.discourse-cdn.com/v4/letter/b/ba9def/32.png) [@birjupat](https://discuss.elastic.co/u/birjupat)\
**Post date:** [July 30, 2014, 3:10pm UTC](https://discuss.elastic.co/t/best-way-to-parse-deeply-nested-aggregations-using-client-api/18979/1 "2014-07-30T15:10:57Z")

</div>

I am using Java client API to get aggregations back. Following is the  
structure which I am dealing with.

aggregations  
top\_models  
buckets  
key : "BMW"  
doc\_count : 3  
top\_models  
buckets  
key : "X5"  
doc\_count : 2  
top\_hits  
source  
model : X5  
color : Red  
source  
model:X5  
color : White  
key : "X3"  
doc\_count : 1  
top\_hits  
source  
model : X3  
color : Red  
key : "Mercedes"  
doc\_count : 2  
top\_models  
buckets  
key : "Benz"  
doc\_count : 1  
top\_hits  
source  
model : Benz  
color : Red

```
              key : "ML"
              doc_count : 1
              top_hits
                   source
                      model : ML
                      color : Black

```

I am trying following (toy) code to retrieve all the results.

def getAggregations(aggres: Option[Aggregations]): Option[Iterable[Any]] = {

```
aggres map { agg =>

  val aggS = agg.asMap().asScala

  aggS map {

    case (name, termAgg: Terms) => getBuckets(Option(termAgg.getBuckets

```

()))

```
    case (name, topHits: TopHits) =>

      val tHits = Option(topHits.getHits())

      tHits map { th => getTopHits(th.asScala)
    }

    case (h, a: InternalAvg) => println(h + "=>" + a.getValue());

  }

}

```

}

def getBuckets(buckets: Option[java.util.Collection[Bucket]]) = {

```
buckets map { bks =>

  val bksS = bks.asScala

  bksS map { b =>

    println("Bucket Key =>" + b.getKey())

    println("Doc count =>" + b.getDocCount())

    getAggregations(Option(b.getAggregations())

  }

}

```

}

I need to populate final result to this class

case class FinalResponse(bucketName: String, count: Long, children: List[  
FinalResponse])

With nested relationship between Aggregations and Buckets it's becoming  
convoluted to retrieve all aggregation results. how do you approach this?

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/b47ef1bd-9a15-4d37-94f2-95b687dfc0d4%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b47ef1bd-9a15-4d37-94f2-95b687dfc0d4%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [July 6, 2017, 1:12am UTC](https://discuss.elastic.co/t/best-way-to-parse-deeply-nested-aggregations-using-client-api/18979/2 "2017-07-06T01:12:12Z")

</div>


