使用elastic4s client

时间:2016-09-05 14:03:41

标签: scala elasticsearch aggregate-functions dsl elastic4s

是否可以使用elastic4s DSL动态执行多级聚合查询。

使用http客户端直接

multiLevelAggregation

 Input: Fields[0..N]
 Output: Data grouped by field tuple

 Steps:
 1.  Build multilevel elasticsearch aggregation query (JSON)
 2.  Execute query on elasticsearch server
 3.  Flatten result and return 

但是如何使用elastic4s或Java客户端执行此操作。

1 个答案:

答案 0 :(得分:1)

在仔细理解了我的问题之后,我设法找到了解决方案,最初我认为这将是elastic4s的限制但不是这样,它很容易通过elastic4s客户端动态构建多字段聚合查询,这是我的解决方案

//For building aggregation query
def buildAgg(groups: Seq[String])(leafAggBuilder: () => AbstractAggregationDefinition): AbstractAggregationDefinition = {
  groups match {
    case x :: xs => aggregation.terms("termAgg").field(x).aggregations(buildAgg(xs)(leafAggBuilder))
    case Nil => leafAggBuilder()
  }
}

//An example leaf aggregation builder
def buildLeafAgg(aggFuncInfo: Pair[String, String])(): AbstractAggregationDefinition = {
  aggFuncInfo._1 match {
    case "avg" => aggregation.avg("aggFunc").field(aggFuncInfo._2)
    case "sum" => aggregation.sum("aggFunc").field(aggFuncInfo._2)
    case "cardinality" => aggregation.cardinality("aggFunc").field(aggFuncInfo._2)
    case _ => aggregation.count("aggFunc").field(aggFuncInfo._2)
  }
}

//For parsing aggregation
def parseAgg[T](groups: Seq[String], agg: Aggregation, allGroups: Seq[String])(leafAggParser: (Seq[String], Aggregation) => Seq[T]): Seq[T] = {
  groups match {
    case x :: xs => {
      val groupAggs = agg.asInstanceOf[StringTerms].getBuckets.asScala.toList
      (for {
        groupAgg <- groupAggs
        aa = groupAgg.getAggregations.asList.asScala.head
        gkey = groupAgg.getKeyAsString
        gacc = allGroups :+ gkey
      } yield parseAgg(xs, aa, gacc)(leafAggParser)).flatten
    }

    case Nil => {
      leafAggParser(allGroups, agg)
    }
  }
}

//An example leaf aggregation parser
def parseSimpleLeafAgg(allGroups: Seq[String], agg: Aggregation): Seq[GroupStats] = {
  val value = agg.asInstanceOf[InternalNumericMetricsAggregation.SingleValue].value()
  val groupId = allGroups.mkString(".")
  Seq(GroupStats(groupId, value))
}

//Usage: Build Query and Parse result
def groupStats(groupFields: Seq[String]): Seq[GroupStats] = {
  val resp = client.execute {
    def leafPlainAggBuilder = buildLeafAgg(("count", "V1")) _
    search(esIndex).size(0).aggregations(buildAgg(groupFields)(leafPlainAggBuilder))
  }.await
  //get the root aggregation
  val agg = resp.aggregations.asList().asScala.head
  def leafAggParser = parseSimpleLeafAgg _
  val res = parseAgg(groupFields, agg, Seq())(leafAggParser)
  res
}