Skip to content

Commit

Permalink
Use original order by default for same-score items rather than sortin…
Browse files Browse the repository at this point in the history
…g by docId
  • Loading branch information
andywebb1975 committed Dec 22, 2024
1 parent 8c7050b commit 2e4dec9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
10 changes: 10 additions & 0 deletions lucene/core/src/java/org/apache/lucene/search/QueryRescorer.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ public TopDocs rescore(IndexSearcher searcher, TopDocs firstPassTopDocs, int top
throws IOException {
ScoreDoc[] hits = firstPassTopDocs.scoreDocs.clone();

// record original index of each hit in its originalIndex
// NB code below relies on hits being in docId order so the order is about to be lost
for (int i = 0; i < hits.length; i++) {
hits[i].originalIndex = i;
}

Arrays.sort(hits, (a, b) -> a.doc - b.doc);

List<LeafReaderContext> leaves = searcher.getIndexReader().leaves();
Expand Down Expand Up @@ -110,6 +116,10 @@ public TopDocs rescore(IndexSearcher searcher, TopDocs firstPassTopDocs, int top
return -1;
} else if (a.score < b.score) {
return 1;
} else if (a.originalIndex > b.originalIndex) {
return 1;
} else if (a.originalIndex < b.originalIndex) {
return -1;
} else {
// This subtraction can't overflow int
// because docIDs are >= 0:
Expand Down
12 changes: 11 additions & 1 deletion lucene/core/src/java/org/apache/lucene/search/ScoreDoc.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public class ScoreDoc {
/** Only set by {@link TopDocs#merge} */
public int shardIndex;

/** Only set by {@link QueryRescorer#rescore} */
public int originalIndex;

/** Constructs a ScoreDoc. */
public ScoreDoc(int doc, float score) {
this(doc, score, -1);
Expand All @@ -49,6 +52,13 @@ public ScoreDoc(int doc, float score, int shardIndex) {
// A convenience method for debugging.
@Override
public String toString() {
return "doc=" + doc + " score=" + score + " shardIndex=" + shardIndex;
return "doc="
+ doc
+ " score="
+ score
+ " shardIndex="
+ shardIndex
+ " originalIndex="
+ originalIndex;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,8 @@ public int compare(Integer a, Integer b) {
} else if (bv < av) {
return reverseInt;
} else {
// Tie break by docID, ascending
return a - b;
// use original order if scores are the same
return 0;
}
} catch (IOException ioe) {
throw new RuntimeException(ioe);
Expand All @@ -464,8 +464,7 @@ public int compare(Integer a, Integer b) {

boolean fail = false;
for (int i = 0; i < numHits; i++) {
// System.out.println("expected=" + expected[i] + " vs " + hits2.scoreDocs[i].doc + " v=" +
// idToNum[Integer.parseInt(r.storedFields().document(expected[i]).get("id"))]);
// System.out.println("expected=" + expected[i] + " vs " + hits2.scoreDocs[i].toString());
if (expected[i].intValue() != hits2.scoreDocs[i].doc) {
// System.out.println(" diff!");
fail = true;
Expand Down

0 comments on commit 2e4dec9

Please sign in to comment.