1

我的问题是这样的:我有来自不同城市的销售数据:

SHOP NAME | SALES AMOUNT | CITY
shop A,     5000           New York
shop B,     4000           New York
shop C,     3000           New York
shop D,     1800           Boston
shop E,     1500           Boston
shop F,     1300           Boston

现在我想要每个城市的前 2 名销售店铺,然后按销售额的降序排列来自不同城市的前 2 名店铺。所以我们可以得到上面例子的结果:

shop A,  5000  New York
shop B,  4000  New York
shop D,  1800  Boston
shop E,  1500  Boston  

我知道他的聚合可以得到前 2 个桶:

POST shop_info/_search
{
    "aggs": {
        "top_tags": {
            "terms": {
                "field": "city_id",
                "field": "sales",
                "size": 3
            },
            "aggs": {
                "top_sales_hits": {
                    "top_hits": {
                        "sort": [
                            {
                                "sales": {
                                    "order": "desc"
                                }
                            }
                        ],
                        "_source": {
                            "includes": [ "sales", "city_id" ]
                        },
                        "size" : 2
                    }
                }
            }
        }
    }
}

但我不知道如何展平来自不同存储桶的元素:

"buckets": [
        {
          "key": 1487251891000,
          "key_as_string": "2017-02-16 13:31:31",
          "doc_count": 10,
          "top_sales_hits": {
            "hits": {
              "total": 10,
              "max_score": null,
              "hits": [
                {
                  "_index": "shop_info",
                  "_type": "shop_info",
                  "_id": "52",
                  "_score": null,
                  "_source": {
                    "ins_tm": "2017-02-16 13:31:31",
                    "city_id": 12
                  },
                  "sort": [
                    1487251891000
                  ]
                },
                {
                  "_index": "shop_info",
                  "_type": "shop_info",
                  "_id": "48",
                  "_score": null,
                  "_source": {
                    "ins_tm": "2017-02-16 13:31:31",
                    "city_id": 12
                  },
                  "sort": [
                    1487251891000
                  ]
                }
              ]
            }
          }
        },
        {
          "key": 1487251892000,
          "key_as_string": "2017-02-16 13:31:32",
          "doc_count": 10,
          "top_sales_hits": {
            "hits": {
              "total": 10,
              "max_score": null,
              "hits": [
                {
                  "_index": "shop_info",
                  "_type": "shop_info",
                  "_id": "65",
                  "_score": null,
                  "_source": {
                    "ins_tm": "2017-02-16 13:31:32",
                    "city_id": 12
                  },
                  "sort": [
                    1487251892000
                  ]
                },
                {
                  "_index": "shop_info",
                  "_type": "shop_info",
                  "_id": "62",
                  "_score": null,
                  "_source": {
                    "ins_tm": "2017-02-16 13:31:32",
                    "city_id": 12
                  },
                  "sort": [
                    1487251892000
                  ]
                }
              ]
            }
          }
        },

先感谢您!非常感谢任何提示。

4

0 回答 0