Skip to content

Apache Lucene open-source search software

Optimize GroupFacetCollector.getFacetEntries() (#15947)

gaobinlongb3e166335bf1
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 93efbd4a046..c2f613f3631 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 @@ package org.apache.lucene.search.grouping;
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 abstract class GroupFacetCollector extends SimpleCollector {
* @return a list of facet entries to be rendered based on the specified offset and limit
*/
public List<FacetEntry> getFacetEntries(int offset, int limit) {
- List<FacetEntry> entries = new ArrayList<>();
+ if (offset >= facetEntries.size()) {
+ return Collections.emptyList();
+ }
+
+ List<FacetEntry> entries = new ArrayList<>(Math.min(limit, facetEntries.size() - offset));
int skipped = 0;
int included = 0;