diff --git a/lucene/grouping/src/java/org/apache/lucene/search/grouping/GroupFacetCollector.java b/lucene/grouping/src/java/org/apache/lucene/search/grouping/GroupFacetCollector.java index 93efbd4a0465..c2f613f3631e 100644 --- a/lucene/grouping/src/java/org/apache/lucene/search/grouping/GroupFacetCollector.java +++ b/lucene/grouping/src/java/org/apache/lucene/search/grouping/GroupFacetCollector.java @@ -18,6 +18,7 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.NavigableSet; @@ -181,7 +182,11 @@ public void addFacetCount(BytesRef facetValue, int count) { * @return a list of facet entries to be rendered based on the specified offset and limit */ public List getFacetEntries(int offset, int limit) { - List entries = new ArrayList<>(); + if (offset >= facetEntries.size()) { + return Collections.emptyList(); + } + + List entries = new ArrayList<>(Math.min(limit, facetEntries.size() - offset)); int skipped = 0; int included = 0;