在ElasticSearch中存储时间相关数据

vbopmzt1  于 2023-06-05  发布在  ElasticSearch
关注(0)|答案(1)|浏览(400)

我想在ElasticSearch中存储与时间相关的数据。每个文档都有一个开始时间和一个结束时间,定义了它的相关时间(例如,打开的票证)。然后我希望能够运行查询,例如:
1.显示1月5日至2月2日期间开放的所有门票。
1.在某个时间范围内打开的所有票证的多个字段上显示面
由于我将有月度索引,因此跨越一个月以上的文档将存储在多个索引中。
例如,一张在1月到4月之间打开的票需要存储在所有4个月的索引中。
我想知道是否有一种简单的方法可以在以后运行跨索引的聚合,它将知道只考虑一次每个票据(用于分面等)。
例如,如果我有以下门票:
1.门票A于2021年1月1日至2021年1月17日开放
1.门票B于15/1/2021至16/2/2021期间开放
1.门票C于2021年12月1日至2021年3月16日期间开放
我们将有以下文档/索引:

Index January:  
  {id: A, StartDate: 1/1/2021, EndDate: 17/1/2021}
  {id: B, StartDate: 15/1/2021}
  {id: C, StartDate: 12/1/2021}
Index February:  
  {id: B, StartDate: 15/1/2021, EndDate: 16/2/2021}
  {id: C, StartDate: 12/1/2021}
Index March:  
  {id: C, StartDate: 12/1/2021, endDate: 16/3/2021}

任何与使用ElasticSearch实现这样的事情的最佳方式相关的输入都是最受欢迎的。如果有其他适合这项工作的高规模和快速聚合的首选数据库,我也很乐意听到替代品。

ndh0cuux

ndh0cuux1#

如果**这两个条件都满足,这将非常简单:
1.每个文档只有一个StartDate值。
1.每个包含EndDate**的文档也包含StartDate
因此,如果我们建立一个index template,覆盖所有未来的月度指数:

PUT _index_template/monthly-indices
{
  "index_patterns": [
    "index-2021-*"
  ],
  "template": {
    "mappings": {
      "properties": {
        "id": {
          "type": "keyword"
        },
        "StartDate": {
          "type": "date",
          "format": "d/M/yyyy"
        },
        "EndDate": {
          "type": "date",
          "format": "d/M/yyyy"
        }
      }
    }
  }
}

然后我们可以从您的问题中添加示例文档:

POST index-2021-01/_doc/A
{
  "id": "A",
  "StartDate": "1/1/2021",
  "EndDate": "17/1/2021"
}

POST index-2021-01/_doc/B
{
  "id": "B",
  "StartDate": "15/1/2021"
}

POST index-2021-02/_doc/B
{
  "id": "B",
  "StartDate": "15/1/2021",
  "EndDate": "16/2/2021"
}

POST index-2021-02/_doc/C
{
  "id": "C",
  "StartDate": "12/2/2021"
}

POST index-2021-03/_doc/C
{
  "id": "C",
  "StartDate": "12/2/2021",
  "EndDate": "16/3/2021"
}

在那之后,问题“哪些门票在2021年1月1日至2021年3月31日之间开放?”可以通过以下方式回答:

POST index-2021-*/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "StartDate": {
              "gte": "1/1/2021"
            }
          }
        },
        {
          "range": {
            "EndDate": {
              "lte": "31/3/2021"
            }
          }
        }
      ]
    }
  }
}

它返回文档ABC--但显然只返回那些同时具有开始和结束日期的条目。
接下来,可以使用这个脚本化的date_histogram聚合来构建一个简单的“facet”统计数据,表示“每月有多少票证打开”:

POST index-2021-*/_search
{
  "size": 0,
  "aggs": {
    "open_count_by_months": {
      "date_histogram": {
        "interval": "month",
        "format": "MMMM yyyy", 
        "script": """
          def start = doc['StartDate'].value;
          def end = doc['EndDate'].size() == 0 ? null : doc['EndDate'].value;
          
          if (end == null) {
            return start
          }
          
          return end
        """
      }
    }
  }
}

屈服

"aggregations" : {
  "open_count_by_months" : {
    "buckets" : [
      {
        "key_as_string" : "January 2021",
        "key" : 1609459200000,
        "doc_count" : 2
      },
      {
        "key_as_string" : "February 2021",
        "key" : 1612137600000,
        "doc_count" : 2
      },
      {
        "key_as_string" : "March 2021",
        "key" : 1614556800000,
        "doc_count" : 1
      }
    ]
  }
}

最后,如果您设法对文档的内容“散布”在多个索引上保持可靠的控制,那么就可以了。
然而,如果你只是让一个cronjob每天同步工单,并构建你的文档,使它们像消息一样“宣布”当前工单状态,即:

{
  "ticket_id": A,
  "status": "OPEN"
  "timestamp": 1613428738570
}

它将引入一些令人讨厌的复杂性,我在前面讨论过herehere

相关问题