【发布时间】:2020-08-27 17:28:27
【问题描述】:
我需要针对以下条件在 elasticsearch 上创建查询。
当添加“rabbitmq.queue.name”属性的最后一个元素等于“service_test_error”并且“rabbitmq.queue.messages.total.count”的值不为“0”时
下面的 sql 查询适用于我的搜索,但我无法使用 elasticsearch 执行相同的查询
select * from metric where rabbitmq.queue.messages.total.count != '0' and rabbitmq.queue.name = 'service_test_error' and timestamp = (select max(timestamp) from metric where rabbitmq.queue.name = 'service_test_error')
以下这些记录是我的 metric-xpto 索引中存在的示例
[
{
"_index": "metric-xpto",
"_type": "_doc",
"_id": "jYP1WnEBmYyEo7K68Zme",
"_version": 1,
"_score": null,
"_source": {
"@timestamp": "2020-04-08T18:03:14.899Z",
"rabbitmq": {
"queue": {
"name": "service_test_error",
"messages": {
"total": {
"count": 0
}
}
}
}
}
},
{
"_index": "metric-xpto",
"_type": "_doc",
"_id": "jYP1WnEBmYyEo7K68Zme",
"_version": 1,
"_score": null,
"_source": {
"@timestamp": "2020-04-07T18:03:14.899Z",
"rabbitmq": {
"queue": {
"name": "service_test_error",
"messages": {
"total": {
"count": 3
}
}
}
}
}
},
{
"_index": "metric-xpto",
"_type": "_doc",
"_id": "jYP1WnEBmYyEo7K68Zme",
"_version": 1,
"_score": null,
"_source": {
"@timestamp": "2020-04-03T17:03:14.899Z",
"rabbitmq": {
"queue": {
"name": "service_alpha_test_error",
"messages": {
"total": {
"count": 8
}
}
}
}
}
},
{
"_index": "metric-xpto",
"_type": "_doc",
"_id": "jYP1WnEBmYyEo7K68Zme",
"_version": 1,
"_score": null,
"_source": {
"@timestamp": "2020-04-03T18:03:14.899Z",
"rabbitmq": {
"queue": {
"name": "service_test_error",
"messages": {
"total": {
"count": 8
}
}
}
}
}
}
]
如何使用 elasticsearch 创建类似的查询?
【问题讨论】:
-
这不能在单个查询中完成。您首先需要获取最大时间戳值,然后使用该时间戳值过滤文档网
-
您好,此查询用于通过 elasticserach 插件触发警报。因此,我需要对 elasticsearch 执行完整的查询。不幸的是,我无法在编程语言上制作第二个过滤器
-
您的目标是获得前 1 个文档,其中 rabbitmq.queue.name = 'service_test_error' 按时间戳排序,或者时间戳与 'service_test_error' 的最大时间戳相同且 queue.name 可以是任何文档?
-
我的目标是使用 rabbitmq.queue.name = 'service_test_error' 插入最新的记录(无论是按时间戳 desc 排序还是获取最大时间戳),但我需要检查这是否匹配文档包含 rabbitmq.queue.messages.total.count != '0'。鉴于这些条件,查询的最终结果需要告诉我是否有返回。
标签: database elasticsearch kibana elastic-stack elasticsearch-dsl