Kibana ElasticSearch获取聚合中每个特定字词前2个结果

kyvafyod  于 2022-12-09  发布在  Kibana
关注(0)|答案(1)|浏览(201)

对于此查询,我希望仅检索每个国家/地区的前2个城市。因此,对于最受欢迎的国家/地区,检索前2个城市,然后检索下一个国家/地区、前2个城市,等等。

{
  "size": 0,
  "aggs": {
    "user_city_id": {
      "terms": {
        "field": "user.city_id",
        "size": 999
      },
      "aggs": {
        "user_country_id": {
          "terms": {
            "field": "user.country_id",
            "size": 1
          },
          "aggs": {
            "user_name": {
              "terms": {
                "field": "user.name",
                "size": 1
              }
            }
          }
        }
      }
    }
  },
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "user.category": 1
          }
        }
      ]
    }
  }
}
l2osamch

l2osamch1#

下面的汇总查询为您提供每个国家2个城市。

{
  "aggs": {
    "user_country_id": {
      "terms": {
        "field": "user.country_id",
        "size": 10
      },
      "aggs": {
        "user_city_id": {
          "terms": {
            "field": "user.city_id",
            "size": 2
          }
        }
      }
    }
  }
}

相关问题