adding MatchAllDocsQuery by John Wang, bug #34946

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@234190 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Daniel Naber 2005-08-21 12:23:00 +00:00
parent c294be7007
commit 3b9f3cf427
3 changed files with 237 additions and 0 deletions

View File

@ -142,6 +142,9 @@ New features
to system properties will not cause a SecurityException anymore. to system properties will not cause a SecurityException anymore.
(Jon Schuster via Daniel Naber, bug #34359) (Jon Schuster via Daniel Naber, bug #34359)
20. Added a new class MatchAllDocsQuery that matches all documents.
(John Wang via Daniel Naber, bug #34946)
API Changes API Changes
1. Several methods and fields have been deprecated. The API documentation 1. Several methods and fields have been deprecated. The API documentation

View File

@ -0,0 +1,150 @@
package org.apache.lucene.search;
/**
* Copyright 2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.Searcher;
import org.apache.lucene.search.Similarity;
import org.apache.lucene.search.Weight;
/**
* A query that matches all documents.
*
* @author John Wang
*/
public class MatchAllDocsQuery extends Query {
public MatchAllDocsQuery() {
}
private class MatchAllScorer extends Scorer {
IndexReader reader;
int count;
int maxDoc;
MatchAllScorer(IndexReader reader, Similarity similarity) {
super(similarity);
this.reader = reader;
count = -1;
maxDoc = reader.maxDoc();
}
public int doc() {
return count;
}
public Explanation explain(int doc) {
Explanation explanation = new Explanation();
explanation.setValue(1.0f);
explanation.setDescription("MatchAllDocsQuery");
return explanation;
}
public boolean next() {
while (count < (maxDoc - 1)) {
count++;
if (!reader.isDeleted(count)) {
return true;
}
}
return false;
}
public float score() {
return 1.0f;
}
public boolean skipTo(int target) {
count = target - 1;
return next();
}
}
private class MatchAllDocsWeight implements Weight {
private Searcher searcher;
public MatchAllDocsWeight(Searcher searcher) {
this.searcher = searcher;
}
public String toString() {
return "weight(" + MatchAllDocsQuery.this + ")";
}
public Query getQuery() {
return MatchAllDocsQuery.this;
}
public float getValue() {
return 1.0f;
}
public float sumOfSquaredWeights() {
return 1.0f;
}
public void normalize(float queryNorm) {
}
public Scorer scorer(IndexReader reader) {
return new MatchAllScorer(reader, getSimilarity(searcher));
}
public Explanation explain(IndexReader reader, int doc) {
// explain query weight
Explanation queryExpl = new Explanation();
queryExpl.setDescription("MatchAllDocsQuery:");
Explanation boostExpl = new Explanation(getBoost(), "boost");
if (getBoost() != 1.0f)
queryExpl.addDetail(boostExpl);
queryExpl.setValue(boostExpl.getValue());
return queryExpl;
}
}
protected Weight createWeight(Searcher searcher) {
return new MatchAllDocsWeight(searcher);
}
public String toString(String field) {
StringBuffer buffer = new StringBuffer();
buffer.append("MatchAllDocsQuery");
if (getBoost() != 1.0f) {
buffer.append("^");
buffer.append(Float.toString(getBoost()));
}
return buffer.toString();
}
public boolean equals(Object o) {
if (!(o instanceof MatchAllDocsQuery))
return false;
MatchAllDocsQuery other = (MatchAllDocsQuery) o;
return this.getBoost() == other.getBoost();
}
public int hashCode() {
return Float.floatToIntBits(getBoost());
}
}

View File

@ -0,0 +1,84 @@
/**
* Copyright 2005 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.lucene.search;
import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
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.store.RAMDirectory;
import junit.framework.TestCase;
/**
* Tests MatchAllDocsQuery.
*
* @author Daniel Naber
*/
public class TestMatchAllDocsQuery extends TestCase {
public void testQuery() throws IOException {
RAMDirectory dir = new RAMDirectory();
IndexWriter iw = new IndexWriter(dir, new StandardAnalyzer(), true);
addDoc("one", iw);
addDoc("two", iw);
addDoc("three four", iw);
iw.close();
IndexSearcher is = new IndexSearcher(dir);
Hits hits = is.search(new MatchAllDocsQuery());
assertEquals(3, hits.length());
// some artificial queries to trigger the use of skipTo():
BooleanQuery bq = new BooleanQuery();
bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
hits = is.search(bq);
assertEquals(3, hits.length());
bq = new BooleanQuery();
bq.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
bq.add(new TermQuery(new Term("key", "three")), BooleanClause.Occur.MUST);
hits = is.search(bq);
assertEquals(1, hits.length());
// delete a document:
is.getIndexReader().delete(0);
hits = is.search(new MatchAllDocsQuery());
assertEquals(2, hits.length());
is.close();
}
public void testEquals() {
Query q1 = new MatchAllDocsQuery();
Query q2 = new MatchAllDocsQuery();
assertTrue(q1.equals(q2));
q1.setBoost(1.5f);
assertFalse(q1.equals(q2));
}
private void addDoc(String text, IndexWriter iw) throws IOException {
Document doc = new Document();
doc.add(new Field("key", text, Field.Store.YES, Field.Index.TOKENIZED));
iw.addDocument(doc);
}
}