aggregations support

This commit is contained in:
Ian Babrou 2014-02-27 16:33:51 +04:00
parent caac0b3f53
commit c7c95eccdc
2 changed files with 37 additions and 0 deletions

31
goes.go
View File

@ -348,3 +348,34 @@ func (r *Request) Url() string {
return u.String()
}
// Buckets returns list of buckets in aggregation
func (a Aggregation) Buckets() []Bucket {
result := []Bucket{}
if buckets, ok := a["buckets"]; ok {
for _, bucket := range buckets.([]interface {}) {
result = append(result, bucket.(map[string]interface{}))
}
}
return result
}
// Key returns key for aggregation bucket
func (b Bucket) Key() interface{} {
return b["key"]
}
// DocCount returns count of documents in this bucket
func (b Bucket) DocCount() uint64 {
return uint64(b["doc_count"].(float64))
}
// Aggregation returns aggregation by name from bucket
func (b Bucket) Aggregation(name string) Aggregation{
if agg, ok := b[name]; ok {
return agg.(map[string]interface{})
} else {
return Aggregation{}
}
}

View File

@ -82,8 +82,14 @@ type Response struct {
// Scroll id for iteration
ScrollId string `json:"_scroll_id"`
Aggregations map[string]Aggregation `json:"aggregations,omitempty"`
}
type Aggregation map[string]interface{}
type Bucket map[string]interface{}
// Represents a document to send to elasticsearch
type Document struct {
// XXX : interface as we can support nil values