mirror of https://github.com/apache/lucene.git
LUCENE-4925: Fix IndexSearcher.search(..., Sort, ...).
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1466694 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
f9d36d9fda
commit
67d433ca28
|
@ -257,6 +257,10 @@ Bug Fixes
|
|||
* LUCENE-4885: FacetsAccumulator did not set the correct value for
|
||||
FacetResult.numValidDescendants. (Mike McCandless, Shai Erera)
|
||||
|
||||
* LUCENE-4925: Fixed IndexSearcher.search when the argument list contains a Sort
|
||||
and one of the sort fields is the relevance score. Only IndexSearchers created
|
||||
with an ExecutorService are concerned. (Adrien Grand)
|
||||
|
||||
Documentation
|
||||
|
||||
* LUCENE-4841: Added example SimpleSortedSetFacetsExample to show how
|
||||
|
|
|
@ -804,7 +804,7 @@ public class IndexSearcher {
|
|||
public TopFieldDocs call() throws IOException {
|
||||
assert slice.leaves.length == 1;
|
||||
final TopFieldDocs docs = searcher.search(Arrays.asList(slice.leaves),
|
||||
weight, after, nDocs, sort, true, doDocScores, doMaxScore);
|
||||
weight, after, nDocs, sort, true, doDocScores || sort.needsScores(), doMaxScore);
|
||||
lock.lock();
|
||||
try {
|
||||
final AtomicReaderContext ctx = slice.leaves[0];
|
||||
|
|
|
@ -201,4 +201,15 @@ public class Sort {
|
|||
public int hashCode() {
|
||||
return 0x45aaf665 + Arrays.hashCode(fields);
|
||||
}
|
||||
|
||||
/** Whether the relevance score is needed to sort documents. */
|
||||
boolean needsScores() {
|
||||
for (SortField sortField : fields) {
|
||||
if (sortField.getType() == SortField.Type.SCORE) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -35,6 +35,7 @@ import org.apache.lucene.index.RandomIndexWriter;
|
|||
import org.apache.lucene.index.Term;
|
||||
import org.apache.lucene.index.Terms;
|
||||
import org.apache.lucene.index.TermsEnum;
|
||||
import org.apache.lucene.search.BooleanClause.Occur;
|
||||
import org.apache.lucene.store.Directory;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
import org.apache.lucene.util.LuceneTestCase;
|
||||
|
@ -1516,4 +1517,31 @@ public class TestSort extends LuceneTestCase {
|
|||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
|
||||
public void testScore() throws IOException {
|
||||
Directory dir = newDirectory();
|
||||
RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
|
||||
Document doc = new Document();
|
||||
doc.add(newStringField("value", "bar", Field.Store.NO));
|
||||
writer.addDocument(doc);
|
||||
doc = new Document();
|
||||
doc.add(newStringField("value", "foo", Field.Store.NO));
|
||||
writer.addDocument(doc);
|
||||
IndexReader ir = writer.getReader();
|
||||
writer.close();
|
||||
|
||||
IndexSearcher searcher = newSearcher(ir);
|
||||
Sort sort = new Sort(SortField.FIELD_SCORE);
|
||||
|
||||
final BooleanQuery bq = new BooleanQuery();
|
||||
bq.add(new TermQuery(new Term("value", "foo")), Occur.SHOULD);
|
||||
bq.add(new MatchAllDocsQuery(), Occur.SHOULD);
|
||||
TopDocs td = searcher.search(bq, 10, sort);
|
||||
assertEquals(2, td.totalHits);
|
||||
assertEquals(1, td.scoreDocs[0].doc);
|
||||
assertEquals(0, td.scoreDocs[1].doc);
|
||||
|
||||
ir.close();
|
||||
dir.close();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue