Elasticsearch跨文档查询数组字段
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
}
}
}
}
}
}
备注:
- 术语与基数聚合: 除非您需要知道正在使用哪些节点,否则请使用基数聚合。它的可扩展性明显更高,并且在您进入 1000 的基数之前,您可能不会发现任何不准确之处。
- 日期直方图间隔: 您可以调整间隔,使其对您有意义。如果您 运行 通过上面的示例,您将只会看到一个直方图桶,但是如果您将
hour
更改为 minute
,您将看到直方图自身构建了更多数据点.
我想从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
}
}
}
}
}
}
备注:
- 术语与基数聚合: 除非您需要知道正在使用哪些节点,否则请使用基数聚合。它的可扩展性明显更高,并且在您进入 1000 的基数之前,您可能不会发现任何不准确之处。
- 日期直方图间隔: 您可以调整间隔,使其对您有意义。如果您 运行 通过上面的示例,您将只会看到一个直方图桶,但是如果您将
hour
更改为minute
,您将看到直方图自身构建了更多数据点.