使用 doc_count 作为累计计数

Use doc_count as cumulative count

我目前正在尝试根据 Elastic Search 中收集的数据生成图表。每次生成用户时,我在 ES 中插入一条记录,具有以下(示例)数据:

{
  "country": "US",
  "id": "79ca9523dcd62420030de12b75e08bb7",
  "createdAt": "1450912898"
}

ID 是用户 ID 的哈希值,因此出于隐私原因,无法从存储在 ES 中的 ID 确定用户 ID。

ES索引中的类型映射如下:

{
  "user": {
    "_timestamp": {
      "enabled": true
    },
    "properties": {
      "country": {
        "type": "string"
      },
      "createdAt": {
        "type": "date",
        "format": "epoch_second"
      },
      "id": {
        "type": "string",
        "index": "not_analyzed"
      }
    }
  }
}

现在,要获取每天的用户图表,我有以下查询:

{
  "size": 0,
  "query": {
    "type": {
      "value": "user"
    }
  },
  "aggs": {
    "users_per_day": {
      "date_histogram": {
        "field": "createdAt",
        "interval": "day"
      }
    }
  }
}

这给了我这样一个不错的结果(对于结果,我将间隔设置为分钟,让您稍微了解问题所在):

[{
  "key_as_string": "1450909920",
  "key": 1450909920000,
  "doc_count": 8
},
{
  "key_as_string": "1450909980",
  "key": 1450909980000,
  "doc_count": 2
},
{
  "key_as_string": "1450910040",
  "key": 1450910040000,
  "doc_count": 5
},
{
  "key_as_string": "1450910100",
  "key": 1450910100000,
  "doc_count": 8
},
{
  "key_as_string": "1450910160",
  "key": 1450910160000,
  "doc_count": 4
},
{
  "key_as_string": "1450910220",
  "key": 1450910220000,
  "doc_count": 3
},
{
  "key_as_string": "1450910280",
  "key": 1450910280000,
  "doc_count": 6
}]

我想使用 doc_count 生成一个累积图,这样我就可以看到我的用户群的增长情况,而不是每天创建的帐户数量。尽管在互联网上搜索,但我找不到似乎与我的问题相关的单一答案。我找到的大多数答案都将我引导至 Cumulative Sum Aggregation 页面,但此处给出的示例将为您提供单个存储桶中捕获的所有结果的累计总和。我想要所有桶总数的累计总和。

cumulative sum aggregation and you can definitely use it. You just need to use the special _count bucket path 你走在正确的道路上,这将完成你期望的工作。

{
  "size": 0,
  "query": {
    "type": {
      "value": "user"
    }
  },
  "aggs": {
    "users_per_day": {
      "date_histogram": {
        "field": "createdAt",
        "interval": "day"
      },
      "aggs": {
        "cumulative": {
          "cumulative_sum": {
            "buckets_path": "_count"
          }
        }
      }
    }
  }
}

结果将如下所示:

[{
  "key_as_string": "1450909920",
  "key": 1450909920000,
  "doc_count": 8,
  "cumulative": {"value": 8}
},
{
  "key_as_string": "1450909980",
  "key": 1450909980000,
  "doc_count": 2,
  "cumulative": {"value": 10}
},
{
  "key_as_string": "1450910040",
  "key": 1450910040000,
  "doc_count": 5,
  "cumulative": {"value": 15}
},
{
  "key_as_string": "1450910100",
  "key": 1450910100000,
  "doc_count": 8,
  "cumulative": {"value": 23}
},
{
  "key_as_string": "1450910160",
  "key": 1450910160000,
  "doc_count": 4,
  "cumulative": {"value": 27}
},
{
  "key_as_string": "1450910220",
  "key": 1450910220000,
  "doc_count": 3,
  "cumulative": {"value": 30}
},
{
  "key_as_string": "1450910280",
  "key": 1450910280000,
  "doc_count": 6,
  "cumulative": {"value": 36}
}]