gpt4 book ai didi

database - 如何使用Elasticsearch创建子查询

转载 作者:行者123 更新时间:2023-12-03 01:17:40 24 4
gpt4 key购买 nike

我需要在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创建类似的查询?

最佳答案

在查询部分,您只能获取最上面的记录,然后检查是否必须在客户端进行邮件计数。

在聚合部分可以做到这一点。

{
"size": 0,
"query": {
"term": {
"rabbitmq.queue.name.keyword": {
"value": "service_test_error"
}
}
},
"aggs": {
"date": {
"terms": {
"field": "@timestamp",
"size": 1,
"order": {
"_term": "desc"
}
},
"aggs": {
"message_count": {
"terms": {
"field": "rabbitmq.queue.messages.total.count",
"size": 10
},
"aggs": {
"filter_count": {
"bucket_selector": {
"buckets_path": {
"key": "_key"
},
"script": "if(params.key>0) return true; else return false;"
}
}
}
},
"select_timestamp": {
"bucket_selector": {
"buckets_path": {
"key": "message_count._bucket_count"
},
"script": "if(params.key>0) return true;else false;"
}
}
}
}
}
}


结果:如果最上面的记录消息计数为零,则存储桶将为空,否则将有数据
 "hits" : {
"total" : {
"value" : 7,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"date" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 6,
"buckets" : [ ]
}
}

flex 搜索没有联接,因此在查询中无法将一个文档与另一个文档进行比较

关于database - 如何使用Elasticsearch创建子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61742137/

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