Skip to content

Commit 794f941

Browse files
javannajpountz
andcommitted
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 <[email protected]>
1 parent bf6a035 commit 794f941

File tree

8 files changed

+18
-28
lines changed

8 files changed

+18
-28
lines changed

lucene/classification/src/java/org/apache/lucene/classification/CachingNaiveBayesClassifier.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.apache.lucene.search.BooleanQuery;
3333
import org.apache.lucene.search.Query;
3434
import org.apache.lucene.search.TermQuery;
35-
import org.apache.lucene.search.TotalHitCountCollector;
3635
import org.apache.lucene.util.BytesRef;
3736

3837
/**
@@ -179,10 +178,8 @@ private Map<BytesRef, Integer> getWordFreqForClassess(String word) throws IOExce
179178
if (query != null) {
180179
booleanQuery.add(query, BooleanClause.Occur.MUST);
181180
}
182-
TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector();
183-
indexSearcher.search(booleanQuery.build(), totalHitCountCollector);
184181

185-
int ret = totalHitCountCollector.getTotalHits();
182+
int ret = indexSearcher.count(booleanQuery.build());
186183
if (ret != 0) {
187184
searched.put(cclass, ret);
188185
}

lucene/classification/src/java/org/apache/lucene/classification/SimpleNaiveBayesClassifier.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.apache.lucene.search.IndexSearcher;
3636
import org.apache.lucene.search.Query;
3737
import org.apache.lucene.search.TermQuery;
38-
import org.apache.lucene.search.TotalHitCountCollector;
3938
import org.apache.lucene.search.WildcardQuery;
4039
import org.apache.lucene.util.BytesRef;
4140

@@ -169,7 +168,6 @@ protected int countDocsWithClass() throws IOException {
169168
Terms terms = MultiTerms.getTerms(this.indexReader, this.classFieldName);
170169
int docCount;
171170
if (terms == null || terms.getDocCount() == -1) { // in case codec doesn't support getDocCount
172-
TotalHitCountCollector classQueryCountCollector = new TotalHitCountCollector();
173171
BooleanQuery.Builder q = new BooleanQuery.Builder();
174172
q.add(
175173
new BooleanClause(
@@ -179,8 +177,7 @@ protected int countDocsWithClass() throws IOException {
179177
if (query != null) {
180178
q.add(query, BooleanClause.Occur.MUST);
181179
}
182-
indexSearcher.search(q.build(), classQueryCountCollector);
183-
docCount = classQueryCountCollector.getTotalHits();
180+
docCount = indexSearcher.count(q.build());
184181
} else {
185182
docCount = terms.getDocCount();
186183
}
@@ -276,9 +273,7 @@ private int getWordFreqForClass(String word, Term term) throws IOException {
276273
if (query != null) {
277274
booleanQuery.add(query, BooleanClause.Occur.MUST);
278275
}
279-
TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector();
280-
indexSearcher.search(booleanQuery.build(), totalHitCountCollector);
281-
return totalHitCountCollector.getTotalHits();
276+
return indexSearcher.count(booleanQuery.build());
282277
}
283278

284279
private double calculateLogPrior(Term term, int docsWithClassSize) throws IOException {

lucene/classification/src/java/org/apache/lucene/classification/document/SimpleNaiveBayesDocumentClassifier.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.apache.lucene.search.BooleanQuery;
4141
import org.apache.lucene.search.Query;
4242
import org.apache.lucene.search.TermQuery;
43-
import org.apache.lucene.search.TotalHitCountCollector;
4443
import org.apache.lucene.util.BytesRef;
4544

4645
/**
@@ -263,9 +262,7 @@ private int getWordFreqForClass(String word, String fieldName, Term term) throws
263262
if (query != null) {
264263
booleanQuery.add(query, BooleanClause.Occur.MUST);
265264
}
266-
TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector();
267-
indexSearcher.search(booleanQuery.build(), totalHitCountCollector);
268-
return totalHitCountCollector.getTotalHits();
265+
return indexSearcher.count(booleanQuery.build());
269266
}
270267

271268
private double calculateLogPrior(Term term, int docsWithClassSize) throws IOException {

lucene/core/src/java/org/apache/lucene/search/TotalHitCountCollector.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616
*/
1717
package org.apache.lucene.search;
1818

19-
/** Just counts the total number of hits. */
19+
/**
20+
* Just counts the total number of hits. For cases when this is the only collector used, {@link
21+
* IndexSearcher#count(Query)} should be called instead of {@link IndexSearcher#search(Query,
22+
* Collector)} as the former is faster whenever the count can be returned directly from the index
23+
* statistics.
24+
*/
2025
public class TotalHitCountCollector extends SimpleCollector {
2126
private int totalHits;
2227

lucene/core/src/test/org/apache/lucene/search/TestFuzzyTermOnShortTerms.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,8 @@ private void countHits(Analyzer analyzer, String[] docs, Query q, int expected)
6262
Directory d = getDirectory(analyzer, docs);
6363
IndexReader r = DirectoryReader.open(d);
6464
IndexSearcher s = new IndexSearcher(r);
65-
TotalHitCountCollector c = new TotalHitCountCollector();
66-
s.search(q, c);
67-
assertEquals(q.toString(), expected, c.getTotalHits());
65+
int totalHits = s.count(q);
66+
assertEquals(q.toString(), expected, totalHits);
6867
r.close();
6968
d.close();
7069
}

lucene/core/src/test/org/apache/lucene/search/TestLRUQueryCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1169,7 +1169,7 @@ public void testDetectMutatedQueries() throws IOException {
11691169
searcher.setQueryCachingPolicy(ALWAYS_CACHE);
11701170

11711171
BadQuery query = new BadQuery();
1172-
searcher.count(query);
1172+
searcher.search(query, new TotalHitCountCollector());
11731173
query.i[0] += 1; // change the hashCode!
11741174

11751175
try {

lucene/join/src/test/org/apache/lucene/search/join/TestJoinUtil.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -681,15 +681,14 @@ public void testMinMaxDocs() throws Exception {
681681
Query joinQuery =
682682
JoinUtil.createJoinQuery(
683683
"join_field", fromQuery, toQuery, searcher, scoreMode, ordinalMap, min, max);
684-
TotalHitCountCollector collector = new TotalHitCountCollector();
685-
searcher.search(joinQuery, collector);
684+
int totalHits = searcher.count(joinQuery);
686685
int expectedCount = 0;
687686
for (int numChildDocs : childDocsPerParent) {
688687
if (numChildDocs >= min && numChildDocs <= max) {
689688
expectedCount++;
690689
}
691690
}
692-
assertEquals(expectedCount, collector.getTotalHits());
691+
assertEquals(expectedCount, totalHits);
693692
}
694693
searcher.getIndexReader().close();
695694
dir.close();

lucene/spatial-extras/src/test/org/apache/lucene/spatial/prefix/TestHeatmapFacetCounter.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.ArrayList;
2525
import java.util.List;
2626
import org.apache.lucene.search.Query;
27-
import org.apache.lucene.search.TotalHitCountCollector;
2827
import org.apache.lucene.spatial.StrategyTestCase;
2928
import org.apache.lucene.spatial.prefix.tree.QuadPrefixTree;
3029
import org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree;
@@ -282,13 +281,12 @@ private int countMatchingDocsAtLevel(Point pt, int facetLevel) throws IOExceptio
282281
Query filter =
283282
new IntersectsPrefixTreeQuery(
284283
pt, strategy.getFieldName(), grid, facetLevel, grid.getMaxLevels());
285-
final TotalHitCountCollector collector = new TotalHitCountCollector();
286-
indexSearcher.search(filter, collector);
284+
int totalHits = indexSearcher.count(filter);
287285
cellsValidated++;
288-
if (collector.getTotalHits() > 0) {
286+
if (totalHits > 0) {
289287
cellValidatedNonZero++;
290288
}
291-
return collector.getTotalHits();
289+
return totalHits;
292290
}
293291

294292
private Shape randomIndexedShape() {

0 commit comments

Comments
 (0)