Index: src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java =================================================================== --- src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java (revision 745917) +++ src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java (arbetskopia) @@ -23,6 +23,7 @@ import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; +import org.apache.lucene.index.IndexReader; import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.util.LuceneTestCase; @@ -34,18 +35,38 @@ public class TestMatchAllDocsQuery extends LuceneTestCase { public void testQuery() throws IOException { + RAMDirectory dir = new RAMDirectory(); IndexWriter iw = new IndexWriter(dir, new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED); iw.setMaxBufferedDocs(2); // force multi-segment - addDoc("one", iw); - addDoc("two", iw); - addDoc("three four", iw); + addDoc("one", iw, 1f); + addDoc("two", iw, 20f); + addDoc("three four", iw, 300f); iw.close(); - - IndexSearcher is = new IndexSearcher(dir); - ScoreDoc[] hits = is.search(new MatchAllDocsQuery(), null, 1000).scoreDocs; + + IndexReader ir = IndexReader.open(dir); + IndexSearcher is = new IndexSearcher(ir); + ScoreDoc[] hits; + + // assert with norms scoring turned off + + hits = is.search(new MatchAllDocsQuery(), null, 1000).scoreDocs; assertEquals(3, hits.length); + assertEquals("one", ir.document(hits[0].doc).get("key")); + assertEquals("two", ir.document(hits[1].doc).get("key")); + assertEquals("three four", ir.document(hits[2].doc).get("key")); + // assert with norms scoring turned on + + MatchAllDocsQuery normsQuery = new MatchAllDocsQuery("key"); + assertEquals(3, hits.length); +// is.explain(normsQuery, hits[0].doc); + hits = is.search(normsQuery, null, 1000).scoreDocs; + + assertEquals("three four", ir.document(hits[0].doc).get("key")); + assertEquals("two", ir.document(hits[1].doc).get("key")); + assertEquals("one", ir.document(hits[2].doc).get("key")); + // some artificial queries to trigger the use of skipTo(): BooleanQuery bq = new BooleanQuery(); @@ -66,6 +87,8 @@ assertEquals(2, hits.length); is.close(); + ir.close(); + dir.close(); } public void testEquals() { @@ -76,9 +99,11 @@ assertFalse(q1.equals(q2)); } - private void addDoc(String text, IndexWriter iw) throws IOException { + private void addDoc(String text, IndexWriter iw, float boost) throws IOException { Document doc = new Document(); - doc.add(new Field("key", text, Field.Store.YES, Field.Index.ANALYZED)); + Field f = new Field("key", text, Field.Store.YES, Field.Index.ANALYZED); + f.setBoost(boost); + doc.add(f); iw.addDocument(doc); } Index: src/java/org/apache/lucene/search/MatchAllDocsQuery.java =================================================================== --- src/java/org/apache/lucene/search/MatchAllDocsQuery.java (revision 745917) +++ src/java/org/apache/lucene/search/MatchAllDocsQuery.java (arbetskopia) @@ -31,17 +31,29 @@ public class MatchAllDocsQuery extends Query { public MatchAllDocsQuery() { + this(null); } + private final String normsField; + + /** + * @param normsField Field used for normalization factor (document boost). Null if nothing. + */ + public MatchAllDocsQuery(String normsField) { + this.normsField = normsField; + } + private class MatchAllScorer extends Scorer { final TermDocs termDocs; final float score; + final byte[] norms; - MatchAllScorer(IndexReader reader, Similarity similarity, Weight w) throws IOException + MatchAllScorer(IndexReader reader, Similarity similarity, Weight w, byte[] norms) throws IOException { super(similarity); this.termDocs = reader.termDocs(null); score = w.getValue(); + this.norms = norms; } public Explanation explain(int doc) { @@ -57,7 +69,11 @@ } public float score() { - return score; + if (norms == null) { + return score; + } else { + return score * Similarity.decodeNorm(norms[doc()]); // normalize for field + } } public boolean skipTo(int target) throws IOException { @@ -98,7 +114,8 @@ } public Scorer scorer(IndexReader reader) throws IOException { - return new MatchAllScorer(reader, similarity, this); + return new MatchAllScorer(reader, similarity, this, + normsField != null ? reader.norms(normsField) : null); } public Explanation explain(IndexReader reader, int doc) {