diff --git a/src/main/java/org/elasticsearch/index/search/child/TopChildrenQuery.java b/src/main/java/org/elasticsearch/index/search/child/TopChildrenQuery.java index 5af4b67b9b3..21e4fca083e 100644 --- a/src/main/java/org/elasticsearch/index/search/child/TopChildrenQuery.java +++ b/src/main/java/org/elasticsearch/index/search/child/TopChildrenQuery.java @@ -208,7 +208,9 @@ public class TopChildrenQuery extends Query { // we found a match, add it and break IntObjectOpenHashMap readerParentDocs = parentDocsPerReader.get(indexReader.getCoreCacheKey()); if (readerParentDocs == null) { - readerParentDocs = new IntObjectOpenHashMap<>(indexReader.maxDoc()); + //The number of docs in the reader and in the query both upper bound the size of parentDocsPerReader + int mapSize = Math.min(indexReader.maxDoc(), context.from() + context.size()); + readerParentDocs = new IntObjectOpenHashMap<>(mapSize); parentDocsPerReader.put(indexReader.getCoreCacheKey(), readerParentDocs); } ParentDoc parentDoc = readerParentDocs.get(parentDocId);