Skip to content

Commit b0f9923

Browse files
authored
Override ValueSource.FromDoubleValuesSource.getSortField (apache#14654)
Add ValueSource.FromDoubleValuesSource.getSortField Thus reducing indirection and supporting needsScores correctly (fixes a bug).
1 parent 24f2c4b commit b0f9923

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

lucene/CHANGES.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ Optimizations
113113

114114
Bug Fixes
115115
---------------------
116-
(No changes)
116+
* GITHUB#14654: ValueSource.fromDoubleValuesSource(dvs).getSortField() would throw errors when
117+
used if the DoubleValuesSource needed scores. (David Smiley)
117118

118119
Build
119120
---------------------

lucene/queries/src/java/org/apache/lucene/queries/function/ValueSource.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,12 @@ public boolean exists(int doc) throws IOException {
319319
};
320320
}
321321

322+
@Override
323+
public SortField getSortField(boolean reverse) {
324+
// avoids indirection and supports a better needsScores()
325+
return in.getSortField(reverse);
326+
}
327+
322328
@Override
323329
public boolean equals(Object o) {
324330
if (this == o) return true;

lucene/queries/src/test/org/apache/lucene/queries/function/TestValueSources.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,31 @@ public void testBuildingFromDoubleValues() throws Exception {
876876
new FunctionQuery(ValueSource.fromDoubleValuesSource(dvs)), new float[] {3.63f, 5.65f});
877877
}
878878

879+
public void testFromDoubleValuesSortWithScore() throws Exception {
880+
// Create a query that matches all documents
881+
var query = new TermQuery(new Term("text", "test"));
882+
883+
// Get the original scores
884+
TopDocs originalDocs = searcher.search(query, documents.size());
885+
ScoreDoc[] originalScoreDocs = originalDocs.scoreDocs;
886+
887+
// Use a DoubleValuesSource that references the score and convert it to a ValueSource
888+
ValueSource scoreSource = ValueSource.fromDoubleValuesSource(DoubleValuesSource.SCORES);
889+
SortField sortField = scoreSource.getSortField(true); // true for reverse (descending)
890+
Sort sort = new Sort(sortField);
891+
892+
// Search with the sort.
893+
// note: used to fail with an assertion before fromDoubleValuesSource's impl was improved
894+
TopDocs sortedDocs = searcher.search(query, documents.size(), sort);
895+
ScoreDoc[] sortedScoreDocs = sortedDocs.scoreDocs;
896+
897+
// Verify we got the same docs in-order but don't check the scores
898+
assertEquals(originalScoreDocs.length, sortedScoreDocs.length);
899+
for (int i = 0; i < originalScoreDocs.length; i++) {
900+
assertEquals(originalScoreDocs[i].doc, sortedScoreDocs[i].doc);
901+
}
902+
}
903+
879904
/**
880905
* Asserts that for every doc, the {@link FunctionValues#exists} value from the {@link
881906
* ValueSource} is <b>true</b>.

0 commit comments

Comments
 (0)