olivere / elastic

Deprecated: Use the official Elasticsearch client for Go at https://github.com/elastic/go-elasticsearch
https://olivere.github.io/elastic/
MIT License
7.39k stars 1.15k forks source link

elastic.v7 NewBucketSelectorAggregation function have a bug,can not execute correct #1624

Closed henrY2Young closed 2 years ago

henrY2Young commented 2 years ago

this is my code

agg:=elastic.NewTermsAggregation().Field("userid").Size(1000000). SubAggregation("sum_order",elastic.NewSumAggregation().Field("orders_times_all"). SubAggregation("bucket_filter",elastic.NewBucketSelectorAggregation().AddBucketsPath("sum_order","sum_order").Script(elastic.NewScript("params.sum_order>1"))))

this is source() out

"aggregations": { "sum_order": { "aggregations": { "bucket_filter": { "bucket_selector": { "buckets_path": { "sum_order": "sum_order" }, "script": { "source": "params.sum_order\u003e1" } } } }, "sum": { "field": "orders_times_all" } } }, "terms": { "field": "userid", "size": 1000000 } }

but es has a panic

elastic: Error 500 (Internal Server Error): Aggregator [sum_order] of type [sum] cannot accept sub-aggregations [type=aggregation_initialization_exception]

Am I using it the wrong way? OR this is a bug

Morriaty-The-Murderer commented 2 years ago

move SubAggregation("bucket_filter", ...) to outer NewTermsAggregation, like this

mainAgg := elastic.NewTermsAggregation().Field("userid").Size(1000000)

mainAgg.SubAggregation("sum_order",elastic.NewSumAggregation().Field("orders_times_all"))

mainAgg.SubAggregation("bucket_filter",elastic.NewBucketSelectorAggregation().AddBucketsPath("sum_order","sum_order").Script(elastic.NewScript("params.sum_order>1")))
henrY2Young commented 2 years ago

膜拜,太感谢了,关注了

olivere commented 2 years ago

Also notice the hundreds of tests. They illustrate how to use the library.