lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mikemcc...@apache.org
Subject svn commit: r1040939 - in /lucene/dev/branches/branch_3x: ./ lucene/ lucene/backwards/src/test/org/apache/lucene/search/ lucene/src/java/org/apache/lucene/search/ lucene/src/test/org/apache/lucene/search/ solr/
Date Wed, 01 Dec 2010 10:42:37 GMT
Author: mikemccand
Date: Wed Dec  1 10:42:36 2010
New Revision: 1040939

URL: http://svn.apache.org/viewvc?rev=1040939&view=rev
Log:
LUCENE-2785: add new TotalHitCountCollector (only counts hits); don't allow numHits=0 to TSDC
or TFC

Added:
    lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/TotalHitCountCollector.java
      - copied unchanged from r1040935, lucene/dev/trunk/lucene/src/java/org/apache/lucene/search/TotalHitCountCollector.java
Modified:
    lucene/dev/branches/branch_3x/   (props changed)
    lucene/dev/branches/branch_3x/lucene/   (props changed)
    lucene/dev/branches/branch_3x/lucene/CHANGES.txt
    lucene/dev/branches/branch_3x/lucene/backwards/src/test/org/apache/lucene/search/TestSort.java
    lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/IndexSearcher.java
    lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/TopFieldCollector.java
    lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/TopScoreDocCollector.java
    lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/search/TestSort.java
    lucene/dev/branches/branch_3x/solr/   (props changed)

Modified: lucene/dev/branches/branch_3x/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/CHANGES.txt?rev=1040939&r1=1040938&r2=1040939&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/CHANGES.txt (original)
+++ lucene/dev/branches/branch_3x/lucene/CHANGES.txt Wed Dec  1 10:42:36 2010
@@ -127,6 +127,11 @@ Changes in runtime behavior
   never be merged by default.  These changes were made to reduce peak
   transient disk usage during optimize which increased due to
   LUCENE-2762.  (Mike McCandless)
+
+* LUCENE-2785: TopScoreDocCollector, TopFieldCollector and
+  the IndexSearcher search methods that take an int nDocs will now
+  throw IllegalArgumentException if nDocs is 0.  Instead, you should
+  use the newly added TotalHitCountCollector.  (Mike McCandless)
   
 API Changes
 
@@ -233,6 +238,9 @@ API Changes
 * LUCENE-2778: RAMDirectory now exposes newRAMFile() which allows to override
   and return a different RAMFile implementation. (Shai Erera)
   
+* LUCENE-2785: Added TotalHitCountCollector whose sole purpose is to
+  count the number of hits matching the query.  (Mike McCandless)
+
 Bug fixes
 
 * LUCENE-2216: OpenBitSet.hashCode returned different hash codes for

