Can the max-metric aggs return any fields i want of the doc which is the max in bucket


(Red) #1

i want to retrieval the sku id in the doc which has sold most.
i need to use aggs to aggs the spu, and find out the best sold sku.
here is my code

          "size":0,
          "aggregations": {
          "spu_group": {
                "terms": {
                  "field": "product_id",
                  "size": 4,
                  "order" : {"sum_sku_sell_num": "desc"}
                },
                "aggregations": {
                    "sum_sku_sell_num":{
                        "sum": {
                            "field": "sku_sell_num"
                        }
                    },
                    "max_sku_sell_num_sku":{
                       "top_hits": {
                            "sort":[
                                {
                                    "sku_sell_num": {
                                        "order": "desc",
                                        "missing": 0
                                    }
                                }
                            ],
                            "_source": {
                                "include": [
                                    "sku_id"
                                ]
                            },
                            "from": 0,
                            "size": 1
                        }
                    }
                }
            }
          }

top_hits can satisfy my request. but i think its a little complex, and max-metric is simple to use, but i dont know how to return any info of the doc.

and top_hits may have low performace than max-metric, metric aggs doesnot create bucket, it only calculate?
although the top_hits is a metric-aggs, it will create and sort the docs if i want to get the max doc.

i may use the top_hits only if it behaves like the max-metric when i set the size of top_hits to 1 .
may i do this??????


(system) #2