esquery/aggs_bucket.go
Ido Perlmuter 8e5dea816b
Add Search() function, two new aggregations (#7)
This commit implements a Search() function, which allow for running
search requests with both a query and aggregations. This function is
meant to more accurately implement the structure of search requests
accepted by ElasticSearch's Search API.

The Query() and Aggregate() functions are still included by the library,
but now simply call Search() internally, making them simple shortcuts.

Two new aggregations are also added: "terms" and "top_hits". These are
implemented a bit differently than previously implemented ones. The
structs and methods for ElasticSearch queries and aggregations will
eventually be auto-generated from a specification file, and will look
more like the new implementations of these new aggregations.
2020-04-06 12:03:42 +03:00

87 lines
2.3 KiB
Go

package esquery
//----------------------------------------------------------------------------//
// TermsAggregation represents an aggregation of type "terms", as described in
// https://www.elastic.co/guide/en/elasticsearch/reference/current/
// search-aggregations-bucket-terms-aggregation.html
type TermsAggregation struct {
name string
field string
size *uint64
shardSize *float64
showTermDoc *bool
aggs []Aggregation
}
// TermsAgg creates a new aggregation of type "terms". The method name includes
// the "Agg" suffix to prevent conflict with the "terms" query.
func TermsAgg(name, field string) *TermsAggregation {
return &TermsAggregation{
name: name,
field: field,
}
}
// Name returns the name of the aggregation.
func (agg *TermsAggregation) Name() string {
return agg.name
}
// Size sets the number of term buckets to return.
func (agg *TermsAggregation) Size(size uint64) *TermsAggregation {
agg.size = &size
return agg
}
// ShardSize sets how many terms to request from each shard.
func (agg *TermsAggregation) ShardSize(size float64) *TermsAggregation {
agg.shardSize = &size
return agg
}
// ShowTermDocCountError sets whether to show an error value for each term
// returned by the aggregation which represents the worst case error in the
// document count.
func (agg *TermsAggregation) ShowTermDocCountError(b bool) *TermsAggregation {
agg.showTermDoc = &b
return agg
}
// Aggs sets sub-aggregations for the aggregation.
func (agg *TermsAggregation) Aggs(aggs ...Aggregation) *TermsAggregation {
agg.aggs = aggs
return agg
}
// Map returns a map representation of the aggregation, thus implementing the
// Mappable interface.
func (agg *TermsAggregation) Map() map[string]interface{} {
innerMap := map[string]interface{}{
"field": agg.field,
}
if agg.size != nil {
innerMap["size"] = *agg.size
}
if agg.shardSize != nil {
innerMap["shard_size"] = *agg.shardSize
}
if agg.showTermDoc != nil {
innerMap["show_term_doc_count_error"] = *agg.showTermDoc
}
outerMap := map[string]interface{}{
"terms": innerMap,
}
if len(agg.aggs) > 0 {
subAggs := make(map[string]map[string]interface{})
for _, sub := range agg.aggs {
subAggs[sub.Name()] = sub.Map()
}
outerMap["aggs"] = subAggs
}
return outerMap
}