### CI Link

https://gradle-enterprise.elastic.co/s/sdobpnvk6mf6w

### Repro line

gradlew ':server:test' --tests "org.elasticsearch.index.query.WrapperQueryBuilderTests.testMaxNestedDepth" -Dtests.seed=68EFB0E52A29A3C7 -Dtests.locale=ar-TN -Dtests.timezone=America/Guadeloupe -Druntime.java=19

### Does it reproduce?

Yes

### Applicable branches

main

### Failure history

_No response_

### Failure excerpt

```
 2> REPRODUCE WITH: ./gradlew ':server:test' --tests "org.elasticsearch.index.query.WrapperQueryBuilderTests.testMaxNestedDepth" -Dtests.seed=68EFB0E52A29A3C7 -Dtests.locale=ar-TN -Dtests.timezone=America/Guadeloupe -Druntime.java=19
  2> java.lang.OutOfMemoryError: Java heap space
        at __randomizedtesting.SeedInfo.seed([68EFB0E52A29A3C7:D01872CB3ED46A68]:0)
        at java.base/java.util.Arrays.copyOf(Arrays.java:3537)
        at java.base/java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:100)
        at java.base/java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:132)
        at com.fasterxml.jackson.core.json.UTF8JsonGenerator._flushBuffer(UTF8JsonGenerator.java:2171)
        at com.fasterxml.jackson.core.json.UTF8JsonGenerator.writeFieldName(UTF8JsonGenerator.java:244)
        at org.elasticsearch.xcontent.provider.json.JsonXContentGenerator.writeFieldName(JsonXContentGenerator.java:187)
        at org.elasticsearch.xcontent.XContentBuilder.field(XContentBuilder.java:363)
        at org.elasticsearch.xcontent.XContentBuilder.startObject(XContentBuilder.java:333)
        at org.elasticsearch.index.query.MatchAllQueryBuilder.doXContent(MatchAllQueryBuilder.java:45)
        at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:81)
        at org.elasticsearch.index.query.BoolQueryBuilder.doXArrayContent(BoolQueryBuilder.java:258)
        at org.elasticsearch.index.query.BoolQueryBuilder.doXContent(BoolQueryBuilder.java:236)
        at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:81)
        at org.elasticsearch.index.query.BoolQueryBuilder.doXArrayContent(BoolQueryBuilder.java:258)
        at org.elasticsearch.index.query.BoolQueryBuilder.doXContent(BoolQueryBuilder.java:235)
        at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:81)
        at org.elasticsearch.index.query.BoolQueryBuilder.doXArrayContent(BoolQueryBuilder.java:258)
        at org.elasticsearch.index.query.BoolQueryBuilder.doXContent(BoolQueryBuilder.java:236)
        at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:81)
        at org.elasticsearch.index.query.BoolQueryBuilder.doXArrayContent(BoolQueryBuilder.java:258)
        at org.elasticsearch.index.query.BoolQueryBuilder.doXContent(BoolQueryBuilder.java:235)
        at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:81)
        at org.elasticsearch.index.query.BoolQueryBuilder.doXArrayContent(BoolQueryBuilder.java:258)
        at org.elasticsearch.index.query.BoolQueryBuilder.doXContent(BoolQueryBuilder.java:235)
        at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:81)
        at org.elasticsearch.index.query.BoolQueryBuilder.doXArrayContent(BoolQueryBuilder.java:258)
        at org.elasticsearch.index.query.BoolQueryBuilder.doXContent(BoolQueryBuilder.java:235)
        at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:81)
        at org.elasticsearch.index.query.BoolQueryBuilder.doXArrayContent(BoolQueryBuilder.java:258)
        at org.elasticsearch.index.query.BoolQueryBuilder.doXContent(BoolQueryBuilder.java:236)
        at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:81)
        at org.elasticsearch.index.query.BoolQueryBuilder.doXArrayContent(BoolQueryBuilder.java:258)
        ```
server/src/test/java/org/elasticsearch/index/query/WrapperQueryBuilderTests.java
maxDepth = INDICES_MAX_NESTED_DEPTH_SETTING.getDefault(Settings.EMPTY);
        }
        for (int i = 1; i < maxDepth - 1; i++) {
            boolQuery = boolQueryBuilderTests.createQueryWithInnerQuery(boolQuery);
        }
        WrapperQueryBuilder query = new WrapperQueryBuilder(Strings.toString(boolQuery));
        AbstractQueryBuilder.setMaxNestedDepth(maxDepth);
        try {
            
            query.rewrite(createSearchExecutionContext());
            
            WrapperQueryBuilder q = new WrapperQueryBuilder(Strings.toString(boolQueryBuilderTests.createQueryWithInnerQuery(boolQuery)));
            IllegalArgumentException e = expectThrows(XContentParseException.class, () -> q.rewrite(createSearchExecutionContext()));
            
            while (e.getCause() != null) {
                assertThat(e.getCause(), Matchers.instanceOf(IllegalArgumentException.class));
                e = (IllegalArgumentException) e.getCause();
            }
            assertEquals(
                "The nested depth of the query exceeds the maximum nested depth for queries set in ["
                    + INDICES_MAX_NESTED_DEPTH_SETTING.getKey()
                    + "]",
                e.getMessage()
            );
        } finally {
            AbstractQueryBuilder.setMaxNestedDepth(INDICES_MAX_NESTED_DEPTH_SETTING.getDefault(Settings.EMPTY));
        }
    }
}