mattbaird/elastigo — pull request #76 by snikch (2014-05-15 03:17:30+00:00)
Title: Aggregations

Support for [aggregations](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations.html).

Available types:
```go
Min(field string)
Max(field string)
Sum(field string)
Avg(field string)
Stats(field string)
ExtendedStats(field string)
ValueCount(field string)
Percentiles(field string)
Cardinality(field string, rehash bool, threshold int)
Global()
Missing(field string)
Terms(field string)
SignificantTerms(field string)
Histogram(field string, interval int)
DateHistogram(field string, interval string)
```

```go
agg := Aggregate("user").Term("user_id")
agg.Aggregates(
Aggregate("quarter").DateHistogram("created_at", "quarter").Aggregates(
Aggregate("total_spent").Sum("price"),
Aggregate("total_saved").Sum("discount"),
)
)
agg.Filter(
Range().Field("quantity").Gt(0),
)
Search("index").Type("item").Aggregates(agg)
```

```
GET /index/item/_search
{
"aggregations": {
"user": {
"terms": {
"field": "user_id"
},
"filter" : {
"range" : { "quantity" : { "gt" : 0 } }
},
"aggregations": {
"quarter": {
"date_histogram": {
"field": "created_at",
"range": "quarter"
},
"aggregations": {
"total_spent": {
"sum": {
"field": "price"
}
},
"total_saved": {
"sum": {
"field": "discount"
}
}
}
}
}
}
}
}
```