Skip to content

Apache Lucene open-source search software

LUCENE-10346: Specially treat SingletonSortedNumericDocValues in FastTaxonomyFacetCounts#countAll() (#574)

gf212126713b3f572c
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index d968eae418c..0aaadf58bfa 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -19,7 +19,8 @@ New Features
Improvements
---------------------
-(No changes)
+
+* LUCENE-10346: Optimize facet counting for single-valued TaxonomyFacetCounts. (Guo Feng)
Optimizations
---------------------
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/FastTaxonomyFacetCounts.java b/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/FastTaxonomyFacetCounts.java
index c3b738c5dfe..439fa09e97d 100644
--- a/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/FastTaxonomyFacetCounts.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/taxonomy/FastTaxonomyFacetCounts.java
@@ -22,8 +22,10 @@ import java.util.List;
import org.apache.lucene.facet.FacetsCollector;
import org.apache.lucene.facet.FacetsCollector.MatchingDocs;
import org.apache.lucene.facet.FacetsConfig;
+import org.apache.lucene.index.DocValues;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.NumericDocValues;
import org.apache.lucene.index.SortedNumericDocValues;
import org.apache.lucene.search.ConjunctionUtils;
import org.apache.lucene.search.DocIdSetIterator;
@@ -96,6 +98,17 @@ public class FastTaxonomyFacetCounts extends IntTaxonomyFacets {
Bits liveDocs = context.reader().getLiveDocs();
+ NumericDocValues ndv = DocValues.unwrapSingleton(dv);
+ if (ndv != null) {
+ for (int doc = ndv.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = ndv.nextDoc()) {
+ if (liveDocs != null && liveDocs.get(doc) == false) {
+ continue;
+ }
+ increment((int) ndv.longValue());
+ }
+ continue;
+ }
+
for (int doc = dv.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; doc = dv.nextDoc()) {
if (liveDocs != null && liveDocs.get(doc) == false) {
continue;