# Terms search gives error "failed to create query: maxClauseCount is set to 1024"

**URL:** <https://forum.opensearch.org/t/terms-search-gives-error-failed-to-create-query-maxclausecount-is-set-to-1024/21729>\
**Category:** OpenSearch\
**Created:** [October 1, 2024, 7:47pm UTC](https://forum.opensearch.org/t/terms-search-gives-error-failed-to-create-query-maxclausecount-is-set-to-1024/21729 "2024-10-01T19:47:43Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ss\_open](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@ss\_open](https://forum.opensearch.org/u/ss_open)\
**Post date:** [October 1, 2024, 7:47pm UTC](https://forum.opensearch.org/t/terms-search-gives-error-failed-to-create-query-maxclausecount-is-set-to-1024/21729/1 "2024-10-01T19:47:44Z")

</div>

**Versions** (relevant - OpenSearch/Dashboard/Server OS/Browser): 2.x

**Describe the issue** : I am using terms search with index and document id. My query looks something like this:

```auto
{
  "query": {
    "terms": {
      "ip": {
        "index": "address_index",
        "id": "b2e29f44-ef98-4426-9bfb-34881a839cdc",
        "path": "ip_values"
      }
    }
  }
}

```

I have more than 10K values for this document in my address\_index.

Note: Query works If I use another document ID where I have less than 1024 values.

**Configuration** :

**Relevant Logs or Screenshots** :

```
    "reason": {
      "type": "query_shard_exception",
      "reason": "failed to create query: maxClauseCount is set to 1024",
      "index": "address_index",
      "index_uuid": "4oi75d-kk45pgpEPvA",
      "caused_by": {
        "type": "too_many_clauses",
        "reason": "maxClauseCount is set to 1024"
      }
    }

```

---

<div class="post-metadata">

**Author:** ![yeonghyeonKo](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.opensearch.org/yeonghyeonko/32/8202_2.png) [@yeonghyeonKo](https://forum.opensearch.org/u/yeonghyeonKo)\
**Post date:** [October 1, 2024, 10:49pm UTC](https://forum.opensearch.org/t/terms-search-gives-error-failed-to-create-query-maxclausecount-is-set-to-1024/21729/2 "2024-10-01T22:49:50Z")

</div>

Can you run **\_analyze** API if it’s possible to modify `indices.query.bool.max_clause_count` from the default(1024) to 2048 or above?

You must be careful that this setting will have an impact on memory usage in clusters so it’s just for checking how many clauses are created by the query you ran. After you test, now’s the time to determine the adequate number for **max\_clause\_count**.

As you know, OpenSearch’s **\_search** queries are always rewritten to Lucene’s BooleanQuery internally.

---

<div class="post-metadata">

**Author:** ![ss\_open](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@ss\_open](https://forum.opensearch.org/u/ss_open)\
**Post date:** [October 1, 2024, 10:57pm UTC](https://forum.opensearch.org/t/terms-search-gives-error-failed-to-create-query-maxclausecount-is-set-to-1024/21729/3 "2024-10-01T22:57:58Z")

</div>

Thanks @yeonghyeonKo

I am migrating from **elasticSearch 7.10** and this same query works there without changing default value of **indices.query.bool.max\_clause\_count**. I am really not sure to modify **max\_clause\_count** because my document could have any number of values. I am trying to understand why am I getting this error with openSearch?

---

<div class="post-metadata">

**Author:** ![mkhl](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@mkhl](https://forum.opensearch.org/u/mkhl)\
**Post date:** [October 3, 2024, 12:51pm UTC](https://forum.opensearch.org/t/terms-search-gives-error-failed-to-create-query-maxclausecount-is-set-to-1024/21729/4 "2024-10-03T12:51:10Z")

</div>

stacktrace would be very helpful to confirm the reason. For awhile, I remember that some of the “multiterm queries” support rewrite property. Could you try to pass `"rewrite":"constant_score"` ?  
_UPD_ pardon, there are no rewrite field in termsquery [OpenSearch/server/src/main/java/org/opensearch/index/query/TermsQueryBuilder.java at main · opensearch-project/OpenSearch · GitHub](https://github.com/opensearch-project/OpenSearch/blob/main/server/src/main/java/org/opensearch/index/query/TermsQueryBuilder.java)

---

<div class="post-metadata">

**Author:** ![ss\_open](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@ss\_open](https://forum.opensearch.org/u/ss_open)\
**Post date:** [October 4, 2024, 6:17pm UTC](https://forum.opensearch.org/t/terms-search-gives-error-failed-to-create-query-maxclausecount-is-set-to-1024/21729/5 "2024-10-04T18:17:34Z")

</div>

Thanks @mkhl

The only stacktrace I am getting from opensearch

````auto
  "took": 50,
  "timed_out": false,
  "_shards": {
    "total": 63,
    "successful": 56,
    "skipped": 0,
    "failed": 7,
    "failures": [
      {
        "shard": 0,
        "index": "address_index",
        "node": "EhLUz-OFQEDSEQK1-kjnfg",
        "reason": {
          "type": "query_shard_exception",
          "reason": "failed to create query: maxClauseCount is set to 1024",
          "index": "address_index",
          "index_uuid": "4oi73eMuSX-qw23pgpEPvA",
          "caused_by": {
            "type": "too_many_clauses",
            "reason": "too_many_clauses: maxClauseCount is set to 1024"
          }
        }
      }```
````

---

<div class="post-metadata">

**Author:** ![mkhl](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@mkhl](https://forum.opensearch.org/u/mkhl)\
**Post date:** [October 4, 2024, 6:55pm UTC](https://forum.opensearch.org/t/terms-search-gives-error-failed-to-create-query-maxclausecount-is-set-to-1024/21729/6 "2024-10-04T18:55:19Z")

</div>

[Common REST Parameters - OpenSearch Documentation](https://opensearch.org/docs/latest/api-reference/common-parameters/#stack-traces) here’s how you can get a stacktrace. It looks like a feature request I want to tackle.

---

<div class="post-metadata">

**Author:** ![ss\_open](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@ss\_open](https://forum.opensearch.org/u/ss_open)\
**Post date:** [October 4, 2024, 7:50pm UTC](https://forum.opensearch.org/t/terms-search-gives-error-failed-to-create-query-maxclausecount-is-set-to-1024/21729/7 "2024-10-04T19:50:41Z")

</div>

Thanks @mkhl

I got the stack trace now.

````auto
        "type": "too_many_clauses",
        "reason": "too_many_clauses: maxClauseCount is set to 1024",
        "stack_trace": "NotSerializableExceptionWrapper[too_many_clauses: maxClauseCount is set to 1024]\n\tat org.apache.lucene.search.BooleanQuery$Builder.add(BooleanQuery.java:115)\n\tat org.apache.lucene.search.BooleanQuery$Builder.add(BooleanQuery.java:129)\n\tat org.opensearch.index.mapper.MappedFieldType.termsQuery(MappedFieldType.java:237)\n\tat org.opensearch.index.mapper.IpFieldMapper$IpFieldType.termsQuery(IpFieldMapper.java:257)\n\tat org.opensearch.index.query.TermsQueryBuilder.doToQuery(TermsQueryBuilder.java:476)\n\tat org.opensearch.index.query.AbstractQueryBuilder.toQuery(AbstractQueryBuilder.java:117)\n\tat org.opensearch.index.query.BoolQueryBuilder.addBooleanClauses(BoolQueryBuilder.java:346)\n\tat org.opensearch.index.query.BoolQueryBuilder.doToQuery(BoolQueryBuilder.java:328)\n\tat org.opensearch.index.query.AbstractQueryBuilder.toQuery(AbstractQueryBuilder.java:117)\n\tat org.opensearch.index.query.BoolQueryBuilder.addBooleanClauses(BoolQueryBuilder.java:346)\n\tat org.opensearch.index.query.BoolQueryBuilder.doToQuery(BoolQueryBuilder.java:326)\n\tat org.opensearch.index.query.AbstractQueryBuilder.toQuery(AbstractQueryBuilder.java:117)\n\tat org.opensearch.index.query.BoolQueryBuilder.addBooleanClauses(BoolQueryBuilder.java:346)\n\tat org.opensearch.index.query.BoolQueryBuilder.doToQuery(BoolQueryBuilder.java:329)\n\tat org.opensearch.index.query.AbstractQueryBuilder.toQuery(AbstractQueryBuilder.java:117)\n\tat org.opensearch.index.query.QueryShardContext.lambda$toQuery$3(QueryShardContext.java:467)\n\tat org.opensearch.index.query.QueryShardContext.toQuery(QueryShardContext.java:479)\n\tat org.opensearch.index.query.QueryShardContext.toQuery(QueryShardContext.java:466)\n\tat org.opensearch.search.SearchService.parseSource(SearchService.java:1238)\n\tat org.opensearch.search.SearchService.createContext(SearchService.java:985)\n\tat org.opensearch.search.SearchService.executeQueryPhase(SearchService.java:593)\n\tat org.opensearch.search.SearchService$2.lambda$onResponse$0(SearchService.java:566)\n\tat org.opensearch.action.ActionRunnable.lambda$supply$0(ActionRunnable.java:74)\n\tat org.opensearch.action.ActionRunnable$2.doRun(ActionRunnable.java:89)\n\tat org.opensearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:52)\n\tat org.opensearch.threadpool.TaskAwareRunnable.doRun(TaskAwareRunnable.java:78)\n\tat org.opensearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:52)\n\tat org.opensearch.common.util.concurrent.TimedRunnable.doRun(TimedRunnable.java:59)\n\tat org.opensearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:908)\n\tat org.opensearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:52)\n\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)\n\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)\n\tat java.lang.Thread.run(Thread.java:833)\n"
      }```
````

---

<div class="post-metadata">

**Author:** ![mkhl](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@mkhl](https://forum.opensearch.org/u/mkhl)\
**Post date:** [October 4, 2024, 9:04pm UTC](https://forum.opensearch.org/t/terms-search-gives-error-failed-to-create-query-maxclausecount-is-set-to-1024/21729/8 "2024-10-04T21:04:50Z")

</div>

Posting it in more readable view. Wondering why TermsQuery wasn’t rewritten to bitset. It’s worth an issue for sure.

````auto
"type": "too_many_clauses",
        "reason": "too_many_clauses: maxClauseCount is set to 1024",
        "stack_trace": "NotSerializableExceptionWrapper[too_many_clauses: maxClauseCount is set to 1024]
	at org.apache.lucene.search.BooleanQuery$Builder.add(BooleanQuery.java:115)
	at org.apache.lucene.search.BooleanQuery$Builder.add(BooleanQuery.java:129)
	at org.opensearch.index.mapper.MappedFieldType.termsQuery(MappedFieldType.java:237)
	at org.opensearch.index.mapper.IpFieldMapper$IpFieldType.termsQuery(IpFieldMapper.java:257)
	at org.opensearch.index.query.TermsQueryBuilder.doToQuery(TermsQueryBuilder.java:476)
	at org.opensearch.index.query.AbstractQueryBuilder.toQuery(AbstractQueryBuilder.java:117)
	at org.opensearch.index.query.BoolQueryBuilder.addBooleanClauses(BoolQueryBuilder.java:346)
	at org.opensearch.index.query.BoolQueryBuilder.doToQuery(BoolQueryBuilder.java:328)
	at org.opensearch.index.query.AbstractQueryBuilder.toQuery(AbstractQueryBuilder.java:117)
	at org.opensearch.index.query.BoolQueryBuilder.addBooleanClauses(BoolQueryBuilder.java:346)
	at org.opensearch.index.query.BoolQueryBuilder.doToQuery(BoolQueryBuilder.java:326)
	at org.opensearch.index.query.AbstractQueryBuilder.toQuery(AbstractQueryBuilder.java:117)
	at org.opensearch.index.query.BoolQueryBuilder.addBooleanClauses(BoolQueryBuilder.java:346)
	at org.opensearch.index.query.BoolQueryBuilder.doToQuery(BoolQueryBuilder.java:329)
	at org.opensearch.index.query.AbstractQueryBuilder.toQuery(AbstractQueryBuilder.java:117)
	at org.opensearch.index.query.QueryShardContext.lambda$toQuery$3(QueryShardContext.java:467)
	at org.opensearch.index.query.QueryShardContext.toQuery(QueryShardContext.java:479)
	at org.opensearch.index.query.QueryShardContext.toQuery(QueryShardContext.java:466)
	at org.opensearch.search.SearchService.parseSource(SearchService.java:1238)
	at org.opensearch.search.SearchService.createContext(SearchService.java:985)
	at org.opensearch.search.SearchService.executeQueryPhase(SearchService.java:593)
	at org.opensearch.search.SearchService$2.lambda$onResponse$0(SearchService.java:566)
	at org.opensearch.action.ActionRunnable.lambda$supply$0(ActionRunnable.java:74)
	at org.opensearch.action.ActionRunnable$2.doRun(ActionRunnable.java:89)
	at org.opensearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:52)
	at org.opensearch.threadpool.TaskAwareRunnable.doRun(TaskAwareRunnable.java:78)
	at org.opensearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:52)
	at org.opensearch.common.util.concurrent.TimedRunnable.doRun(TimedRunnable.java:59)
	at org.opensearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:908)
	at org.opensearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:52)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.lang.Thread.run(Thread.java:833)
"
      }```
````

---

<div class="post-metadata">

**Author:** ![mkhl](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@mkhl](https://forum.opensearch.org/u/mkhl)\
**Post date:** [October 4, 2024, 10:32pm UTC](https://forum.opensearch.org/t/terms-search-gives-error-failed-to-create-query-maxclausecount-is-set-to-1024/21729/10 "2024-10-04T22:32:19Z")

</div>

here [IpFieldMapper](https://github.com/opensearch-project/OpenSearch/blob/421a1cc2270ef02f02fc7621895f8b15ec7282f1/server/src/main/java/org/opensearch/index/mapper/IpFieldMapper.java#L279) it falls back to [MappedFieldType](https://github.com/opensearch-project/OpenSearch/blob/421a1cc2270ef02f02fc7621895f8b15ec7282f1/server/src/main/java/org/opensearch/index/mapper/MappedFieldType.java#L236) which is prone to maxClauses error. Presumably if you avoid slashes in those IPs it should work.

**UPD** Do you have an idea how many concrete IPs and mask/s you have in those lists?  
What can work is if you have a separate fields for concrete IPs and mask/s - in this case concrete IPs isn’t limited by max\_caluses, but mask/s still encounter this limitation. Then you can bool/should two term lookups. It’s not ideal workaround but this may work.

raised [[Feature Request] relax max Clauses Count limitation of termS query over IP field · Issue #16200 · opensearch-project/OpenSearch · GitHub](https://github.com/opensearch-project/OpenSearch/issues/16200)

---

<div class="post-metadata">

**Author:** ![mkhl](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@mkhl](https://forum.opensearch.org/u/mkhl)\
**Post date:** [November 28, 2024, 6:30am UTC](https://forum.opensearch.org/t/terms-search-gives-error-failed-to-create-query-maxclausecount-is-set-to-1024/21729/11 "2024-11-28T06:30:55Z")

</div>

fyi, there will be no this limit for number of masks for ip for indexed field ip starting from 2.19.

> <https://github.com/opensearch-project/OpenSearch/pull/16391>
>
> \### Now
> 
> \- \`terms\_query\` over IP with mask \`/\` values parsed as \`BooleanQuery\`….
> \- \`BooleanQuery\` is prone to max clauses limit
> \- as a result 1025 masks \[fails with too many clauses limit\](https://forum.opensearch.org/t/terms-search-gives-error-failed-to-create-query-maxclausecount-is-set-to-1024/21729/8) 
> 
> \### Proposal
> 
> \- for indexed field combine masks with MultiRangePointQuery 
> \- for doc\_values only field still fall back to Boolean Query 
> 
> \### Related Issues
> Resolves #16200
> 
> 
> \### Check List
> \- \[x\] Functionality includes testing.
> \- \[\] API changes companion pull request \[created\](https://github.com/opensearch-project/opensearch-api-specification/blob/main/DEVELOPER\_GUIDE.md), if applicable.
> \- \[\] Public documentation issue/PR \[created\](https://github.com/opensearch-project/documentation-website/issues/new/choose), if applicable.
> 
> By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
> For more information on following Developer Certificate of Origin and signing off your commits, please check \[here\](https://github.com/opensearch-project/OpenSearch/blob/main/CONTRIBUTING.md#developer-certificate-of-origin).

But it still remains for dv only field.

---

<div class="post-metadata">

**Author:** ![mkhl](https://avatars.discourse-cdn.com/v4/letter/m/e68b1a/32.png) [@mkhl](https://forum.opensearch.org/u/mkhl)\
**Post date:** [June 10, 2025, 9:12pm UTC](https://forum.opensearch.org/t/terms-search-gives-error-failed-to-create-query-maxclausecount-is-set-to-1024/21729/12 "2025-06-10T21:12:56Z")

</div>

Starting from 3.1 there should no limit for ip/masks in terms\_query even over dv\_only field.

> <https://github.com/opensearch-project/OpenSearch/pull/18357>
>
> \### Description
> Avoid clauses limit what searching terms\_query for IP/masks ove…r docValues fields.
> 
> \### Related Issues
> Resolves #17394
> 
> 
> \### Check List
> \- \[x\] Functionality includes testing.
> 
> By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
> For more information on following Developer Certificate of Origin and signing off your commits, please check \[here\](https://github.com/opensearch-project/OpenSearch/blob/main/CONTRIBUTING.md#developer-certificate-of-origin).
