### 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
qb = new WrapperQueryBuilder(
            new WrapperQueryBuilder(new WrapperQueryBuilder(new TermQueryBuilder(TEXT_FIELD_NAME, "bar").toString()).toString()).toString()
        );
        assertEquals(new TermQuery(new Term(TEXT_FIELD_NAME, "bar")), qb.rewrite(searchExecutionContext).toQuery(searchExecutionContext));
        qb = new WrapperQueryBuilder(new BoolQueryBuilder().toString());
        assertEquals(new MatchAllDocsQuery(), qb.rewrite(searchExecutionContext).toQuery(searchExecutionContext));
    }
    @Override
    protected Query rewrite(Query query) throws IOException {
        
        
        
        if (query != null) {
            MemoryIndex idx = new MemoryIndex();
            return idx.createSearcher().rewrite(query);
        }
        return new MatchAllDocsQuery(); 
    }
    @Override
    protected WrapperQueryBuilder createQueryWithInnerQuery(QueryBuilder queryBuilder) {
        return new WrapperQueryBuilder(Strings.toString(queryBuilder));
    }
    public void testMaxNestedDepth() throws IOException {
        BoolQueryBuilderTests boolQueryBuilderTests = new BoolQueryBuilderTests();
        BoolQueryBuilder boolQuery = boolQueryBuilderTests.createQueryWithInnerQuery(new MatchAllQueryBuilder());
        int maxDepth;
        if (frequently()) {
            maxDepth = randomIntBetween(3, 5);
            AbstractQueryBuilder.setMaxNestedDepth(maxDepth);
        } else {