Skip to content

Commit

Permalink
#36 Fix nested field in GroupByField aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Nov 30, 2017
1 parent 9555807 commit e6ea543
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
6 changes: 3 additions & 3 deletions app/org/elastic4play/models/AttachmentAttributeFormat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.elastic4play.models
import play.api.Logger
import play.api.libs.json.{ JsValue, Json }

import com.sksamuel.elastic4s.ElasticDsl.{ keywordField, longField, nestedField, textField }
import com.sksamuel.elastic4s.ElasticDsl.{ keywordField, longField, nestedField }
import com.sksamuel.elastic4s.mappings.NestedFieldDefinition
import org.scalactic._

Expand Down Expand Up @@ -55,8 +55,8 @@ object AttachmentAttributeFormat extends AttributeFormat[Attachment]("attachment
keywordField("name"),
keywordField("hashes"),
longField("size"),
textField("contentType"),
textField("id"))
keywordField("contentType"),
keywordField("id"))

override def definition(dblists: DBLists, attribute: Attribute[Attachment]): Seq[AttributeDefinition] =
Seq(
Expand Down
50 changes: 31 additions & 19 deletions app/org/elastic4play/services/Aggregations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -196,29 +196,41 @@ class GroupByTime(aggregationName: String, fields: Seq[String], interval: String
}
}

class GroupByField(aggregationName: String, field: String, size: Option[Int], sortBy: Seq[String], subAggs: Seq[Agg]) extends Agg(aggregationName) {
def apply(model: BaseModelDef): Seq[TermsAggregationDefinition] = {
Seq(termsAggregation(s"${aggregationName}_$field").field(field).subAggregations(subAggs.flatMap(_.apply(model))))
.map { agg size.fold(agg)(s agg.size(s)) }
.map {
case agg if sortBy.isEmpty agg
case agg
val sortDefinition = sortBy
.flatMap {
case "_count" | "+_count" Seq(Order.count(true))
case "-_count" Seq(Order.count(false))
case "_term" | "+_term" Seq(Order.term(true))
case "-_term" Seq(Order.term(false))
case f if f.startsWith("+") Seq(Order.aggregation(f.drop(1), true))
case f if f.startsWith("-") Seq(Order.aggregation(f.drop(1), false))
case f if f.length() > 0 Seq(Order.aggregation(f, true))
}
agg.order(Order.compound(sortDefinition.asJava))
class GroupByField(aggregationName: String, fieldName: String, size: Option[Int], sortBy: Seq[String], subAggs: Seq[Agg]) extends Agg(aggregationName) {
private def setSize(agg: TermsAggregationDefinition): TermsAggregationDefinition = {
size.fold(agg)(s agg.size(s))
}

private def setOrder(agg: TermsAggregationDefinition): TermsAggregationDefinition = {
val sortDefinition = sortBy
.flatMap {
case "_count" | "+_count" Seq(Order.count(true))
case "-_count" Seq(Order.count(false))
case "_term" | "+_term" Seq(Order.term(true))
case "-_term" Seq(Order.term(false))
case f if f.startsWith("+") Seq(Order.aggregation(f.drop(1), true))
case f if f.startsWith("-") Seq(Order.aggregation(f.drop(1), false))
case f if f.length() > 0 Seq(Order.aggregation(f, true))
case _ Nil
}
if (sortDefinition.nonEmpty)
agg.order(Order.compound(sortDefinition.asJava))
else
agg
}

def apply(model: BaseModelDef): Seq[AggregationDefinition] = {
val agg = setSize(setOrder(termsAggregation(s"${aggregationName}_$fieldName").field(fieldName).subAggregations(subAggs.flatMap(_.apply(model)))))
Seq(fieldName.split("\\.").init.foldLeft[AggregationDefinition](agg) { (agg, f)
nestedAggregation(aggregationName, f).subaggs(agg)
})
}

def processResult(model: BaseModelDef, aggregations: RichAggregations): JsObject = {
val buckets = aggregations.getAs[Terms](s"${aggregationName}_$field").getBuckets
val buckets = fieldName.split("\\.").init.foldLeft(aggregations) { (a, _)
RichAggregations(a.getAs[Nested](aggregationName).getAggregations)
}
.getAs[Terms](s"${aggregationName}_$fieldName").getBuckets
JsObject {
buckets.asScala.map { bucket
val results = subAggs
Expand Down
4 changes: 2 additions & 2 deletions app/org/elastic4play/services/auth/ADAuthSrv.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ case class ADConnection(
}
}

def authenticate(username: String, password: String)(implicit request: RequestHeader): Try[Unit] = {
def authenticate(username: String, password: String): Try[Unit] = {
connect(domainName + "\\" + username, password)(_ Success(()))
}

def changePassword(username: String, oldPassword: String, newPassword: String)(implicit authContext: AuthContext): Try[Unit] = {
def changePassword(username: String, oldPassword: String, newPassword: String): Try[Unit] = {
val unicodeOldPassword = ("\"" + oldPassword + "\"").getBytes("UTF-16LE")
val unicodeNewPassword = ("\"" + newPassword + "\"").getBytes("UTF-16LE")
connect(domainName + "\\" + username, oldPassword) { ctx
Expand Down

0 comments on commit e6ea543

Please sign in to comment.