esquery/aggs_bucket.go
Hardy 49a92fc25e
Features: SearchAfter、Term Aggregation Order、Aggregation Include Filter Values (#14)
* Add support for multi_match queries

* Add support for highlights

* Add support for nested aggregations and filtered aggregations

* Update README

* Fix formatting

* fix

* feat: add support for search after

* fix:set search after []string to []interface

* fix:add .gitignore

* feat:Support for term aggs order

* Feat: Support include filter for termAggs

* Update aggregations_test.go

Fix conflict.

* Update go.mod

Co-authored-by: Caleb Champlin <caleb.champlin@gmail.com>
Co-authored-by: Hardy <caoxiaozhen@secnium.com>
Co-authored-by: Oran Moshai <12291998+oranmoshai@users.noreply.github.com>
2021-03-15 09:43:59 +02:00

113 lines
2.8 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
order map[string]string
include []string
}
// 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
}
// Order sets the sort for terms agg
func (agg *TermsAggregation) Order(order map[string]string) *TermsAggregation {
agg.order = order
return agg
}
// Include filter the values for buckets
func (agg *TermsAggregation) Include(include ...string) *TermsAggregation {
agg.include = include
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
}
if agg.order != nil {
innerMap["order"] = agg.order
}
if agg.include != nil {
if len(agg.include) <= 1 {
innerMap["include"] = agg.include[0]
} else {
innerMap["include"] = agg.include
}
}
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
}