[英]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.