【问题标题】:Elasticsearch Date Histogram with a Point in Time count of documents带有时间点文档计数的 Elasticsearch 日期直方图
【发布时间】:2023-04-06 09:31:02
【问题描述】:

我正在尝试创建一个显示每月员工数量的日期直方图。

员工映射如下所示:

{
    "number": 1234,
    "firstName": "Chris",
    "lastName": "Smith",
    "employmentDates: [
        {
            "startDate": "2014-10-03T06:00:00Z",
            "endDate": "2017-11-04T06:00:00Z"
        }
    ],
    "lastPaidOnDate": "2017-11-10T06:00:00Z",
    ....
}

给定这样的开始和结束场景(三名员工):

|----------------|
       |-----------------------------|
  |---|    |---------------------|
 ^   ^   ^   ^   ^   ^

我希望直方图与此类似:

"aggregations": {
    "employees_per_month": {
        "buckets": [
            {
                "key_as_string": "2017-01-01",
                "doc_count": 1
            },
            {
                "key_as_string": "2017-02-01",
                "doc_count": 2
            },
            {
                "key_as_string": "2017-03-01",
                "doc_count": 2
            },
            {
                "key_as_string": "2017-04-01",
                "doc_count": 3
            },
            {
                "key_as_string": "2017-05-01",
                "doc_count": 3
            },
            {
                "key_as_string": "2017-06-01",
                "doc_count": 2
            }
        ]
    }
}

似乎我需要在脚本字段上进行子聚合,但我不确定从哪里开始。

非常感谢您的帮助。

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation


    【解决方案1】:

    我相信这可以通过使用 DateHistogram 来完成。但我建议一种简单的方法。您必须在特定月份每次运行查询:

    {
      "size": 0,
      "aggregations": {
        "bool_agg": {
          "filter": {
            "bool": {
              "must": [
                {
                  "range": {
                    "employmentDates.startDate": {
                      "lt": "2017-12-01T00:00:00Z"
                    }
                  }
                },
                {
                  "range": {
                    "employmentDates.endDate": {
                      "gte": "2017-11-01T00:00:00Z"
                    }
                  }
                }
              ]
            }
          },
          "aggregations": {
            "distinct_agg": {
              "cardinality": {
                "field": "number"
              }
            }
          }
        }
      }
    }
    

    注意如果employmentDates 包含多于一条记录,例如:

    "employmentDates: [
       {
          "startDate": "2014-10-03T06:00:00Z",
          "endDate": "2017-11-04T06:00:00Z"
       }
       {
          "startDate": "2018-03-03T06:00:00Z",
          "endDate": "2018-07-04T06:00:00Z"
       }
    

    您必须使用Nested Datatype 进行嵌套,示例可以在here 中找到。 并将查询更新为:

    {
      "size": 0,
      "aggregations": {
        "nested_agg": {
          "nested": {
            "path": "employmentDates"
          },
          "aggregations": {
            "bool_agg": {
              "filter": {
                "bool": {
                  "must": [
                    {
                      "range": {
                        "employmentDates.startDate": {
                          "lt": "2017-12-01T00:00:00Z"
                        }
                      }
                    },
                    {
                      "range": {
                        "employmentDates.endDate": {
                          "gte": "2017-11-01T00:00:00Z"
                        }
                      }
                    }
                  ]
                }
              },
              "aggregations": {
                "comment_to_issue": {
                  "reverse_nested": {},
                  "aggregations": {
                    "distinct_agg": {
                      "cardinality": {
                        "field": "number"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-14
      • 1970-01-01
      • 2021-11-29
      • 2016-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-06
      相关资源
      最近更新 更多