### CI Link

https://gradle-enterprise.elastic.co/s/b57j3vuz7qk2q/tests/:server:test/org.elasticsearch.index.query.SpanContainingQueryBuilderTests/testMaxNestedDepth

### Repro line

`gradlew ':server:test' --tests "org.elasticsearch.index.query.SpanContainingQueryBuilderTests.testMaxNestedDepth" -Dtests.seed=FCAC1620A8E66666 -Dtests.locale=de-AT -Dtests.timezone=America/Shiprock -Druntime.java=19`

### Does it reproduce?

Yes

### Applicable branches

main

### Failure history

https://gradle-enterprise.elastic.co/scans/tests?search.timeZoneId=Europe/Berlin&tests.container=org.elasticsearch.index.query.SpanContainingQueryBuilderTests&tests.test=testMaxNestedDepth#

### Failure excerpt

```
java.lang.OutOfMemoryError: Java heap space	
	at __randomizedtesting.SeedInfo.seed([FCAC1620A8E66666:445BD40EBC1BAFC9]:0)	
		•••
	at org.elasticsearch.index.query.SpanContainingQueryBuilder.doXContent(SpanContainingQueryBuilder.java:88)	
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:80)	
	at org.elasticsearch.index.query.SpanContainingQueryBuilder.doXContent(SpanContainingQueryBuilder.java:87)	
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:80)	
	at org.elasticsearch.index.query.SpanContainingQueryBuilder.doXContent(SpanContainingQueryBuilder.java:89)	
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:80)	
	at org.elasticsearch.index.query.SpanContainingQueryBuilder.doXContent(SpanContainingQueryBuilder.java:89)	
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:80)	
	at org.elasticsearch.index.query.SpanContainingQueryBuilder.doXContent(SpanContainingQueryBuilder.java:89)	
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:80)	
	at org.elasticsearch.index.query.SpanContainingQueryBuilder.doXContent(SpanContainingQueryBuilder.java:89)	
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:80)	
	at org.elasticsearch.index.query.SpanContainingQueryBuilder.doXContent(SpanContainingQueryBuilder.java:89)	
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:80)	
	at org.elasticsearch.index.query.SpanContainingQueryBuilder.doXContent(SpanContainingQueryBuilder.java:87)	
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:80)	
	at org.elasticsearch.index.query.SpanContainingQueryBuilder.doXContent(SpanContainingQueryBuilder.java:89)	
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:80)	
	at org.elasticsearch.index.query.SpanContainingQueryBuilder.doXContent(SpanContainingQueryBuilder.java:87)	
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:80)	
	at org.elasticsearch.index.query.SpanContainingQueryBuilder.doXContent(SpanContainingQueryBuilder.java:87)	
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:80)	
	at org.elasticsearch.index.query.SpanContainingQueryBuilder.doXContent(SpanContainingQueryBuilder.java:87)	
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:80)	
	at org.elasticsearch.index.query.SpanContainingQueryBuilder.doXContent(SpanContainingQueryBuilder.java:87)	
```
test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java
}
    protected QueryBuilder parseQuery(String queryAsString) throws IOException {
        XContentParser parser = createParser(JsonXContent.jsonXContent, queryAsString);
        return parseQuery(parser);
    }
    protected QueryBuilder parseQuery(XContentParser parser) throws IOException {
        QueryBuilder parseInnerQueryBuilder = parseTopLevelQuery(parser);
        assertNull(parser.nextToken());
        return parseInnerQueryBuilder;
    }
    /**
     * Whether the queries produced by this builder are expected to be cacheable.
     */
    protected boolean builderGeneratesCacheableQueries() {
        return true;
    }
    /**
     * Test creates the {@link Query} from the {@link QueryBuilder} under test and delegates the
     * assertions being made on the result to the implementing subclass.
     */
    public void testToQuery() throws IOException {
        for (int runs = 0; runs < NUMBER_OF_TESTQUERIES; runs++) {
            SearchExecutionContext context = createSearchExecutionContext();
            assert context.isCacheable();
            context.setAllowUnmappedFields(true);
            QB firstQuery = createTestQueryBuilder();
            QB controlQuery = copyQuery(firstQuery);
            /* we use a private rewrite context here since we want the most realistic way of asserting that we are cacheable or not.
             * We do it this way in SearchService where
             * we first rewrite the query with a private context, then reset the context and then build the actual lucene query*/