【问题标题】:How to do a range aggregation in elasticsearch如何在elasticsearch中进行范围聚合
【发布时间】:2020-12-19 09:34:13
【问题描述】:

enter image description here 按字段聚合时userguid

{
  "_source": false,
  "aggregations": {
    "range_userGuid": {
      "terms": {
        "field": "userGuid"
      }
    }
  }
}

我得到结果

  "aggregations" : {
    "range_userGuid" : {
      "doc_count_error_upper_bound" : 151,
      "sum_other_doc_count" : 2424145,
      "buckets" : [
        {
          "key" : 803100110976,
          "doc_count" : 1
        },
        {
          "key" : 813110447915,
          "doc_count" : 10
        },
        {
          "key" : 803100110306,
          "doc_count" : 101
        },
        {
          "key" : 2123312,
          "doc_count" : 300
        },
        {
          "key" : 3452342,
          "doc_count" : 9999
        },
      ]
    }
  }

现在我想从 aggs 结果中获取范围。例如(0-100],(100-1000],>1000,获取用户数。预期结果:

[
    {
        "from": 0,
        "to": 100,
        "count": 2  <---- 2 users, 803100110976 and 813110447915
    },
    {
        "from": 100,
        "to": "1000",
        "count": 2  <---- 803100110306 and 2123312
    },
    {
        "from": 1001,
        "count": 1 <---- 3452342
    }
]

aggs的bucket大小在150000左右,这样的查询怎么写?

【问题讨论】:

  • 这能回答你的问题吗? Range ElasticSearch Aggregation
  • @Jian userGuid 字段的数据类型是什么?是数字吗?
  • @Val userGuid 是数字,桶大小会大到150000左右

标签: elasticsearch elasticsearch-aggregation


【解决方案1】:

您可以使用range aggregation 来实现您的期望:

POST /test/_search
{
   "size": 0,
   "aggs": {
      "range_userGuid": {
         "range": {
            "field": "userGuid",
            "ranges": [
               {
                  "from": 0,
                  "to": 100
               },
               {
                  "from": 100,
                  "to": 200
               },
               {
                  "from": 200,
                  "to": 1000
               },
               {
                  "from": 1000
               }
            ]
         }
      }
   }
}

更新:根据您的需要调整this answer

POST index/_search
{
  "size": 0,
  "aggs": {
    "users_0_100": {
      "terms": {
        "field": "userGuid",
        "size": 1000
      },
      "aggs": {
        "0_100": {
          "bucket_selector": {
            "buckets_path": {
              "docCount": "_count"
            },
            "script": "params.docCount < 100"
          }
        }
      }
    },
    "users_100_200": {
      "terms": {
        "field": "userGuid",
        "size": 1000
      },
      "aggs": {
        "100_200": {
          "bucket_selector": {
            "buckets_path": {
              "docCount": "_count"
            },
            "script": "params.docCount >= 100 && params.docCount < 200"
          }
        }
      }
    },
    "users_200_1000": {
      "terms": {
        "field": "userGuid",
        "size": 1000
      },
      "aggs": {
        "200_1000": {
          "bucket_selector": {
            "buckets_path": {
              "docCount": "_count"
            },
            "script": "params.docCount >= 200 && params.docCount < 1000"
          }
        }
      }
    },
    "users_1000": {
      "terms": {
        "field": "userGuid",
        "size": 1000
      },
      "aggs": {
        "1000": {
          "bucket_selector": {
            "buckets_path": {
              "docCount": "_count"
            },
            "script": "params.docCount >= 1000"
          }
        }
      }
    }
  }
}

【讨论】:

  • 我想获取 userGuid 出现的计数范围,而不是 userGuid 范围。例如: (0-1500] : 1 (1500,10000]:2
  • 好的,我没有从你的描述中得到它,对不起。然后@SahilGupta 引用的我的另一个答案应该可以满足您的需要。
  • 我期望的范围字段是聚合的 doc_count range_userGuid
  • 是的,这就是我提到的另一个答案所提供的
  • 您提供的示例按字段 amount 进行范围,但我的 excpet 范围是 doc_count
猜你喜欢
  • 2018-11-28
  • 1970-01-01
  • 2016-04-13
  • 1970-01-01
  • 1970-01-01
  • 2018-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多