Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

fix FieldSortedHitQueue.maxscore: LUCENE-462

Yonik Seeley34c98cc0bfc5
diff --git a/CHANGES.txt b/CHANGES.txt
index 89f186117e7..baf166d7892 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -249,6 +249,10 @@ Bug fixes
16. Fixed a sorting problem with MultiSearchers that can lead to
missing or duplicate docs due to equal docs sorting in an arbitrary order.
(Yonik Seeley, LUCENE-456)
+
+17. A single hit using the expert level sorted search methods
+ resulted in the score not being normalized.
+ (Yonik Seeley, LUCENE-462)
Optimizations
diff --git a/src/java/org/apache/lucene/search/FieldSortedHitQueue.java b/src/java/org/apache/lucene/search/FieldSortedHitQueue.java
index 710674c39b4..6fd0497655b 100644
--- a/src/java/org/apache/lucene/search/FieldSortedHitQueue.java
+++ b/src/java/org/apache/lucene/search/FieldSortedHitQueue.java
@@ -68,11 +68,29 @@ extends PriorityQueue {
/** Stores the sort criteria being used. */
protected SortField[] fields;
- /** Stores the maximum score value encountered, for normalizing.
- * we only care about scores greater than 1.0 - if all the scores
- * are less than 1.0, we don't have to normalize. */
- protected float maxscore = 1.0f;
+ /** Stores the maximum score value encountered, needed for normalizing. */
+ protected float maxscore = Float.NEGATIVE_INFINITY;
+ /** returns the maximum score encountered by elements inserted via insert()
+ */
+ public float getMaxScore() {
+ return maxscore;
+ }
+
+ // The signature of this method takes a FieldDoc in order to avoid
+ // the unneeded cast to retrieve the score.
+ // inherit javadoc
+ public boolean insert(FieldDoc fdoc) {
+ maxscore = Math.max(maxscore,fdoc.score);
+ return super.insert(fdoc);
+ }
+
+ // This overrides PriorityQueue.insert() so that insert(FieldDoc) that
+ // keeps track of the score isn't accidentally bypassed.
+ // inherit javadoc
+ public boolean insert(Object fdoc) {
+ return insert((FieldDoc)fdoc);
+ }
/**
* Returns whether <code>a</code> is less relevant than <code>b</code>.
@@ -84,10 +102,6 @@ extends PriorityQueue {
final ScoreDoc docA = (ScoreDoc) a;
final ScoreDoc docB = (ScoreDoc) b;
- // keep track of maximum score
- if (docA.score > maxscore) maxscore = docA.score;
- if (docB.score > maxscore) maxscore = docB.score;
-
// run comparators
final int n = comparators.length;
int c = 0;
diff --git a/src/test/org/apache/lucene/search/TestSort.java b/src/test/org/apache/lucene/search/TestSort.java
index 89b0346eb7d..f9813b7d9fb 100644
--- a/src/test/org/apache/lucene/search/TestSort.java
+++ b/src/test/org/apache/lucene/search/TestSort.java
@@ -516,7 +516,7 @@ implements Serializable {
TopDocs docs2 = full.search(queryE, filt, nDocs, sort);
- // assertEquals(docs1.scoreDocs[0].score, docs2.scoreDocs[0].score, 1e-6);
+ assertEquals(docs1.scoreDocs[0].score, docs2.scoreDocs[0].score, 1e-6);
}