2017-09-27 55 views
0

我有一個elasticsearch查詢使用了大量的match子句(大約1300),因爲我有一個非常大的數據集。 ES拋出一個錯誤說這個:Elasticsearch通過重構bool match子句避免maxClauseCount

"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, 
} 

我做了一些研究,並在網上發現,這是不是一個好的做法,以增加maxClauseCountelastic中的一些提到我應該將我的查詢重寫爲terms查詢,而不是bool。這是我的查詢的一個例子。如何重寫它,以便我不擊中maxClauseCount

{ 
    "query": { 
    "bool": { 
     "must_not": [ 
     { 
      "match": { 
      "city": "dallas" 
      } 
     }, 
     { 
      "match": { 
      "city": "london" 
      } 
     }, 
     { 
      "match": { 
      "city": "singapore" 
      } 
     }, 
     { 
      "match": { 
      "city": "prague" 
      } 
     }, 
     { 
      "match": { 
      "city": "ontario" 
      } 
     }, 
     ........................................... 
     ........................................... 
     ........................................... 
     ] 
    } 
    } 
} 
+1

切換匹配查詢的字詞查詢只OK,如果你的搜索使用精確匹配,而不是分析比賽(詞語查詢是區分大小寫的,等等)。如果是這樣的話的確,這是減少子句數量的簡單方法。 – GPI

回答

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