我想在ElasticSearch中存储与时间相关的数据。每个文档都有一个开始时间和结束时间,它们定义了相关的时间(例如,公开票证)。然后,我希望能够运行查询,例如:
由于我将拥有每月索引,因此跨越一个月以上的文档将存储在多个索引中。
例如,在一月和四月之间打开的票证需要存储在所有4个月索引的索引中。
我想知道是否有一种简单的方法可以稍后在跨索引的聚合上运行,这些聚合将知道只考虑每个票证一次(用于分面等)。
例如,如果我有以下票证:
我们将提供以下文件/索引:
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实现这种事情的最佳方法有关的任何输入。如果还有其他适合大规模,快速聚合的首选数据库,我也很高兴听到其他替代方案。
如果同时满足以下两个条件,这将非常简单:
StartDate
值。EndDate
也都有一个StartDate
。因此,如果我们建立涵盖未来所有月度指数的指数模板:
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"
}
之后,出现问题“哪个门票在1/1/2021和31/3/2021之间开放?” 然后可以通过以下方式回答:
POST index-2021-*/_search
{
"query": {
"bool": {
"must": [
{
"range": {
"StartDate": {
"gte": "1/1/2021"
}
}
},
{
"range": {
"EndDate": {
"lte": "31/3/2021"
}
}
}
]
}
}
}
它返回docs A
,B
和C
,但显然只有那些同时具有开始和结束日期的条目。
展望未来,可以使用以下脚本化date_histogram
汇总来构造一个简单的“方面”统计数据,表示“每月有多少张票证打开” :
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
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句