title: Why does this query cause 'too many clauses'?
body: <p>I have a query with only a few 'shoulds' and 'filters', but one of the filters has a terms query with ~20,000 terms in it.  Our max_terms_count is 200k but this is complaining about 'clauses'.</p>
  <blockquote>
  <p>Caused by: org.elasticsearch.ElasticsearchException: Elasticsearch exception [type=too_many_clauses, reason=too_many_clauses: maxClauseCount is set to 1024]</p>
  </blockquote>
  <p>I've written queries containing terms queries with far more terms than this.  Why is this query causing a 'too many clauses' error?  How can I rewrite this query to get the same result without the error?</p>
  <pre><code>{
      &quot;query&quot; : {
        &quot;bool&quot; : {
          &quot;filter&quot; : [
            {
              &quot;nested&quot; : {
                &quot;query&quot; : {
                  &quot;range&quot; : {
                    &quot;dateField&quot; : {
                      &quot;from&quot; : &quot;2019-12-03T21:34:30.653Z&quot;,
                      &quot;to&quot; : &quot;2020-12-02T21:34:30.653Z&quot;,
                      &quot;include_lower&quot; : true,
                      &quot;include_upper&quot; : true,
                      &quot;boost&quot; : 1.0
                    }
                  }
                },
                &quot;path&quot; : &quot;observed_feeds&quot;,
                &quot;ignore_unmapped&quot; : false,
                &quot;score_mode&quot; : &quot;none&quot;,
                &quot;boost&quot; : 1.0
              }
            }
          ],
          &quot;should&quot; : [
            {
              &quot;bool&quot; : {
                &quot;filter&quot; : [
                  {
                    &quot;terms&quot; : {
                      &quot;ipAddressField&quot; : [
                        &quot;123.123.123.123&quot;,
                        &quot;124.124.124.124&quot;,
                        ... like 20,000 of these
                      ],
                      &quot;boost&quot; : 1.0
                    }
                  }
                ],
                &quot;adjust_pure_negative&quot; : true,
                &quot;boost&quot; : 1.0
              }
            }
          ],
          &quot;adjust_pure_negative&quot; : true,
          &quot;minimum_should_match&quot; : &quot;1&quot;,
          &quot;boost&quot; : 1.0
        }
      }
  }
  </code></pre>
  <p>Edit:  one note - The reason I'm wrapping the terms query in a should -&gt; bool is because there are times where we need to have multiple terms queries OR'd together.  This happened to not be one of them.</p>