Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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())


  }


  }

 }

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?

share|improve this question

This question has an open bounty worth +100 reputation from user432024 ending in 19 hours.

The question is widely applicable to a large audience. A detailed canonical answer is required to address all the concerns.

1 Answer 1

At my previous project we use that way to desirialize complex objects from elastic search:

Elastic search allow you to get a plain json view (as a string) of each element of result set. Than we simply use Jacson json library with scala module and make pojo-like classes to deserialize data.

Elastic search java api is horrible multi-laer set of nested maps. forget about that.

val hits = qb.execute().actionGet().getHits().getHits().asScala
hits.map { hit =>
  (hit.getId, hit.getSourceAsString, hit.getVersion)
}

For agregations it should be availible too

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.