【问题标题】:How can I reshape my data before I turn it into a histogram?在将数据转换为直方图之前,如何重塑数据?
【发布时间】:2016-03-17 18:56:08
【问题描述】:

假设我有一个访问索引,其中包含“访问”类型的文档,如下所示:

{
               "id": "c223a991-b4e7-4333-ba45-a576010b568b",
// other properties
               "buildingId": "48da1a81-fa73-4d4f-aa22-a5750162ed1e",
               "arrivalDateTimeUtc": "2015-12-22T21:15:00Z"
}

以下函数将返回一个直方图,该直方图根据给定时区返回给定范围内每天的访问桶。

    public Bucket<HistogramItem> Execute(MyParameterType parameters)
    {
        var buildingFilter = Filter<VisitProjection>.Term(x => x.BuildingId, parameters.BuildingId);
        var dateFilter = Filter<VisitProjection>.Range(r => r
            .OnField(p => p.ArrivalDateTimeUtc)
            .GreaterOrEquals(parameters.EarliestArrivalDateTimeUtc)
            .LowerOrEquals(parameters.LatestArrivalDateTimeUtc)
        );

        var result = _elasticClient.Search<VisitProjection>(s => s
            .Index("visits")
            .Type("visit")
            .Aggregations(a => a
                .Filter("my_filter_agg", f => f
                    .Filter(fd => buildingFilter && dateFilter)
                        .Aggregations(ta => ta.DateHistogram("my_date_histogram", h => h
                            .Field(p => p.ArrivalDateTimeUtc)
                            .Interval(parameters.DateInterval) // "day"
                            .TimeZone(NodaTimeHelpers.WindowsToIana(parameters.TimeZoneInfo)) // This is a critical piece of the equation.
                            .MinimumDocumentCount(0)
                        )
                    )
                )
            )
        );

        return result.Aggs.Nested("my_filter_agg").DateHistogram("my_date_histogram");
    }
}

// Returns [{Date: 12/22/2015 12:00:00 AM, DocCount: 1}]

现在想象一下我改变了一些东西。想象一下,我在文档中添加了一个新字段:

{
               "id": "c223a991-b4e7-4333-ba45-a576010b568b",
// other properties
               "buildingId": "48da1a81-fa73-4d4f-aa22-a5750162ed1e",
               "arrivalDateTimeUtc": "2015-12-22T21:15:00Z",
               "departureDateTimeUtc": "2015-12-23T22:00:00Z" // new property
}

并假设我要返回以下内容:

// Returns [{Date: 12/22/2015 12:00:00 AM, DocCount: 1}, {Date: 12/23/2015 12:00:00 AM, DocCount: 1}]

因为访问跨越两天,我想要一个日期直方图,记录访问跨越的每一天的一个单位。

如何使用 NEST/Elastic Search 做到这一点?


注 1:除非有人说服我,否则我认为收集范围内的所有文档并在中间层(或 C# 层)执行聚合/桶化和日期直方图不是一个好主意。

注意 2:这个问题的时区方面很关键,因为我需要根据给定的时区对计数进行分桶。

【问题讨论】:

    标签: c# elasticsearch nest


    【解决方案1】:

    一种方法是使用scripted_metric aggregation 并根据您的两个日期字段自己执行分桶。根据您拥有的文档数量,相当复杂且性能不佳。

    但是,另一种更简单的解决方案是使用单个日期字段并将间隔的所有日期放入一个数组(首先到达,最后离开以及介于两者之间的所有其他日期),如下所示:

    {
         "id": "c223a991-b4e7-4333-ba45-a576010b568b",
         "buildingId": "48da1a81-fa73-4d4f-aa22-a5750162ed1e",
         "visitDateTimeUtc": ["2015-12-22T21:15:00Z", "2015-12-23T22:00:00Z" ]
    }
    

    如果一次访问跨越三/四/等天,您可以用到达和离开之间的天数“填充”数组

    {
         "id": "c223a991-b4e7-4333-ba45-a576010b568b",
         "buildingId": "48da1a81-fa73-4d4f-aa22-a5750162ed1e",
         "visitDateTimeUtc": ["2015-12-22T21:15:00Z", "2015-12-23T22:00:00Z", "2015-12-24T22:00:00Z", "2015-12-25T22:00:00Z" ]
    }
    

    通过这样做,您的date_histogram 聚合将考虑间隔的所有日期。

    【讨论】:

      【解决方案2】:

      我会考虑通过在您的 ES 模型上为 VisitDays 设置一个新的数组属性来解决这个问题,因此如果有人从 2015 年 1 月 1 日到 2015 年 1 月 5 日一直停留,那么您的模型将是这样的:

      {
          "id" : "c223a991-b4e7-4333-ba45-a576010b568b",
          // other properties
          "buildingId" : "48da1a81-fa73-4d4f-aa22-a5750162ed1e",
          "arrivalDateTimeUtc" : "2015-01-01T21:15:00Z",
          "departureDateTimeUtc" : "2015-01-05T22:00:00Z", // new property
          "visitDays" : [
              "2015-01-01",
              "2015-01-02",
              "2015-01-03",
              "2015-01-04",
              "2015-01-05"
          ]
      }
      

      如果您这样做了,那么您的分桶将非常容易且非常快速。脚本化的字段不会很快。你是 100% 正确的,在 C# 中尝试这样做是行不通的,因为它太慢了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-14
        • 1970-01-01
        • 2020-05-28
        • 1970-01-01
        • 2021-02-02
        • 1970-01-01
        • 2021-10-22
        • 2019-09-09
        相关资源
        最近更新 更多