![](/img/trans.png)
[英]Elasticsearch : How to group documents by field when field is an array
[英]Elasticsearch query array field across documents
我想從elasticsearch查詢數組字段。 我有一個數組字段,其中包含分配給作業的一個或多個gpu的節點號。 假設某些人可能與其他人共享同一gpu節點,那么不同的人可能會同時使用同一節點。 我想獲取在特定時間使用的不同節點的總數。
假設我有三行數據,它們屬於同一時間間隔。 我想繪制一個直方圖,顯示該時期內有三個節點被占用。 我可以在Kibana上實現嗎?
范例:
[3]
[3,4,5]
[4,5]
我期望輸出為3,因為僅使用了3個不同的節點。
提前致謝
您可以將日期直方圖聚合與術語聚合(如果節點的確切數目很重要)或基數聚合(如果您可以接受較高基數的某些誤差)組合使用,來實現此目的。
完整示例:
# 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
,則會看到直方圖本身具有更多數據點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.