### CI Link

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

### Repro line

`gradlew ':server:test' --tests "org.elasticsearch.index.query.SpanNotQueryBuilderTests.testMaxNestedDepth" -Dtests.seed=FCAC1620A8E66666 -Dtests.locale=hu -Dtests.timezone=Etc/GMT+11 -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.SpanNotQueryBuilderTests&tests.test=testMaxNestedDepth#

### Failure excerpt

```
java.lang.OutOfMemoryError: Java heap space	
	at __randomizedtesting.SeedInfo.seed([FCAC1620A8E66666:445BD40EBC1BAFC9]:0)	
		•••
	at org.elasticsearch.index.query.SpanNotQueryBuilder.doXContent(SpanNotQueryBuilder.java:145)	
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:80)	
	at org.elasticsearch.index.query.SpanNotQueryBuilder.doXContent(SpanNotQueryBuilder.java:148)	
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:80)	
	at org.elasticsearch.index.query.SpanNotQueryBuilder.doXContent(SpanNotQueryBuilder.java:148)	
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:80)	
	at org.elasticsearch.index.query.SpanNotQueryBuilder.doXContent(SpanNotQueryBuilder.java:146)	
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:80)	
	at org.elasticsearch.index.query.SpanNotQueryBuilder.doXContent(SpanNotQueryBuilder.java:146)	
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:80)	
	at org.elasticsearch.index.query.SpanNotQueryBuilder.doXContent(SpanNotQueryBuilder.java:146)	
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:80)	
	at org.elasticsearch.index.query.SpanNotQueryBuilder.doXContent(SpanNotQueryBuilder.java:146)	
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:80)	
	at org.elasticsearch.index.query.SpanNotQueryBuilder.doXContent(SpanNotQueryBuilder.java:148)	
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:80)	
	at org.elasticsearch.index.query.SpanNotQueryBuilder.doXContent(SpanNotQueryBuilder.java:146)	
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:80)	
	at org.elasticsearch.index.query.SpanNotQueryBuilder.doXContent(SpanNotQueryBuilder.java:146)	
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:80)	
	at org.elasticsearch.index.query.SpanNotQueryBuilder.doXContent(SpanNotQueryBuilder.java:146)	
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:80)	
	at org.elasticsearch.index.query.SpanNotQueryBuilder.doXContent(SpanNotQueryBuilder.java:148)	
	at org.elasticsearch.index.query.AbstractQueryBuilder.toXContent(AbstractQueryBuilder.java:80)	
	at org.elasticsearch.index.query.SpanNotQueryBuilder.doXContent(SpanNotQueryBuilder.java:148)
```
server/src/main/java/org/elasticsearch/index/query/AbstractQueryBuilder.java
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import static org.elasticsearch.search.SearchModule.INDICES_MAX_NESTED_DEPTH_SETTING;
/**
 * Base class for all classes producing lucene queries.
 * Supports conversion to BytesReference and creation of lucene Query objects.
 */
public abstract class AbstractQueryBuilder<QB extends AbstractQueryBuilder<QB>> implements QueryBuilder {
    
    public static final float DEFAULT_BOOST = 1.0f;
    public static final ParseField NAME_FIELD = new ParseField("_name");
    public static final ParseField BOOST_FIELD = new ParseField("boost");
    private static int maxNestedDepth = 20;
    protected String queryName;
    protected float boost = DEFAULT_BOOST;
    protected AbstractQueryBuilder() {
    }
    protected AbstractQueryBuilder(StreamInput in) throws IOException {
        boost = in.readFloat();
        checkNegativeBoost(boost);
        queryName = in.readOptionalString();
    }
    @Override
    public final void writeTo(StreamOutput out) throws IOException {
        out.writeFloat(boost);
        out.writeOptionalString(queryName);