【问题标题】:Aggregate document value per hour每小时汇总文件价值
【发布时间】:2018-07-26 12:30:35
【问题描述】:

我有一个关于聚合的问题。我读到了Date Histogram Aggregation。但它只按日期对文档进行排序。所以我有索引 visits 字段 datevisited_pa​​ge。我想汇总例如每小时的计数(例如每小时用户访问页面)。应该使用上面的聚合还是我应该以不同的方式聚合?

【问题讨论】:

  • visited_pa​​ge 类型是什么?整数?细绳?可以举个数据的例子吗?
  • 让它成为字符串。我只需要每小时每个visited_pa​​ge 计数

标签: elasticsearch


【解决方案1】:

查询应该是这样的:

GET {index_name}/{type}/_search
{
  "size": 0, // no need to display search result, can boost query speed
  "aggs": {
    "unique_visited_page": {
      "terms": {
        "field": "visited_page" // this must be indexed with keyword type
      },
      "aggs": {
        "visit_page_per_hour" : {
          "date_histogram" : {
              "field" : "date_field",
              "interval" : "hour"
          }
        }
      }
    }
  }
}

我们首先按 visited_pa​​ge 进行汇总,然后按每个 visited_pa​​ge 进行汇总,我们每小时向下钻取以获取计数。

使用我的示例数据的示例响应

{
  ...
  "hits": {
    "total": 4,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "unique_visited_page": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "contact.html",
          "doc_count": 2,
          "visit_page_per_hour": {
            "buckets": [
              {
                "key_as_string": "2018-07-24T14:00:00.000Z",
                "key": 1532440800000,
                "doc_count": 1
              },
              {
                "key_as_string": "2018-07-24T15:00:00.000Z",
                "key": 1532444400000,
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key": "index.html",
          "doc_count": 1,
          "visit_page_per_hour": {
            "buckets": [
              {
                "key_as_string": "2018-07-24T13:00:00.000Z",
                "key": 1532437200000,
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key": "page.html",
          "doc_count": 1,
          "visit_page_per_hour": {
            "buckets": [
              {
                "key_as_string": "2018-07-24T13:00:00.000Z",
                "key": 1532437200000,
                "doc_count": 1
              }
            ]
          }
        }
      ]
    }
  }
}

结果的关键是我们的 visited_pa​​ge 值,然后它将每小时汇总并返回 doc_countdoc_count 可能是您想要的值。

希望对你有帮助。

【讨论】:

  • 终于查到了你的答案。几乎没有问题,但都解决了。这正是我所需要的。非常感谢您的帮助
【解决方案2】:

看起来您需要多桶聚合。 我找到了this

你感兴趣的是这个:

 GET /_search
{
    "aggs" : {
        "my_buckets": {
            "composite" : {
                "sources" : [
                    { "date": { "date_histogram": { "field": "timestamp", "interval": "1d" } } },
                    { "product": { "terms": {"field": "product" } } }
                ]
            }
        }
    }
}

这将从两个值源、一个 date_histogram 和一个术语创建的值创建复合存储桶。每个桶由两个值组成,一个用于聚合中定义的每个值源。允许任何类型的组合,并且数组中的顺序保留在复合桶中。

有帮助吗?

【讨论】:

  • 我对你的例子有一个例外。我应该为这些聚合添加一些元数据到索引吗? SearchParseException[Could not find aggregator type [composite] in [my_buckets]];
  • @Shioshin 您使用的是哪个版本的 ES?正如您在文档中看到的那样,这种类型的聚合处于 beta 状态,看起来它是在 6.1 中首次引入的
猜你喜欢
  • 1970-01-01
  • 2020-09-26
  • 2011-07-09
  • 2012-10-21
  • 1970-01-01
  • 2016-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多