### 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
assertEquals("this query must be rewritten first", e.getMessage());
        QueryBuilder rewrite = qb.rewrite(createSearchExecutionContext());
        assertEquals(tqb, rewrite);
    }
    public void testRewriteWithInnerName() throws IOException {
        QueryBuilder builder = new WrapperQueryBuilder("""
            { "match_all" : {"_name" : "foobar"}}""");
        SearchExecutionContext searchExecutionContext = createSearchExecutionContext();
        assertEquals(new MatchAllQueryBuilder().queryName("foobar"), builder.rewrite(searchExecutionContext));
        builder = new WrapperQueryBuilder("""
            { "match_all" : {"_name" : "foobar"}}""").queryName("outer");
        assertEquals(
            new BoolQueryBuilder().must(new MatchAllQueryBuilder().queryName("foobar")).queryName("outer"),
            builder.rewrite(searchExecutionContext)
        );
    }
    public void testRewriteWithInnerBoost() throws IOException {
        final TermQueryBuilder query = new TermQueryBuilder(TEXT_FIELD_NAME, "bar").boost(2);
        QueryBuilder builder = new WrapperQueryBuilder(query.toString());
        SearchExecutionContext searchExecutionContext = createSearchExecutionContext();
        assertEquals(query, builder.rewrite(searchExecutionContext));
        builder = new WrapperQueryBuilder(query.toString()).boost(3);
        assertEquals(new BoolQueryBuilder().must(query).boost(3), builder.rewrite(searchExecutionContext));
    }
    public void testRewriteInnerQueryToo() throws IOException {
        SearchExecutionContext searchExecutionContext = createSearchExecutionContext();
        QueryBuilder qb = new WrapperQueryBuilder(
            new WrapperQueryBuilder(new TermQueryBuilder(TEXT_FIELD_NAME, "bar").toString()).toString()
        );
        assertEquals(new TermQuery(new Term(TEXT_FIELD_NAME, "bar")), qb.rewrite(searchExecutionContext).toQuery(searchExecutionContext));