Apache Lucene open-source search software
CountingFacetsCollector did not sort the nodes when FacetRequest.getNumResults was > taxoReader.getSize()
Shai Erera956d88477827
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/search/CountingFacetsCollector.java b/lucene/facet/src/java/org/apache/lucene/facet/search/CountingFacetsCollector.javaindex f3b15f1fb62..8902f62ddeb 100644--- a/lucene/facet/src/java/org/apache/lucene/facet/search/CountingFacetsCollector.java+++ b/lucene/facet/src/java/org/apache/lucene/facet/search/CountingFacetsCollector.java@@ -3,6 +3,8 @@ package org.apache.lucene.facet.search;import java.io.IOException;import java.util.ArrayList;import java.util.Arrays;+import java.util.Collections;+import java.util.Comparator;import java.util.HashMap;import java.util.List;import java.util.Map.Entry;@@ -265,6 +267,17 @@ public class CountingFacetsCollector extends FacetsCollector {}child = siblings[child];}+ Collections.sort(nodes, new Comparator<FacetResultNode>() {+ @Override+ public int compare(FacetResultNode o1, FacetResultNode o2) {+ int value = (int) (o2.value - o1.value);+ if (value == 0) {+ value = o2.ordinal - o1.ordinal;+ }+ return value;+ }+ });+root.subResults = nodes;res.add(new FacetResult(fr, root, nodes.size()));continue;diff --git a/lucene/facet/src/test/org/apache/lucene/facet/search/CountingFacetsCollectorTest.java b/lucene/facet/src/test/org/apache/lucene/facet/search/CountingFacetsCollectorTest.javaindex f5fbbd8b2bb..348aa6daca1 100644--- a/lucene/facet/src/test/org/apache/lucene/facet/search/CountingFacetsCollectorTest.java+++ b/lucene/facet/src/test/org/apache/lucene/facet/search/CountingFacetsCollectorTest.java@@ -351,8 +351,16 @@ public class CountingFacetsCollectorTest extends LuceneTestCase {for (FacetResult res : facetResults) {FacetResultNode root = res.getFacetResultNode();assertEquals("wrong count for " + root.label, allExpectedCounts.get(root.label), (int) root.value);+ int prevValue = Integer.MAX_VALUE;+ int prevOrdinal = Integer.MAX_VALUE;for (FacetResultNode child : root.subResults) {assertEquals("wrong count for " + child.label, allExpectedCounts.get(child.label), (int) child.value);+ assertTrue("wrong sort order of sub results: child.value=" + child.value + " prevValue=" + prevValue, child.value <= prevValue);+ if (child.value == prevValue) {+ assertTrue("wrong sort order of sub results", child.ordinal < prevOrdinal);+ }+ prevValue = (int) child.value;+ prevOrdinal = child.ordinal;}}