lucene-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dna...@apache.org
Subject svn commit: r234190 - in /lucene/java/trunk: CHANGES.txt src/java/org/apache/lucene/search/MatchAllDocsQuery.java src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java
Date Sun, 21 Aug 2005 12:23:08 GMT
Author: dnaber
Date: Sun Aug 21 05:23:00 2005
New Revision: 234190

URL: http://svn.apache.org/viewcvs?rev=234190&view=rev
Log:
adding MatchAllDocsQuery by John Wang, bug #34946

Added:
    lucene/java/trunk/src/java/org/apache/lucene/search/MatchAllDocsQuery.java
    lucene/java/trunk/src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java
Modified:
    lucene/java/trunk/CHANGES.txt

Modified: lucene/java/trunk/CHANGES.txt
URL: http://svn.apache.org/viewcvs/lucene/java/trunk/CHANGES.txt?rev=234190&r1=234189&r2=234190&view=diff
==============================================================================
--- lucene/java/trunk/CHANGES.txt (original)
+++ lucene/java/trunk/CHANGES.txt Sun Aug 21 05:23:00 2005
@@ -142,6 +142,9 @@
     to system properties will not cause a SecurityException anymore.
     (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
 
  1. Several methods and fields have been deprecated. The API documentation 

Added: lucene/java/trunk/src/java/org/apache/lucene/search/MatchAllDocsQuery.java
URL: http://svn.apache.org/viewcvs/lucene/java/trunk/src/java/org/apache/lucene/search/MatchAllDocsQuery.java?rev=234190&view=auto
==============================================================================
--- lucene/java/trunk/src/java/org/apache/lucene/search/MatchAllDocsQuery.java (added)
+++ lucene/java/trunk/src/java/org/apache/lucene/search/MatchAllDocsQuery.java Sun Aug 21
05:23:00 2005
@@ -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());
+  }
+}

Added: lucene/java/trunk/src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java
URL: http://svn.apache.org/viewcvs/lucene/java/trunk/src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java?rev=234190&view=auto
==============================================================================
--- lucene/java/trunk/src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java (added)
+++ lucene/java/trunk/src/test/org/apache/lucene/search/TestMatchAllDocsQuery.java Sun Aug
21 05:23:00 2005
@@ -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);
+  }
+
+}



Mime
View raw message