From 34c98cc0bfc5030548ef3dd35c4e8979dafba46a Mon Sep 17 00:00:00 2001 From: Yonik Seeley Date: Fri, 11 Nov 2005 21:19:02 +0000 Subject: [PATCH] fix FieldSortedHitQueue.maxscore: LUCENE-462 git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@332651 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 4 +++ .../lucene/search/FieldSortedHitQueue.java | 30 ++++++++++++++----- .../org/apache/lucene/search/TestSort.java | 2 +- 3 files changed, 27 insertions(+), 9 deletions(-) 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 a is less relevant than b. @@ -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); }