簡體   English   中英

ElasticSearch 獲得聚合中每個特定術語的前 2 個結果

[英]ElasticSearch get top 2 results per specific term in aggregations

鑒於此查詢,我只想檢索並限制每個國家/地區的前 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
          }
        }
      ]
    }
  }
}

下面的聚合查詢為每個國家/地區提供 2 個城市。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM