簡體   English   中英

如何獲取 elasticsearch 中文檔的開始日期和結束日期字段之間的每一天的存儲桶

[英]How to get a bucket for each day between start date and end date fields of a document in elasticsearch

我在 elasticsearch 的索引中有如下文件

[
    {
        "Id": 1,
        "start": "2021-04-20T00:00:00.000000Z",
        "end": "2021-04-22T22:45:20.000000Z",
        "event_type": "A"
    },
    {
        "Id": 2,
        "start": "2021-04-23T00:01:00.000000Z",
        "end": "2021-04-26T21:50:20.000000Z",
        "event_type": "B"
    },
    {
        "Id": 3,
        "start": "2021-04-27T00:03:30.000000Z",
        "end": "2021-04-29T04:15:30.000000Z",
        "event_type": "A"
    }
]

我想計算每個event_type跨越的天數。 例如,對於上述文檔, event_type A"Id": 1從 20 日(開始)到 22 日(結束),另外 3 天在"Id": 3從 27 日(開始) 至 29 日(結束)。 所以我想為event_type A計數 6 。 event_type B在第二個文檔中跨越 4 天, "Id": 2從 23 日(開始)到 26 日(結束)。 所以,對於event_type B ,我想計數為 4。

事件類型 數數
一個 6
4

我知道如何使用術語聚合來獲取每種事件類型的文檔數

GET /split_range/_search
{
  "size": 0,
  "aggs": {
    "by_event_type": {
      "terms": {
        "field": "event_type"
      }
    }
  }
}

有沒有辦法在開始和結束(包括開始和結束)之間的每一天將該文檔進一步拆分為多個存儲桶並獲得該計數?

如果 json 有點一致,我將始終使用 pandas 來完成此類任務。

data = [
    {
        "Id": 1,
        "start": "2021-04-20T00:00:00.000000Z",
        "end": "2021-04-22T22:45:20.000000Z",
        "event_type": "A"
    },
    {
        "Id": 2,
        "start": "2021-04-23T00:01:00.000000Z",
        "end": "2021-04-26T21:50:20.000000Z",
        "event_type": "B"
    },
    {
        "Id": 3,
        "start": "2021-04-27T00:03:30.000000Z",
        "end": "2021-04-29T04:15:30.000000Z",
        "event_type": "A"
    }
]
df = pd.DataFrame(data)
df['end'] =  pd.to_datetime(df['end']).dt.date
df['start'] =  pd.to_datetime(df['start']).dt.date
df["diff"] = (df["end"] - df["start"])
print(df.head())

將導致:

   Id         end event_type       start   diff
0   1  2021-04-22          A  2021-04-20 2 days
1   2  2021-04-26          B  2021-04-23 3 days
2   3  2021-04-29          A  2021-04-27 2 days

您可以將 sum sub 聚合與腳本一起使用,如下所述。

  1. 在字段event_type上使用term聚合來獲取唯一的事件類型。
  2. 添加sum子聚合以計算每種事件類型的天數總和。
  3. sum聚合中提供了計算日差的script
GET split_range/_search
{
  "size": 0,
  "aggs": {
    "GroupByEventType": {
      "terms": {
        "field": "event_type",
        "size": 10000,
        "order": {
          "_key": "asc"
        }
      },
      "aggs": {
        "sumOfDays": {
          "sum": {
            "script": """
                ZonedDateTime start = doc['start'].value;
                ZonedDateTime end = doc['end'].value;
                return start.until(end, ChronoUnit.DAYS);
             """
          }
        }
      }
    }
  }
}

當我針對您問題中提供的示例數據集嘗試上述查詢時,得到了以下答案。

{
  "aggregations" : {
    "GroupByEventType" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "A",
          "doc_count" : 2,
          "sumOfDays" : {
            "value" : 4.0
          }
        },
        {
          "key" : "B",
          "doc_count" : 1,
          "sumOfDays" : {
            "value" : 3.0
          }
        }
      ]
    }
  }
}

響應包含每個事件類型的子聚合,即天數總和。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM