You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jp...@apache.org on 2022/01/25 18:01:44 UTC

[lucene] branch branch_9x updated: LUCENE-10002: Replace simple usages of TotalHitCountCollector with IndexSearcher#count (#612)

This is an automated email from the ASF dual-hosted git repository.

jpountz pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/branch_9x by this push:
     new 794f941  LUCENE-10002: Replace simple usages of TotalHitCountCollector with IndexSearcher#count (#612)
794f941 is described below

commit 794f941f52f77737633ea23cd1892d330ce030a7
Author: Luca Cavanna <ja...@users.noreply.github.com>
AuthorDate: Tue Jan 25 16:11:19 2022 +0100

    LUCENE-10002: Replace simple usages of TotalHitCountCollector with IndexSearcher#count (#612)
    
    In case only number of documents are collected, IndexSearcher#search(Query, Collector) is commonly used, which does not use the executor that's been eventually set to the searcher. Calling `IndexSearcher#count(Query)` makes the code more concise and is also more correct as it honours the executor that's been set to the searcher instance.
    
    Co-authored-by: Adrien Grand <jp...@gmail.com>
---
 .../lucene/classification/CachingNaiveBayesClassifier.java       | 5 +----
 .../apache/lucene/classification/SimpleNaiveBayesClassifier.java | 9 ++-------
 .../document/SimpleNaiveBayesDocumentClassifier.java             | 5 +----
 .../java/org/apache/lucene/search/TotalHitCountCollector.java    | 7 ++++++-
 .../test/org/apache/lucene/search/TestFuzzyTermOnShortTerms.java | 5 ++---
 .../src/test/org/apache/lucene/search/TestLRUQueryCache.java     | 2 +-
 .../src/test/org/apache/lucene/search/join/TestJoinUtil.java     | 5 ++---
 .../apache/lucene/spatial/prefix/TestHeatmapFacetCounter.java    | 8 +++-----
 8 files changed, 18 insertions(+), 28 deletions(-)

diff --git a/lucene/classification/src/java/org/apache/lucene/classification/CachingNaiveBayesClassifier.java b/lucene/classification/src/java/org/apache/lucene/classification/CachingNaiveBayesClassifier.java
index db8c6db..deff420 100644
--- a/lucene/classification/src/java/org/apache/lucene/classification/CachingNaiveBayesClassifier.java
+++ b/lucene/classification/src/java/org/apache/lucene/classification/CachingNaiveBayesClassifier.java
@@ -32,7 +32,6 @@ import org.apache.lucene.search.BooleanClause;
 import org.apache.lucene.search.BooleanQuery;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.TermQuery;
-import org.apache.lucene.search.TotalHitCountCollector;
 import org.apache.lucene.util.BytesRef;
 
 /**
@@ -179,10 +178,8 @@ public class CachingNaiveBayesClassifier extends SimpleNaiveBayesClassifier {
         if (query != null) {
           booleanQuery.add(query, BooleanClause.Occur.MUST);
         }
-        TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector();
-        indexSearcher.search(booleanQuery.build(), totalHitCountCollector);
 
-        int ret = totalHitCountCollector.getTotalHits();
+        int ret = indexSearcher.count(booleanQuery.build());
         if (ret != 0) {
           searched.put(cclass, ret);
         }
diff --git a/lucene/classification/src/java/org/apache/lucene/classification/SimpleNaiveBayesClassifier.java b/lucene/classification/src/java/org/apache/lucene/classification/SimpleNaiveBayesClassifier.java
index c1b5601..668c649 100644
--- a/lucene/classification/src/java/org/apache/lucene/classification/SimpleNaiveBayesClassifier.java
+++ b/lucene/classification/src/java/org/apache/lucene/classification/SimpleNaiveBayesClassifier.java
@@ -35,7 +35,6 @@ import org.apache.lucene.search.BooleanQuery;
 import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.TermQuery;
-import org.apache.lucene.search.TotalHitCountCollector;
 import org.apache.lucene.search.WildcardQuery;
 import org.apache.lucene.util.BytesRef;
 
@@ -169,7 +168,6 @@ public class SimpleNaiveBayesClassifier implements Classifier<BytesRef> {
     Terms terms = MultiTerms.getTerms(this.indexReader, this.classFieldName);
     int docCount;
     if (terms == null || terms.getDocCount() == -1) { // in case codec doesn't support getDocCount
-      TotalHitCountCollector classQueryCountCollector = new TotalHitCountCollector();
       BooleanQuery.Builder q = new BooleanQuery.Builder();
       q.add(
           new BooleanClause(
@@ -179,8 +177,7 @@ public class SimpleNaiveBayesClassifier implements Classifier<BytesRef> {
       if (query != null) {
         q.add(query, BooleanClause.Occur.MUST);
       }
-      indexSearcher.search(q.build(), classQueryCountCollector);
-      docCount = classQueryCountCollector.getTotalHits();
+      docCount = indexSearcher.count(q.build());
     } else {
       docCount = terms.getDocCount();
     }
@@ -276,9 +273,7 @@ public class SimpleNaiveBayesClassifier implements Classifier<BytesRef> {
     if (query != null) {
       booleanQuery.add(query, BooleanClause.Occur.MUST);
     }
-    TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector();
-    indexSearcher.search(booleanQuery.build(), totalHitCountCollector);
-    return totalHitCountCollector.getTotalHits();
+    return indexSearcher.count(booleanQuery.build());
   }
 
   private double calculateLogPrior(Term term, int docsWithClassSize) throws IOException {
diff --git a/lucene/classification/src/java/org/apache/lucene/classification/document/SimpleNaiveBayesDocumentClassifier.java b/lucene/classification/src/java/org/apache/lucene/classification/document/SimpleNaiveBayesDocumentClassifier.java
index 8110c96..d8ff07d 100644
--- a/lucene/classification/src/java/org/apache/lucene/classification/document/SimpleNaiveBayesDocumentClassifier.java
+++ b/lucene/classification/src/java/org/apache/lucene/classification/document/SimpleNaiveBayesDocumentClassifier.java
@@ -40,7 +40,6 @@ import org.apache.lucene.search.BooleanClause;
 import org.apache.lucene.search.BooleanQuery;
 import org.apache.lucene.search.Query;
 import org.apache.lucene.search.TermQuery;
-import org.apache.lucene.search.TotalHitCountCollector;
 import org.apache.lucene.util.BytesRef;
 
 /**
@@ -263,9 +262,7 @@ public class SimpleNaiveBayesDocumentClassifier extends SimpleNaiveBayesClassifi
     if (query != null) {
       booleanQuery.add(query, BooleanClause.Occur.MUST);
     }
-    TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector();
-    indexSearcher.search(booleanQuery.build(), totalHitCountCollector);
-    return totalHitCountCollector.getTotalHits();
+    return indexSearcher.count(booleanQuery.build());
   }
 
   private double calculateLogPrior(Term term, int docsWithClassSize) throws IOException {
diff --git a/lucene/core/src/java/org/apache/lucene/search/TotalHitCountCollector.java b/lucene/core/src/java/org/apache/lucene/search/TotalHitCountCollector.java
index 4d283ea..9d9ad41 100644
--- a/lucene/core/src/java/org/apache/lucene/search/TotalHitCountCollector.java
+++ b/lucene/core/src/java/org/apache/lucene/search/TotalHitCountCollector.java
@@ -16,7 +16,12 @@
  */
 package org.apache.lucene.search;
 
-/** Just counts the total number of hits. */
+/**
+ * Just counts the total number of hits. For cases when this is the only collector used, {@link
+ * IndexSearcher#count(Query)} should be called instead of {@link IndexSearcher#search(Query,
+ * Collector)} as the former is faster whenever the count can be returned directly from the index
+ * statistics.
+ */
 public class TotalHitCountCollector extends SimpleCollector {
   private int totalHits;
 
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestFuzzyTermOnShortTerms.java b/lucene/core/src/test/org/apache/lucene/search/TestFuzzyTermOnShortTerms.java
index 73b755b..c2261ac 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestFuzzyTermOnShortTerms.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestFuzzyTermOnShortTerms.java
@@ -62,9 +62,8 @@ public class TestFuzzyTermOnShortTerms extends LuceneTestCase {
     Directory d = getDirectory(analyzer, docs);
     IndexReader r = DirectoryReader.open(d);
     IndexSearcher s = new IndexSearcher(r);
-    TotalHitCountCollector c = new TotalHitCountCollector();
-    s.search(q, c);
-    assertEquals(q.toString(), expected, c.getTotalHits());
+    int totalHits = s.count(q);
+    assertEquals(q.toString(), expected, totalHits);
     r.close();
     d.close();
   }
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java b/lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java
index 18953ad..c7223a4 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java
@@ -1169,7 +1169,7 @@ public class TestLRUQueryCache extends LuceneTestCase {
     searcher.setQueryCachingPolicy(ALWAYS_CACHE);
 
     BadQuery query = new BadQuery();
-    searcher.count(query);
+    searcher.search(query, new TotalHitCountCollector());
     query.i[0] += 1; // change the hashCode!
 
     try {
diff --git a/lucene/join/src/test/org/apache/lucene/search/join/TestJoinUtil.java b/lucene/join/src/test/org/apache/lucene/search/join/TestJoinUtil.java
index cea1862..ef6e834 100644
--- a/lucene/join/src/test/org/apache/lucene/search/join/TestJoinUtil.java
+++ b/lucene/join/src/test/org/apache/lucene/search/join/TestJoinUtil.java
@@ -681,15 +681,14 @@ public class TestJoinUtil extends LuceneTestCase {
       Query joinQuery =
           JoinUtil.createJoinQuery(
               "join_field", fromQuery, toQuery, searcher, scoreMode, ordinalMap, min, max);
-      TotalHitCountCollector collector = new TotalHitCountCollector();
-      searcher.search(joinQuery, collector);
+      int totalHits = searcher.count(joinQuery);
       int expectedCount = 0;
       for (int numChildDocs : childDocsPerParent) {
         if (numChildDocs >= min && numChildDocs <= max) {
           expectedCount++;
         }
       }
-      assertEquals(expectedCount, collector.getTotalHits());
+      assertEquals(expectedCount, totalHits);
     }
     searcher.getIndexReader().close();
     dir.close();
diff --git a/lucene/spatial-extras/src/test/org/apache/lucene/spatial/prefix/TestHeatmapFacetCounter.java b/lucene/spatial-extras/src/test/org/apache/lucene/spatial/prefix/TestHeatmapFacetCounter.java
index 8a2f72d..b3080d6 100644
--- a/lucene/spatial-extras/src/test/org/apache/lucene/spatial/prefix/TestHeatmapFacetCounter.java
+++ b/lucene/spatial-extras/src/test/org/apache/lucene/spatial/prefix/TestHeatmapFacetCounter.java
@@ -24,7 +24,6 @@ import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 import org.apache.lucene.search.Query;
-import org.apache.lucene.search.TotalHitCountCollector;
 import org.apache.lucene.spatial.StrategyTestCase;
 import org.apache.lucene.spatial.prefix.tree.QuadPrefixTree;
 import org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree;
@@ -282,13 +281,12 @@ public class TestHeatmapFacetCounter extends StrategyTestCase {
     Query filter =
         new IntersectsPrefixTreeQuery(
             pt, strategy.getFieldName(), grid, facetLevel, grid.getMaxLevels());
-    final TotalHitCountCollector collector = new TotalHitCountCollector();
-    indexSearcher.search(filter, collector);
+    int totalHits = indexSearcher.count(filter);
     cellsValidated++;
-    if (collector.getTotalHits() > 0) {
+    if (totalHits > 0) {
       cellValidatedNonZero++;
     }
-    return collector.getTotalHits();
+    return totalHits;
   }
 
   private Shape randomIndexedShape() {