跨文档的Elasticsearch查询数组字段

问题描述 投票:0回答:1

我想从elasticsearch查询数组字段。我有一个数组字段,其中包含分配给作业的一个或多个gpu节点号。考虑到某些人可能与其他人共享相同的gpu节点,不同的人可能同时使用同一节点。我想获得在特定时间使用的不同节点的总数。

假设我有三行数据属于同一时间间隔。我想绘制一个直方图,显示该时段内有三个节点占用。我能在Kibana上实现吗?

示例:

[3]

[3,4,5]

[4,5]

我期望输出为3,因为只使用了3个不同的节点。

提前致谢

arrays elasticsearch kibana
1个回答
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
          }
        }
      }
    }
  }
}

笔记:

  1. 术语与基数聚合:使用基数聚合,除非您需要知道正在使用的WHICH节点。它具有更高的可扩展性,直到你达到1000的基数,你可能不会看到任何不准确。
  2. 日期直方图间隔:您可以使用间隔进行播放,使其对您有意义。如果您运行上面的示例,您将只看到一个直方图桶,但是如果您将hour更改为minute,您将看到直方图构建本身具有更多数据点。
© www.soinside.com 2019 - 2024. All rights reserved.