我有一个Forex刻度数据索引,我想创建一个查询,以获得某个“分辨率”的开始和结束日期内的出价。我的意思是决议是每1分钟或几小时我想要1个值,中位数或平均值。
我猜我需要将日期直方图聚合与统计聚合一起使用 - 但是如何?
我正在处理的示例不正确的查询是:
{
"fields": ["ask_price", "bid_price", "tick_date"],
"query": {
"match": {
"currency_pair": currencyPair
}
},
"filter": {
"range": {
"gte": startDate,
"lte": endDate,
"format": "yyyy-MM-dd"
}
},
"aggs": {
"resolution": {
"date_histogram": {
"field": "tick_date",
"interval": "1m"
}
}
}
}
答案 0 :(得分:1)
像这样使用subAggregation
:
{
"fields": ["ask_price", "bid_price", "tick_date"],
"query": {
"match": {
"currency_pair": currencyPair
}
},
"filter": {
"range": {
"gte": startDate,
"lte": endDate,
"format": "yyyy-MM-dd"
}
},
"aggs": {
"resolution": {
"date_histogram": {
"field": "tick_date",
"interval": "1m"
},
"aggs": {
"statistics": {
"stats": {
"field": "field_name_whose_stats_are_to_be_found"
}
}
}
}
}
}
答案 1 :(得分:1)
考虑到@ Richa的回答,我得出了答案。我必须使用Subaggregation
所以最终正确的查询将是:
{
"query": {
"match": {
"currency_pair": currencyPair
}
},
"filter": {
"range": {
"tick_date": {
"gte": startDate,
"lte": endDate,
"format": "yyyy-MM-dd"
}
}
},
"aggs": {
"resolution": {
"date_histogram": {
"field": "tick_date",
"interval": "15m"
},
"aggs": {
"avg_bid": {
"avg": {
"field": "bid_price"
}
},
"avg_ask": {
"avg": {
"field": "ask_price"
}
},
"median_bid": {
"percentiles": {
"field": "bid_price",
"percents": [50]
}
},
"median_ask": {
"percentiles": {
"field": "bid_ask",
"percents": [50]
}
}
}
}
}