【问题标题】:Elasticsearch date histogram for earliest timestamp by distinct value不同值的最早时间戳的 Elasticsearch 日期直方图
【发布时间】:2019-08-28 00:21:53
【问题描述】:

我有一个 ES 索引,其中每个文档都是物理城市中的一个事件。这些文档包含一堆关于事件的信息,以及事件发生的时间戳。 即

[{
  "host_city": "denver",
  "timestamp":" "2019-08-26T07:10:07Z",
  "other_meta": 123
},
{
  "host_city": "denver",
  "timestamp":" "2019-08-24T07:13:17Z",
  "other_meta": 123
},
{
  "host_city": "washington",
  "timestamp":" "2019-05-21T09:10:00Z",
  "other_meta": 123
},

所有城市都有多个活动,但就这个问题而言,我只对他们举办有史以来第一次活动感兴趣。

我需要创建一个日期直方图,显示(每天)过去 30 天内举办第一次活动的城市数量。如果一个城市在过去 30 天内举办过活动,但在此之前的任何时间举办过任何活动,则应将其从直方图中排除 - 我试图仅显示 全新的城市

  • 2019 年 1 月 2 日:50 个城市举办了首场活动
  • 1/3/2019:16个城市举办首场活动 *等等。

我目前正在从 solr 迁移到 ES,所以没有要显示的示例代码。目前我正在做大量的预处理来实现这一点:我第一次在每个城市看到一个事件时维护一个单独的索引,当我看到一个全新的城市时添加一个新文档。然后我对这个单独的索引进行基本的直方图聚合。

我希望能够从我的原始事件文档中即时计算此直方图 - 这在 elasticsearch 中是否可行?我需要什么类型的查询来实现这一点?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    使用 7.3 中内置的新 Data Frames 转换(这是一项付费功能)非常简单,它将为您构建一个衍生的、面向城市的索引。

    使用数据框,您可以定义一个作业以按 host_city 分组并按最小值 timestamp 聚合

    PUT _data_frame/transforms/first-appearance-dataframe
    {
      "description": "Track the first appearance data of all host cities"
      "source": {
        "index": [
          "my-source-index*"
        ],
        "query": {
          "match_all": {}
        }
      },
      "dest": {
        "index": "city-first-appearances"
      },
      "pivot": {
        "group_by": {
          "userid": {
            "terms": {
              "field": "host_city"
            }
          }
        },
        "aggregations": {
          "first_timestamp": {
            "min": {
              "field": "timestamp"
            }
          }
        }
      }
    }
    

    然后,就是一个简单的日期直方图,得到你想要的

    GET city-first-appearances/_search?size=0
    {
      "aggs": {
        "new-cities": {
          "date_histogram": {
            "field": "first-timestamp",
            "calendar_interval": "day"
          }
        }
      }
    }
    

    可以通过在创建作业时传递sync 参数将数据框设置为连续运行,因此每次运行直方图聚合时都会保持最新。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多