ES提供的聚合接口相当灵活和丰富,值得系统的学习一下。学习方式想了下,应该跟SQL结合起来,SQL相对来说比较直观,ES官网对Aggregation描述的展开也是从SQL开始的,所以通过设计不同的SQL 聚合查询场景,分别看下ES中用Aggregation API和Java API是怎么实现的。
测试数据源于官网
POST /cars/transactions/_bulk
{ "index": {}}
{ "price" : 10000, "color" : "red", "make" : "honda", "sold" : "2014-10-28" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 30000, "color" : "green", "make" : "ford", "sold" : "2014-05-18" }
{ "index": {}}
{ "price" : 15000, "color" : "blue", "make" : "toyota", "sold" : "2014-07-02" }
{ "index": {}}
{ "price" : 12000, "color" : "green", "make" : "toyota", "sold" : "2014-08-19" }
{ "index": {}}
{ "price" : 20000, "color" : "red", "make" : "honda", "sold" : "2014-11-05" }
{ "index": {}}
{ "price" : 80000, "color" : "red", "make" : "bmw", "sold" : "2014-01-01" }
{ "index": {}}
{ "price" : 25000, "color" : "blue", "make" : "ford", "sold" : "2014-02-12" }
测试数据是汽车的销售信息,这次实现的SQL是按颜色groupby,查询每种颜色的销售数目。
SELECT COUNT(color) FROM table GROUP BY color
Aggregation API:
GET /cars/transactions/_search
{
"size" : 0,
"aggs" : {
"popular_colors" : {
"terms" : {
"field" : "color"
}
}
}
}
java API:
/**
* test sql: SELECT COUNT(color) FROM table GROUP BY color
*/
def aggTest1(): Unit = {
import scala.collection.JavaConverters._
val agg = AggregationBuilders
.terms("color")
.field("color")
.order(Terms.Order.count(false))
val searchRes = queryBuilder.setQuery(boolQueryBuilder).addAggregation(agg).execute().actionGet()
searchRes.getAggregations.get("color").asInstanceOf[Terms].getBuckets.asScala.foreach{ bucket =>
println(s"key: ${bucket.getKeyAsString}, value ${bucket.getDocCount}")
}
}