From cc685d325e71307c5a8135c43410ee0086359330 Mon Sep 17 00:00:00 2001 From: Hardy Date: Fri, 12 Mar 2021 19:37:19 +0800 Subject: [PATCH] feat:Support for term aggs order --- aggregations_test.go | 40 ++++++++++++++++++++++++++++++++++------ aggs_bucket.go | 10 ++++++++++ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/aggregations_test.go b/aggregations_test.go index 39eeaa3..080ff30 100644 --- a/aggregations_test.go +++ b/aggregations_test.go @@ -61,8 +61,8 @@ func TestAggregations(t *testing.T) { { "a complex, multi-aggregation, nested", Aggregate( - NestedAgg("categories","categories"). - Aggs(TermsAgg("type","outdoors")), + NestedAgg("categories", "categories"). + Aggs(TermsAgg("type", "outdoors")), FilterAgg("filtered", Term("type", "t-shirt")), ), @@ -72,9 +72,9 @@ func TestAggregations(t *testing.T) { "nested": map[string]interface{}{ "path": "categories", }, - "aggs": map[string]interface{} { - "type": map[string]interface{} { - "terms": map[string]interface{} { + "aggs": map[string]interface{}{ + "type": map[string]interface{}{ + "terms": map[string]interface{}{ "field": "outdoors", }, }, @@ -83,7 +83,7 @@ func TestAggregations(t *testing.T) { "filtered": map[string]interface{}{ "filter": map[string]interface{}{ "term": map[string]interface{}{ - "type": map[string]interface{} { + "type": map[string]interface{}{ "value": "t-shirt", }, }, @@ -92,5 +92,33 @@ func TestAggregations(t *testing.T) { }, }, }, + { + "order for termsAggs", + //eq.Aggregate(eq.TermsAgg("a1", "FIELD1").Size(0).Aggs(eq.Sum("a2", "FIELD2.SUBFIELD"))) + Aggregate( + TermsAgg("categories", "categories"). + Order(map[string]string{"priceSum": "desc"}). + Size(5).Aggs(Sum("priceSum", "price"))), + map[string]interface{}{ + "aggs": map[string]interface{}{ + "categories": map[string]interface{}{ + "terms": map[string]interface{}{ + "field": "categories", + "order": map[string]interface{}{ + "priceSum": "desc", + }, + "size": 5, + }, + "aggs": map[string]interface{}{ + "priceSum": map[string]interface{}{ + "sum": map[string]interface{}{ + "field": "price", + }, + }, + }, + }, + }, + }, + }, }) } diff --git a/aggs_bucket.go b/aggs_bucket.go index 09b5185..c13a898 100644 --- a/aggs_bucket.go +++ b/aggs_bucket.go @@ -12,6 +12,7 @@ type TermsAggregation struct { shardSize *float64 showTermDoc *bool aggs []Aggregation + order map[string]string } // TermsAgg creates a new aggregation of type "terms". The method name includes @@ -54,6 +55,12 @@ func (agg *TermsAggregation) Aggs(aggs ...Aggregation) *TermsAggregation { return agg } +// Order sets the sort for terms agg +func (agg *TermsAggregation) Order(order map[string]string) *TermsAggregation { + agg.order = order + return agg +} + // Map returns a map representation of the aggregation, thus implementing the // Mappable interface. func (agg *TermsAggregation) Map() map[string]interface{} { @@ -70,6 +77,9 @@ func (agg *TermsAggregation) Map() map[string]interface{} { if agg.showTermDoc != nil { innerMap["show_term_doc_count_error"] = *agg.showTermDoc } + if agg.order != nil { + innerMap["order"] = agg.order + } outerMap := map[string]interface{}{ "terms": innerMap,