### Elasticsearch Version

8.6.0

### Installed Plugins

_No response_

### Java Version

_bundled_

### OS Version

docker-image = docker.elastic.co/elasticsearch/elasticsearch:8.6

### Problem Description

We are trying to migrate from Elasticsearch 6.7.1 to Elasticsearch 8.6.0.

With the same document we always get IndexOutOfBoundsException while trying to fetch innerhits in Elasticsearch 8.6.0. Everything works fine in Elasticsearch 6.7.1.


### Steps to Reproduce

[elasticsearch_6.7.1 (1).txt](https://github.com/elastic/elasticsearch/files/10466823/elasticsearch_6.7.1.1.txt)
[elasticsearch_8.6.0.txt](https://github.com/elastic/elasticsearch/files/10466824/elasticsearch_8.6.0.txt)


### Logs (if relevant)

`{"@timestamp":"2023-01-20T13:39:21.170Z", "log.level": "WARN", "message":"path: /test/_search, params: {index=test}", "ecs.version": "1.2.0","service.name":"ES_ECS","event.dataset":"elasticsearch.server","process.thread.name":"elasticsearch[es01][search][T#10]","log.logger":"rest.suppressed","elasticsearch.cluster.uuid":"T3_Kb_fZTOORKN7Bn5jz2g","elasticsearch.node.id":"fESFHfZ-RxuDaPIIKhOoNA","elasticsearch.node.name":"es01","elasticsearch.cluster.name":"es_alex","error.type":"org.elasticsearch.action.search.SearchPhaseExecutionException","error.message":"all shards failed","error.stack_trace":"Failed to execute phase [query], all shards failed; shardFailures {[fESFHfZ-RxuDaPIIKhOoNA][test][0]: org.elasticsearch.transport.RemoteTransportException: [es01][172.27.0.2:9300][indices:data/read/search[phase/query]]\nCaused by: org.elasticsearch.search.fetch.FetchPhaseExecutionException: Fetch Failed [Error running fetch phase for doc [73]]\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.search.fetch.FetchPhase.buildSearchHits(FetchPhase.java:181)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.search.fetch.FetchPhase.execute(FetchPhase.java:93)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.search.SearchService.executeFetchPhase(SearchService.java:659)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:634)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.search.SearchService.lambda$executeQueryPhase$2(SearchService.java:489)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.action.ActionRunnable.lambda$supply$0(ActionRunnable.java:47)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.action.ActionRunnable$2.doRun(ActionRunnable.java:62)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:26)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.common.util.concurrent.TimedRunnable.doRun(TimedRunnable.java:33)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:769)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:26)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)\n\tat java.base/java.lang.Thread.run(Thread.java:833)\nCaused by: org.elasticsearch.search.fetch.FetchPhaseExecutionException: Fetch Failed [Error running fetch phase for doc [43]]\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.search.fetch.FetchPhase.buildSearchHits(FetchPhase.java:181)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.search.fetch.FetchPhase.execute(FetchPhase.java:93)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.search.fetch.subphase.InnerHitsPhase.hitExecute(InnerHitsPhase.java:76)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.search.fetch.subphase.InnerHitsPhase$1.process(InnerHitsPhase.java:52)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.search.fetch.FetchPhase.buildSearchHits(FetchPhase.java:177)\n\t... 13 more\nCaused by: java.lang.IndexOutOfBoundsException: Index: 43, Size: 1\n\tat java.base/java.util.Collections$SingletonList.get(Collections.java:4959)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.search.fetch.FetchPhase.prepareNestedHitContext(FetchPhase.java:466)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.search.fetch.FetchPhase.prepareHitContext(FetchPhase.java:312)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.search.fetch.FetchPhase.buildSearchHits(FetchPhase.java:165)\n\t... 17 more\n}\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.action.search.AbstractSearchAsyncAction.onPhaseFailure(AbstractSearchAsyncAction.java:728)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.action.search.AbstractSearchAsyncAction.executeNextPhase(AbstractSearchAsyncAction.java:418)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.action.search.AbstractSearchAsyncAction.onPhaseDone(AbstractSearchAsyncAction.java:760)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.action.search.AbstractSearchAsyncAction.onShardFailure(AbstractSearchAsyncAction.java:512)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.action.search.AbstractSearchAsyncAction$1.onFailure(AbstractSearchAsyncAction.java:349)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.action.ActionListener$Delegating.onFailure(ActionListener.java:92)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.action.ActionListenerResponseHandler.handleException(ActionListenerResponseHandler.java:48)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.action.search.SearchTransportService$ConnectionCountingHandler.handleException(SearchTransportService.java:642)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.transport.TransportService$UnregisterChildTransportResponseHandler.handleException(TransportService.java:1650)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.transport.TransportService$ContextRestoreResponseHandler.handleException(TransportService.java:1390)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.transport.TransportService$DirectResponseChannel.processException(TransportService.java:1512)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.transport.TransportService$DirectResponseChannel.sendResponse(TransportService.java:1487)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.transport.TaskTransportChannel.sendResponse(TaskTransportChannel.java:50)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.action.support.ChannelActionListener.onFailure(ChannelActionListener.java:48)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.action.ActionRunnable.onFailure(ActionRunnable.java:77)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:28)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.common.util.concurrent.TimedRunnable.doRun(TimedRunnable.java:33)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:769)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:26)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)\n\tat java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)\n\tat java.base/java.lang.Thread.run(Thread.java:833)\nCaused by: org.elasticsearch.ElasticsearchException$1: Index: 43, Size: 1\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.ElasticsearchException.guessRootCauses(ElasticsearchException.java:638)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.action.search.AbstractSearchAsyncAction.executeNextPhase(AbstractSearchAsyncAction.java:416)\n\t... 20 more\nCaused by: java.lang.IndexOutOfBoundsException: Index: 43, Size: 1\n\tat java.base/java.util.Collections$SingletonList.get(Collections.java:4959)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.search.fetch.FetchPhase.prepareNestedHitContext(FetchPhase.java:466)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.search.fetch.FetchPhase.prepareHitContext(FetchPhase.java:312)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.search.fetch.FetchPhase.buildSearchHits(FetchPhase.java:165)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.search.fetch.FetchPhase.execute(FetchPhase.java:93)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.search.fetch.subphase.InnerHitsPhase.hitExecute(InnerHitsPhase.java:76)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.search.fetch.subphase.InnerHitsPhase$1.process(InnerHitsPhase.java:52)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.search.fetch.FetchPhase.buildSearchHits(FetchPhase.java:177)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.search.fetch.FetchPhase.execute(FetchPhase.java:93)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.search.SearchService.executeFetchPhase(SearchService.java:659)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:634)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.search.SearchService.lambda$executeQueryPhase$2(SearchService.java:489)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.action.ActionRunnable.lambda$supply$0(ActionRunnable.java:47)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.action.ActionRunnable$2.doRun(ActionRunnable.java:62)\n\tat org.elasticsearch.server@8.4.3/org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:26)\n\t... 6 more\n"}`
server/src/main/java/org/elasticsearch/index/mapper/DocumentParserContext.java
}
    public boolean isWithinCopyTo() {
        return false;
    }
    /**
     * Return a new context that will be used within a nested document.
     */
    public final DocumentParserContext createNestedContext(String fullPath) {
        if (isWithinCopyTo()) {
            
            return this;
        }
        final LuceneDocument doc = new LuceneDocument(fullPath, doc());
        addDoc(doc);
        return switchDoc(doc);
    }
    /**
     * Return a new context that has the provided document as the current document.
     */
    public final DocumentParserContext switchDoc(final LuceneDocument document) {
        return new Wrapper(this) {
            @Override
            public LuceneDocument doc() {
                return document;
            }
        };
    }
    /**
     * Return a context for copy_to directives
     * @param copyToField   the name of the field to copy to