Skip to content

Apache Lucene open-source search software

Stop returning null in SearchGroup#merge (#16088)

Luca Cavanna3f998812269a
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 5b0bf2fabde..b03e7fda485 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -347,6 +347,8 @@ Improvements
* GITHUB#15660: Introduce LargeNumHitsTopDocsCollectorManager to parallelize search when using LargeNumHitsTopDocsCollector. (Binlong Gao)
+* GITHUB#16088: SearchGroup#merge now returns an empty collection instead of null when topGroups is empty. (Luca Cavanna)
+
Optimizations
---------------------
* GITHUB#15861: Optimise PhraseScorer by short circuiting non competitive documents in TOP_SCORES mode. (Prithvi S)
diff --git a/lucene/grouping/src/java/org/apache/lucene/search/grouping/SearchGroup.java b/lucene/grouping/src/java/org/apache/lucene/search/grouping/SearchGroup.java
index c05b0b11265..9f434b57d03 100644
--- a/lucene/grouping/src/java/org/apache/lucene/search/grouping/SearchGroup.java
+++ b/lucene/grouping/src/java/org/apache/lucene/search/grouping/SearchGroup.java
@@ -319,11 +319,7 @@ public class SearchGroup<T> {
}
}
- if (newTopGroups.isEmpty()) {
- return null;
- } else {
- return newTopGroups;
- }
+ return newTopGroups;
}
}
@@ -332,15 +328,9 @@ public class SearchGroup<T> {
* provided groupSort must match how the groups were sorted, and the provided SearchGroups must
* have been computed with fillFields=true passed to {@link
* FirstPassGroupingCollector#getTopGroups}.
- *
- * <p>NOTE: this returns null if the topGroups is empty.
*/
public static <T> Collection<SearchGroup<T>> merge(
List<Collection<SearchGroup<T>>> topGroups, int offset, int topN, Sort groupSort) {
- if (topGroups.isEmpty()) {
- return null;
- } else {
- return new GroupMerger<T>(groupSort).merge(topGroups, offset, topN);
- }
+ return new GroupMerger<T>(groupSort).merge(topGroups, offset, topN);
}
}
diff --git a/lucene/grouping/src/test/org/apache/lucene/search/grouping/TestGrouping.java b/lucene/grouping/src/test/org/apache/lucene/search/grouping/TestGrouping.java
index c191966a031..b345f6a5c8d 100644
--- a/lucene/grouping/src/test/org/apache/lucene/search/grouping/TestGrouping.java
+++ b/lucene/grouping/src/test/org/apache/lucene/search/grouping/TestGrouping.java
@@ -269,6 +269,24 @@ public class TestGrouping extends LuceneTestCase {
dir.close();
}
+ public void testSearchGroupMergeEmptyResult() {
+ // Empty shard list must return an empty collection, not null
+ Collection<SearchGroup<BytesRef>> result =
+ SearchGroup.merge(Collections.emptyList(), 0, 5, Sort.RELEVANCE);
+ assertNotNull(result);
+ assertTrue(result.isEmpty());
+
+ // When offset skips past all groups the result must also be an empty collection, not null
+ SearchGroup<BytesRef> group = new SearchGroup<>();
+ group.groupValue = new BytesRef("a");
+ group.sortValues = new Object[] {1.0f}; // Float value for Sort.RELEVANCE
+ List<Collection<SearchGroup<BytesRef>>> shardGroups =
+ Collections.singletonList(Collections.singletonList(group));
+ result = SearchGroup.merge(shardGroups, 10, 5, Sort.RELEVANCE); // offset 10 > 1 group
+ assertNotNull(result);
+ assertTrue(result.isEmpty());
+ }
+
private void addGroupField(Document doc, String groupField, String value) {
doc.add(new SortedDocValuesField(groupField, new BytesRef(value)));
}
@@ -1512,21 +1530,17 @@ public class TestGrouping extends LuceneTestCase {
SearchGroup.merge(shardGroups, groupOffset, topNGroups, groupSort);
if (VERBOSE) {
System.out.println(" top groups merged:");
- if (mergedTopGroups == null) {
- System.out.println(" null");
- } else {
- System.out.println(" " + mergedTopGroups.size() + " top groups:");
- for (SearchGroup<BytesRef> group : mergedTopGroups) {
- System.out.println(
- " ["
- + groupToString(group.groupValue)
- + "] groupSort="
- + Arrays.toString(group.sortValues));
- }
+ System.out.println(" " + mergedTopGroups.size() + " top groups:");
+ for (SearchGroup<BytesRef> group : mergedTopGroups) {
+ System.out.println(
+ " ["
+ + groupToString(group.groupValue)
+ + "] groupSort="
+ + Arrays.toString(group.sortValues));
}
}
- if (mergedTopGroups != null) {
+ if (!mergedTopGroups.isEmpty()) {
// Now 2nd pass:
final List<TopGroups<BytesRef>> shardTopGroups = new ArrayList<>(subSearchers.length);
for (int shardIDX = 0; shardIDX < subSearchers.length; shardIDX++) {