Skip to content

Commit

Permalink
#66 Add nestedAggregation to GroupByTime
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Aug 27, 2018
1 parent 8fc4c8c commit 07add73
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
27 changes: 21 additions & 6 deletions app/org/elastic4play/services/Aggregations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,31 @@ class GroupByCategory(aggregationName: String, categories: Map[String, QueryDef]
}

class GroupByTime(aggregationName: String, fields: Seq[String], interval: String, subAggs: Seq[Agg]) extends Agg(aggregationName) {
def apply(model: BaseModelDef): Seq[DateHistogramAggregation] = {
fields.map { f
dateHistogramAggregation(s"${aggregationName}_$f").field(f).interval(new DateHistogramInterval(interval)).subAggregations(subAggs.flatMap(_.apply(model)))
def apply(model: BaseModelDef): Seq[AggregationDefinition] = {
fields.map { fieldName
val dateHistoAgg = dateHistogramAggregation(s"${aggregationName}_$fieldName").field(fieldName).interval(new DateHistogramInterval(interval)).subAggregations(subAggs.flatMap(_.apply(model)))
fieldName
.split("\\.")
.toSeq
.init
.inits
.toSeq
.init
.foldLeft[AggregationDefinition](dateHistoAgg) { (agg, f)
nestedAggregation(aggregationName, f.mkString(".")).subaggs(agg)
}
}
}

def processResult(model: BaseModelDef, aggregations: RichAggregations): JsObject = {
val aggs = fields.map { f
val buckets = aggregations.getAs[Histogram](s"${aggregationName}_$f").getBuckets
f buckets.asScala.map { bucket
val aggs = fields.map { fieldName

val agg = fieldName.split("\\.").init.foldLeft(aggregations) { (a, _)
RichAggregations(a.getAs[Nested](aggregationName).getAggregations)
}

val buckets = agg.getAs[Histogram](s"${aggregationName}_$fieldName").getBuckets
fieldName buckets.asScala.map { bucket
val results = subAggs
.map(_.processResult(model, RichAggregations(bucket.getAggregations)))
.reduceOption(_ ++ _)
Expand Down
2 changes: 1 addition & 1 deletion app/org/elastic4play/services/auth/ADAuthSrv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ case class ADConnection(
}
.flatten
.recoverWith {
case ldapError =>
case ldapError
logger.debug("LDAP connect error", ldapError)
Failure(ldapError)
}
Expand Down
2 changes: 1 addition & 1 deletion app/org/elastic4play/services/auth/LdapAuthSrv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ case class LdapConnection(
}
.flatten
.recoverWith {
case ldapError =>
case ldapError
logger.debug("LDAP connect error", ldapError)
Failure(ldapError)
}
Expand Down

0 comments on commit 07add73

Please sign in to comment.