我想从elasticsearch查询数组字段。我有一个数组字段,其中包含分配给作业的一个或多个gpu的节点号。假设某些人可能与其他人共享同一gpu节点,那么不同的人可能会同时使用同一节点。我想获取在特定时间使用的不同节点的总数。
说我有三行数据,它们属于同一时间间隔。我想绘制一个直方图,显示该时期内有三个节点被占用。我可以在Kibana上实现这一目标吗?
示例:
[3]
[3,4,5]
[4,5]
我期望输出为3,因为仅使用了3个不同的节点。
预先感谢
答案 0 :(得分:0)
您可以将日期直方图聚合与术语聚合(如果节点的确切数目很重要)或基数聚合(如果您可以接受较高基数的某些误差)组合使用,以实现此目的。
完整示例:
# Start with a clean slate
DELETE test-index
# Create the index
PUT test-index
{
"mappings": {
"event": {
"properties": {
"nodes": {
"type": "integer"
},
"timestamp": {
"type": "date"
}
}
}
}
}
# Index a few events (using the rows from your question)
POST test-index/event/_bulk
{"index":{}}
{"timestamp": "2018-06-10T00:00:00Z", "nodes":[3]}
{"index":{}}
{"timestamp": "2018-06-10T00:01:00Z", "nodes":[3,4,5]}
{"index":{}}
{"timestamp": "2018-06-10T00:02:00Z", "nodes":[4,5]}
# STRATEGY 1: Cardinality aggregation (scalable, but potentially inaccurate)
POST test-index/event/_search
{
"size": 0,
"aggs": {
"active_nodes_histo": {
"date_histogram": {
"field": "timestamp",
"interval": "hour"
},
"aggs": {
"active_nodes": {
"cardinality": {
"field": "nodes"
}
}
}
}
}
}
# STRATEGY 2: Terms aggregation (exact, but potentially much more expensive)
POST test-index/event/_search
{
"size": 0,
"aggs": {
"active_nodes_histo": {
"date_histogram": {
"field": "timestamp",
"interval": "hour"
},
"aggs": {
"active_nodes": {
"terms": {
"field": "nodes",
"size": 10
}
}
}
}
}
}
注意:
hour
更改为minute
,则会看到直方图本身具有更多数据点。