首页 > 解决方案 > Elasticsearch 避免聚合中的 maxClauseCount 错误

问题描述

我有一个索引,其中包含 Elasticsearch 5.X 中的以下文档。
它将文档文件的一行字符串作为单个记录保存。

{"file_id":"file0001", "row_id":"1", "text":"(text field to search...)"}
{"file_id":"file0001", "row_id":"2", "text":"(text field to search...)"}
{"file_id":"file0001", "row_id":"3", "text":"(text field to search...)"}
{"file_id":"file0002", "row_id":"1", "text":"(text field to search...)"}
{"file_id":"file0002", "row_id":"2", "text":"(text field to search...)"}
...Millions of documents...

并发送以下查询以提取每个文件的前 500 个命中行。

{
   "_source":[
      "file_id",
      "text"
   ],
   "size":0,
   "query":{
      "filtered":{
         "query"{
            "must":{
               "regexp":{
                  "text":".*[o2].*"
               }
            }
         },
         "filter":{
            "terms":{
               "file_id":[
                  (Thousands of file_ids...)
               ]
            }
         }
      }
   },
   "aggs":{
      "top-docs":{
         "terms":{
            "field":"file_id",
            "size":5000
         },
         "aggs":{
            "top_file_hits":{
               "top_hits":{
                  "size":500,
                  "highlight":{
                     "pre_tags":["<em>"],
                     "post_tags":["</em>"],
                     "fields":{
                        "text":{}
                     }
                  }
               }
            }
         }
      }
   }
}

然后返回以下错误。

{
  "error" : {
    "root_cause" : [
      {
        "type" : "too_many_clauses",
        "reason" : "maxClauseCount is set to 1024"

我认为 aggs 过程很重,但我想不出不使用它的方法。有任何想法吗?

标签: elasticsearch

解决方案


推荐阅读