Apache Solr open-source search software
LUCENE-8529: TopSuggestDocsCollector will now use the completion key to tiebreak completion suggestions with identical scores.
Jim Ferenczi81c092d8262a
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex a8e100a3bd4..88b09e9badc 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -228,6 +228,9 @@ Improvements* LUCENE-8562: Speed up merging segments of points with data dimensions by only sorting on the indexeddimensions. (Ignacio Vera)+* LUCENE-8529: TopSuggestDocsCollector will now use the completion key to tiebreak completion+ suggestion with identical scores. (Jim Ferenczi)+Optimizations* LUCENE-8552: FieldInfos.getMergedFieldInfos no longer does any merging if there is <= 1 segment.diff --git a/lucene/suggest/src/java/org/apache/lucene/search/suggest/document/SuggestScoreDocPriorityQueue.java b/lucene/suggest/src/java/org/apache/lucene/search/suggest/document/SuggestScoreDocPriorityQueue.javaindex b395a3f784d..9927cdd3062 100644--- a/lucene/suggest/src/java/org/apache/lucene/search/suggest/document/SuggestScoreDocPriorityQueue.java+++ b/lucene/suggest/src/java/org/apache/lucene/search/suggest/document/SuggestScoreDocPriorityQueue.java@@ -16,6 +16,7 @@*/package org.apache.lucene.search.suggest.document;+import org.apache.lucene.search.suggest.Lookup;import org.apache.lucene.search.suggest.document.TopSuggestDocs.SuggestScoreDoc;import org.apache.lucene.util.PriorityQueue;@@ -35,8 +36,10 @@ final class SuggestScoreDocPriorityQueue extends PriorityQueue<SuggestScoreDoc>@Overrideprotected boolean lessThan(SuggestScoreDoc a, SuggestScoreDoc b) {if (a.score == b.score) {+ // tie break by completion key+ int cmp = Lookup.CHARSEQUENCE_COMPARATOR.compare(a.key, b.key);// prefer smaller doc id, in case of a tie- return a.doc > b.doc;+ return cmp != 0 ? cmp > 0 : a.doc > b.doc;}return a.score < b.score;}diff --git a/lucene/suggest/src/java/org/apache/lucene/search/suggest/document/TopSuggestDocsCollector.java b/lucene/suggest/src/java/org/apache/lucene/search/suggest/document/TopSuggestDocsCollector.javaindex 6a7f1eaef41..0e895001976 100644--- a/lucene/suggest/src/java/org/apache/lucene/search/suggest/document/TopSuggestDocsCollector.java+++ b/lucene/suggest/src/java/org/apache/lucene/search/suggest/document/TopSuggestDocsCollector.java@@ -19,7 +19,6 @@ package org.apache.lucene.search.suggest.document;import java.io.IOException;import java.util.ArrayList;import java.util.Collections;-import java.util.Comparator;import java.util.List;import org.apache.lucene.analysis.CharArraySet;@@ -28,6 +27,7 @@ import org.apache.lucene.search.CollectionTerminatedException;import org.apache.lucene.search.ScoreMode;import org.apache.lucene.search.SimpleCollector;import org.apache.lucene.search.TotalHits;+import org.apache.lucene.search.suggest.Lookup;import static org.apache.lucene.search.suggest.document.TopSuggestDocs.SuggestScoreDoc;@@ -148,18 +148,19 @@ public class TopSuggestDocsCollector extends SimpleCollector {// numSegments), but typically numSegments is smallish and num is smallish so this won't matter much in practice:Collections.sort(pendingResults,- new Comparator<SuggestScoreDoc>() {- @Override- public int compare(SuggestScoreDoc a, SuggestScoreDoc b) {- // sort by higher score- int cmp = Float.compare(b.score, a.score);- if (cmp == 0) {- // tie break by lower docID:- cmp = Integer.compare(a.doc, b.doc);- }- return cmp;- }- });+ (a, b) -> {+ // sort by higher score+ int cmp = Float.compare(b.score, a.score);+ if (cmp == 0) {+ // tie break by completion key+ cmp = Lookup.CHARSEQUENCE_COMPARATOR.compare(a.key, b.key);+ if (cmp == 0) {+ // prefer smaller doc id, in case of a tie+ cmp = Integer.compare(a.doc, b.doc);+ }+ }+ return cmp;+ });List<SuggestScoreDoc> hits = new ArrayList<>();diff --git a/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestFuzzyCompletionQuery.java b/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestFuzzyCompletionQuery.javaindex 40c3f88118d..ae93de4462a 100644--- a/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestFuzzyCompletionQuery.java+++ b/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestFuzzyCompletionQuery.java@@ -103,8 +103,8 @@ public class TestFuzzyCompletionQuery extends LuceneTestCase {CompletionQuery query = new ContextQuery(new FuzzyCompletionQuery(analyzer, new Term("suggest_field", "sugge")));TopSuggestDocs suggest = suggestIndexSearcher.suggest(query, 5, false);assertSuggestions(suggest,- new Entry("suggestion", "type4", 4),new Entry("suggdestion", "type4", 4),+ new Entry("suggestion", "type4", 4),new Entry("sugdgestion", "type3", 3),new Entry("sudggestion", "type2", 2),new Entry("sduggestion", "type1", 1)diff --git a/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestSuggestField.java b/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestSuggestField.javaindex 4b8cab36a05..d5503a54ef4 100644--- a/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestSuggestField.java+++ b/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestSuggestField.java@@ -21,7 +21,6 @@ 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.HashSet;import java.util.List;@@ -53,6 +52,7 @@ import org.apache.lucene.index.Term;import org.apache.lucene.search.ScoreDoc;import org.apache.lucene.search.TopDocs;import org.apache.lucene.search.suggest.BitsProducer;+import org.apache.lucene.search.suggest.Lookup;import org.apache.lucene.store.Directory;import org.apache.lucene.store.OutputStreamDataOutput;import org.apache.lucene.util.Bits;@@ -342,18 +342,19 @@ public class TestSuggestField extends LuceneTestCase {}}Collections.sort(expected,- new Comparator<Entry>() {- @Override- public int compare(Entry a, Entry b) {- // sort by higher score:- int cmp = Float.compare(b.value, a.value);- if (cmp == 0) {- // tie break by smaller docID:- cmp = Integer.compare(a.id, b.id);- }- return cmp;- }- });+ (a, b) -> {+ // sort by higher score:+ int cmp = Float.compare(b.value, a.value);+ if (cmp == 0) {+ // tie break by completion key+ cmp = Lookup.CHARSEQUENCE_COMPARATOR.compare(a.output, b.output);+ if (cmp == 0) {+ // prefer smaller doc id, in case of a tie+ cmp = Integer.compare(a.id, b.id);+ }+ }+ return cmp;+ });boolean dedup = random().nextBoolean();if (dedup) {