Skip to content

Instantly share code, notes, and snippets.

@sksamuel
Last active June 30, 2017 05:46
Show Gist options
  • Select an option

  • Save sksamuel/58929ce0e6a5684d5487f1a70a29b04e to your computer and use it in GitHub Desktop.

Select an option

Save sksamuel/58929ce0e6a5684d5487f1a70a29b04e to your computer and use it in GitHub Desktop.
conversion of 1.4 elasticsearch aggs into elastic4s 6.0 map
def aggregationAsMap(aggregations: Aggregations): Map[String, Map[String, AnyRef]] = {
import scala.collection.JavaConverters._
def convert(agg: Aggregation): Map[String, AnyRef] = {
agg match {
case terms: StringTerms =>
val buckets = terms.getBuckets.asScala.map { bucket =>
Map(
"key" -> bucket.getKey,
"doc_count" -> bucket.getDocCount
) ++ aggregationAsMap(bucket.getAggregations)
}
Map(
"name" -> agg.getName,
"doc_count_error_upper_bound" -> terms.getDocCountError.toString,
"sum_other_doc_count" -> terms.getSumOfOtherDocCounts.toString,
"buckets" -> buckets
)
case filter: Filter =>
filter.getAggregations
Map(
"name" -> agg.getName,
"doc_count" -> filter.getDocCount.toString
) ++ aggregationAsMap(filter.getAggregations)
case child: Children =>
Map(
"name" -> agg.getName,
"doc_count" -> child.getDocCount.toString
) ++ aggregationAsMap(child.getAggregations)
case card: Cardinality => Map("name" -> card.getName, "value" -> card.getValue.toString)
case sum: Sum => Map("name" -> sum.getName, "value" -> sum.getValue.toString)
case max: Max => Map("name" -> max.getName, "value" -> max.getValue.toString)
case min: Min => Map("name" -> min.getName, "value" -> min.getValue.toString)
case date: DateHistogram =>
val buckets = date.getBuckets.asScala.map { bucket =>
Map(
"key" -> bucket.getKeyAsDate.getMillis,
"key_as_string" -> bucket.getKey,
"doc_count" -> bucket.getDocCount
) ++ aggregationAsMap(bucket.getAggregations)
}
Map(
"name" -> agg.getName,
"buckets" -> buckets
)
}
}
aggregations.getAsMap.asScala.toMap.mapValues(convert)
}
@sksamuel

sksamuel commented Jun 22, 2017

Copy link
Copy Markdown
Author

Added a test, which outputs:

Map(agg1 -> Map(name -> agg1, doc_count_error_upper_bound -> 0, sum_other_doc_count -> 0, buckets -> ArrayBuffer(Map(key -> meth sidekick, doc_count -> 3), Map(key -> dea agent, doc_count -> 2), Map(key -> heavy, doc_count -> 2), Map(key -> meth kingpin, doc_count -> 2), Map(key -> lawyer, doc_count -> 1))))

Which you can now take this generated map, and feed it into the elastic4s Aggregations case class, which expects a Map.

@sksamuel

Copy link
Copy Markdown
Author

Version 3 now supports children and filter aggregations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment