[英]Optimize ES query with too many terms elements
我们正在处理一个数十亿记录的数据集,目前所有的数据都保存在 ElasticSearch 中,所有的查询和聚合都是用 ElasticSearch 进行的。
简化的查询体如下,我们把设备id放在terms
中,然后用should
拼接,避免1024个terms
的限制,terms元素的总数达到100,000,现在变得很慢。
{
"_source": {
"excludes": [
"raw_msg"
]
},
"query": {
"filter": {
"bool": {
"must": [
{
"range": {
"create_ms": {
"gte": 1664985600000,
"lte": 1665071999999
}
}
}
],
"should": [
{
"terms": {
"device_id": [
"1328871",
"1328899",
"1328898",
"1328934",
"1328919",
"1328976",
"1328977",
"1328879",
"1328910",
"1328902",
... # more values, since terms not support values more than 1024, wen concate all of them with should
]
}
},
{
"terms": {
"device_id": [
"1428871",
"1428899",
"1428898",
"1428934",
"1428919",
"1428976",
"1428977",
"1428879",
"1428910",
"1428902",
...
]
}
},
... # concate more terms until all of the 100,000 values are included
],
"minimum_should_match": 1
}
}
},
"aggs": {
"create_ms": {
"date_histogram": {
"field": "create_ms",
"interval": "hour",
}
}
},
"size": 0}
我的问题是有没有办法优化这个案例? 还是有更好的选择来进行这种搜索?
实时或接近实时是必须的,其他引擎也是可以接受的。
数据的简化模式:
"id" : {
"type" : "long"
},
"content" : {
"type" : "text"
},
"device_id" : {
"type" : "keyword"
},
"create_ms" : {
"type" : "date"
},
... # more field
您可以使用带有术语查找的术语查询来指定更大的值列表,如下所示
将您的 ID 存储在特定文档中,ID 如“device_ids”
"should": [
{
"terms": {
"device_id": {
"index": "your-index-name",
"id": "device_ids",
"path": "field-name"
}
}
}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.