Skip to content

Apache Lucene open-source search software

LUCENE-1543: allow MatchAllDocsQuery to specify a field whose norms are used to set document scores

Michael McCandless7d18c4997ab4
diff --git a/CHANGES.txt b/CHANGES.txt
index f053b100e13..b0082a8b0dc 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -183,6 +183,10 @@ New features
maintain sort order of the original byte[] when the bytes are
interpreted as unsigned. (Steven Rowe via Mike McCandless)
+18. LUCENE-1543: Allow MatchAllDocsQuery to optionally use norms from
+ a specific fields to set the score for a document. (Karl Wettin
+ via Mike McCandless)
+
Optimizations
1. LUCENE-1427: Fixed QueryWrapperFilter to not waste time computing
diff --git a/src/java/org/apache/lucene/search/MatchAllDocsQuery.java b/src/java/org/apache/lucene/search/MatchAllDocsQuery.java
index dc0ae56725e..685e6d68235 100644
--- a/src/java/org/apache/lucene/search/MatchAllDocsQuery.java
+++ b/src/java/org/apache/lucene/search/MatchAllDocsQuery.java
@@ -31,17 +31,29 @@ import java.io.IOException;
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 class MatchAllDocsQuery extends Query {
}
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 class MatchAllDocsQuery extends Query {
}
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) {
diff --git a/src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java b/src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java
index e4559a055f0..13380fad97d 100644
--- a/src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java
+++ b/src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java
@@ -23,6 +23,7 @@ import org.apache.lucene.document.Document;
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,47 @@ import org.apache.lucene.util.LuceneTestCase;
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");
+ hits = is.search(normsQuery, null, 1000).scoreDocs;
+ assertEquals(3, hits.length);
+
+ 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"));
+
+ // change norm & retest
+ ir.setNorm(0, "key", 400f);
+ normsQuery = new MatchAllDocsQuery("key");
+ hits = is.search(normsQuery, null, 1000).scoreDocs;
+ assertEquals(3, hits.length);
+
+ assertEquals("one", ir.document(hits[0].doc).get("key"));
+ assertEquals("three four", ir.document(hits[1].doc).get("key"));
+ assertEquals("two", ir.document(hits[2].doc).get("key"));
+
// some artificial queries to trigger the use of skipTo():
BooleanQuery bq = new BooleanQuery();
@@ -66,6 +96,8 @@ public class TestMatchAllDocsQuery extends LuceneTestCase {
assertEquals(2, hits.length);
is.close();
+ ir.close();
+ dir.close();
}
public void testEquals() {
@@ -76,9 +108,11 @@ public class TestMatchAllDocsQuery extends LuceneTestCase {
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);
}