Elasticsearch 通过重构 bool 匹配子句来避免 maxClauseCount

Elasticsearch avoid maxClauseCount by refactoring bool match clauses

我有一个 elasticsearch 查询,它使用了很多 match 子句(大约 1300 个),因为我有一个非常大的数据集。 ES 抛出这样的错误:

"error": {
    "root_cause": [
      {
        "type": "too_many_clauses",
        "reason": "too_many_clauses: maxClauseCount is set to 1024"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
}

我在网上做了一些研究,发现增加 maxClauseCount 并不是一个好的做法。 elastic 中的一些人提到我应该将我的查询重写为 terms 查询而不是 bool。这是我的查询示例。我该如何重写它才能不点击 maxClauseCount?

{
  "query": {
    "bool": {
      "must_not": [
        {
          "match": {
            "city": "dallas"
          }
        },
        {
          "match": {
            "city": "london"
          }
        },
        {
          "match": {
            "city": "singapore"
          }
        },
        {
          "match": {
            "city": "prague"
          }
        },
        {
          "match": {
            "city": "ontario"
          }
        },
        ...........................................
        ...........................................
        ...........................................
      ]
     }
    }
}
POST test/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "terms": {
            "city": [
              "prague",
              "london",
              "chicago",
              "singapore",
              "new york",
              "san francisco",
              "mexico city",
              "baghdad"
            ]
          }
        }
      ]
    }
  }
}