# Executing a search query with 30K+ clauses

**URL:** https://discuss.elastic.co/t/executing-a-search-query-with-30k-clauses/314441
**Category:** Elasticsearch
**Created:** [September 14, 2022, 4:56pm UTC](https://discuss.elastic.co/t/executing-a-search-query-with-30k-clauses/314441 "2022-09-14T16:56:02Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![tnitave](https://avatars.discourse-cdn.com/v4/letter/t/22d042/32.png) [@tnitave](https://discuss.elastic.co/u/tnitave)
#### Post date: [September 14, 2022, 4:56pm UTC](https://discuss.elastic.co/t/executing-a-search-query-with-30k-clauses/314441/1 "2022-09-14T16:56:02Z")

</div>

I am interested in executing an Elasticsearch search query with 5000+ terms across 5 different fields which leads to 25K+ clauses. According to [documentation](https://www.elastic.co/guide/en/elasticsearch/reference/6.7/search-settings.html) default `max_clause_count` limit is set to `1024` and updating this limit to 30K doesn't work as expected.

Can anyone please suggest an efficient alternative to execute this query?

Thanks.

Sample query:

```auto
"query":{
        "bool":{
            "should":[
                {
                    "match_phrase_prefix":{
                        "field_1": "term_1"
                    }
                }
                .
                .
                .
                {
                    "match_phrase_prefix":{
                        "field_1": "term_5000"
                    }
                },
                {
                    "match_phrase_prefix":{
                        "field_2": "term_1"
                    }
                }
                .
                .
                .
                {
                    "match_phrase_prefix":{
                        "field_2": "term_5000"
                    }
                }
                .
                .
                .
            ]
        }
    }

```

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [September 14, 2022, 5:22pm UTC](https://discuss.elastic.co/t/executing-a-search-query-with-30k-clauses/314441/2 "2022-09-14T17:22:29Z")

</div>

> [@tnitave](#):
>
> Can anyone please suggest an efficient alternative to execute this query?

Limits in Elasticsearch are generally there for a very good reason and queries with huge amounts of clauses tend to be slow, especially for large data sets. I therefore doubt there is any efficient way to make your query run as it is constructed.

You have not described the data and how the fields queried are mapped nor what the query actually does. Without knowing this it is hard for anyone to provide alternative approaches of achieving what you are looking for.

It would also be useful to know how large your data set is and how it is sharded together with what your query latency target/limit is.

---

<div class="post-metadata">

### Author: ![Mark\_Harwood1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood1/32/101255_2.png) [@Mark\_Harwood1](https://discuss.elastic.co/u/Mark_Harwood1)
#### Post date: [September 14, 2022, 8:29pm UTC](https://discuss.elastic.co/t/executing-a-search-query-with-30k-clauses/314441/3 "2022-09-14T20:29:38Z")

</div>

I benchmarked an alternative [approach](https://github.com/elastic/elasticsearch/issues/74954#issuecomment-876363500) for large numbers of terms that might be of interest.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [October 12, 2022, 8:29pm UTC](https://discuss.elastic.co/t/executing-a-search-query-with-30k-clauses/314441/4 "2022-10-12T20:29:40Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
