Last active
June 30, 2017 05:46
-
-
Save sksamuel/58929ce0e6a5684d5487f1a70a29b04e to your computer and use it in GitHub Desktop.
conversion of 1.4 elasticsearch aggs into elastic4s 6.0 map
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
Author
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
Added a test, which outputs:
Which you can now take this generated map, and feed it into the elastic4s
Aggregationscase class, which expects a Map.