gpt4 book ai didi

mysql - 按修改后的时间戳分组并从 Elasticsearch 返回最大值

转载 作者:行者123 更新时间:2023-11-29 06:35:10 25 4
gpt4 key购买 nike

我正在运行 Elasticsearch v1.2.2。

我有一组这样的文档

[
{ id: 1, source: { host: 'test.localhost', timestamp: 1407937236, loading_time: 2.841917 } },
{ id: 2, source: { host: 'test.localhost', timestamp: 1407937262, loading_time: 2.009191 } },
{ id: 3, source: { host: 'test.localhost', timestamp: 1407937322, loading_time: 2.084986 } },
{ id: 4, source: { host: 'test.localhost', timestamp: 1407937382, loading_time: 2.869245 } },
{ id: 5, source: { host: 'test.localhost', timestamp: 1407937442, loading_time: 2.559648 } },
...
]

(基本上所有分钟我都针对内部主机运行测试,这会返回加载时间。)

现在我想生成一个概览图:

  • 按 30 分钟分组的时间戳
  • 返回(该分组的)最大 loading_time
  • host 是特定的东西
  • 在特定的时间戳范围内
  • 按时间戳排序

由于我是 Elasticsearch 的新手,我什至不知道这一切是否可行。

在 MySQL 中这看起来像这样

SELECT (FLOOR(`timestamp` / 1800) * 1800) AS timestamp
MAX(`loading_time`) AS loading_time
FROM `elasticsearch_table`
GROUP BY (FLOOR(`timestamp` / 1800) * 1800)
WHERE `host` = 'test.localhost'
AND `timestamp` BETWEEN 1407937236 AND 1407937442
ORDER BY `timestamp` ASC

(不确定这个 MySQL 查询是否有效,但它应该让您了解我想要实现的目标。)

最佳答案

用于此类计算的功能称为 aggregations在 Elasticsearch 中。

下面是一些适合您对每个步骤的需要的聚合:

  • 按 30 分钟分组的时间戳 => date_histogram 30m间隔聚合
  • 返回(该分组的)最大 loading_time => max加载时间聚合。
  • host 是特定的地方 => filterterm filter 聚合在主机上。
  • 在特定时间戳范围之间 => 另一个具有 range 的过滤器聚合过滤器。
  • 按时间戳排序

棘手的部分是正确嵌套不同的聚合。您应该对小型数据集进行一些测试来检查这一点。

尝试这样的事情:

GET test/collection/_search?search_type=count
{
"aggs": {
"filter_by_host":{
"filter": {
"and": {
"filters": [
{"term": {"host": "test.localhost"}},
{"range": {"timestamp": {
"from": 1407937230000,
"to": 1407937400000
}}}
]
}
},
"aggs": {
"date": {
"date_histogram": {
"field": "timestamp",
"interval": "2m"
},
"aggs": {
"max_loading_time": {
"max" : {"field" : "loading_time" }}
}
}
}
}
}
}
}

间隔只有 2 分钟,选择范围边界只是为了从数据集中排除第五个文档,以查看它是否有效过滤。

唯一缺少的部分是排序:您不能对计数结果进行排序。

请求输出:

{
...
"aggregations": {
"filter_by_host": {
"doc_count": 4,
"date": {
"buckets": [
{
"key_as_string": "2014-08-13T13:40:00.000Z",
"key": 1407937200000,
"doc_count": 2,
"max_loading_time": {
"value": 2.841917
}
},
{
"key_as_string": "2014-08-13T13:42:00.000Z",
"key": 1407937320000,
"doc_count": 2,
"max_loading_time": {
"value": 2.869245
}
}
]
}
}
}
}

关于mysql - 按修改后的时间戳分组并从 Elasticsearch 返回最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25323378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com