Modified: lucene/dev/branches/branch_3x/lucene/backwards/src/test/org/apache/lucene/search/TestSort.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/backwards/src/test/org/apache/lucene/search/TestSort.java?rev=1040939&r1=1040938&r2=1040939&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/backwards/src/test/org/apache/lucene/search/TestSort.java
(original)
+++ lucene/dev/branches/branch_3x/lucene/backwards/src/test/org/apache/lucene/search/TestSort.java
Wed Dec  1 10:42:36 2010
@@ -962,7 +962,7 @@ public class TestSort extends LuceneTest
   private void assertMatches(Searcher searcher, Query query, Sort sort,
       String expectedResult) throws IOException {
     //ScoreDoc[] result = searcher.search (query, null, 1000, sort).scoreDocs;
-    TopDocs hits = searcher.search (query, null, expectedResult.length(), sort);
+    TopDocs hits = searcher.search (query, null, Math.max(1, expectedResult.length()), sort);
     ScoreDoc[] result = hits.scoreDocs;
     assertEquals(hits.totalHits, expectedResult.length());
     StringBuilder buff = new StringBuilder(10);

Modified: lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/IndexSearcher.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/IndexSearcher.java?rev=1040939&r1=1040938&r2=1040939&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/IndexSearcher.java
(original)
+++ lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/IndexSearcher.java
Wed Dec  1 10:42:36 2010
@@ -158,11 +158,11 @@ public class IndexSearcher extends Searc
   @Override
   public TopDocs search(Weight weight, Filter filter, int nDocs) throws IOException {
 
-    if (nDocs <= 0) {
-      throw new IllegalArgumentException("nDocs must be > 0");
+    int limit = reader.maxDoc();
+    if (limit == 0) {
+      limit = 1;
     }
-
-    nDocs = Math.min(nDocs, reader.maxDoc());
+    nDocs = Math.min(nDocs, limit);
 
     TopScoreDocCollector collector = TopScoreDocCollector.create(nDocs, !weight.scoresDocsOutOfOrder());
     search(weight, filter, collector);
@@ -190,7 +190,11 @@ public class IndexSearcher extends Searc
                              Sort sort, boolean fillFields)
       throws IOException {
 
-    nDocs = Math.min(nDocs, reader.maxDoc());
+    int limit = reader.maxDoc();
+    if (limit == 0) {
+      limit = 1;
+    }
+    nDocs = Math.min(nDocs, limit);
 
     TopFieldCollector collector = TopFieldCollector.create(sort, nDocs,
         fillFields, fieldSortDoTrackScores, fieldSortDoMaxScore, !weight.scoresDocsOutOfOrder());

Modified: lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/TopFieldCollector.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/TopFieldCollector.java?rev=1040939&r1=1040938&r2=1040939&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/TopFieldCollector.java
(original)
+++ lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/TopFieldCollector.java
Wed Dec  1 10:42:36 2010
@@ -909,6 +909,10 @@ public abstract class TopFieldCollector 
       throw new IllegalArgumentException("Sort must contain at least one field");
     }
     
+    if (numHits <= 0) {
+      throw new IllegalArgumentException("numHits must be > 0; please use TotalHitCountCollector
if you just need the total hit count");
+    }
+
     FieldValueHitQueue queue = FieldValueHitQueue.create(sort.fields, numHits);
     if (queue.getComparators().length == 1) {
       if (docsScoredInOrder) {

Modified: lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/TopScoreDocCollector.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/TopScoreDocCollector.java?rev=1040939&r1=1040938&r2=1040939&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/TopScoreDocCollector.java
(original)
+++ lucene/dev/branches/branch_3x/lucene/src/java/org/apache/lucene/search/TopScoreDocCollector.java
Wed Dec  1 10:42:36 2010
@@ -109,6 +109,10 @@ public abstract class TopScoreDocCollect
    */
   public static TopScoreDocCollector create(int numHits, boolean docsScoredInOrder) {
     
+    if (numHits <= 0) {
+      throw new IllegalArgumentException("numHits must be > 0; please use TotalHitCountCollector
if you just need the total hit count");
+    }
+
     if (docsScoredInOrder) {
       return new InOrderTopScoreDocCollector(numHits);
     } else {

Modified: lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/search/TestSort.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/search/TestSort.java?rev=1040939&r1=1040938&r2=1040939&view=diff
==============================================================================
--- lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/search/TestSort.java (original)
+++ lucene/dev/branches/branch_3x/lucene/src/test/org/apache/lucene/search/TestSort.java Wed
Dec  1 10:42:36 2010
@@ -26,9 +26,6 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Locale;
 
-import junit.framework.Test;
-import junit.framework.TestSuite;
-
 import org.apache.lucene.analysis.SimpleAnalyzer;
 import org.apache.lucene.document.Document;
 import org.apache.lucene.document.Field;
@@ -1045,7 +1042,7 @@ public class TestSort extends LuceneTest
   private void assertMatches(Searcher searcher, Query query, Sort sort,
       String expectedResult) throws IOException {
     //ScoreDoc[] result = searcher.search (query, null, 1000, sort).scoreDocs;
-    TopDocs hits = searcher.search (query, null, expectedResult.length(), sort);
+    TopDocs hits = searcher.search (query, null, Math.max(1, expectedResult.length()), sort);
     ScoreDoc[] result = hits.scoreDocs;
     assertEquals(hits.totalHits, expectedResult.length());
     StringBuilder buff = new StringBuilder(10);
@@ -1114,4 +1111,23 @@ public class TestSort extends LuceneTest
     indexStore.close();
   }
 
+  public void testCountingCollector() throws Exception {
+    Directory indexStore = newDirectory();
+    RandomIndexWriter writer = new RandomIndexWriter(random, indexStore);
+    for (int i=0; i<5; i++) {
+      Document doc = new Document();
+      doc.add (new Field ("string", "a"+i, Field.Store.NO, Field.Index.NOT_ANALYZED));
+      doc.add (new Field ("string", "b"+i, Field.Store.NO, Field.Index.NOT_ANALYZED));
+      writer.addDocument (doc);
+    }
+    IndexReader reader = writer.getReader();
+    writer.close();
+
+    IndexSearcher searcher = new IndexSearcher(reader);
+    TotalHitCountCollector c = new TotalHitCountCollector();
+    searcher.search(new MatchAllDocsQuery(), null, c);
+    assertEquals(5, c.getTotalHits());
+    reader.close();
+    indexStore.close();
+  }
 }



Mime
View raw message