Skip to content

Apache Lucene open-source search software

LUCENE-10070: Skip deleted documents during facet counting for all documents (#293)

goankurdeff5a1f5aa8
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 7be739879ac..c41a58dbac4 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -273,6 +273,8 @@ Improvements
Bug fixes
+* LUCENE-10070 Skip deleted docs when accumulating facet counts for all docs. (Ankur Goel, Greg Miller)
+
* LUCENE-9686: Fix read past EOF handling in DirectIODirectory. (Zach Chen,
Julie Tibshirani)
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/FacetUtils.java b/lucene/facet/src/java/org/apache/lucene/facet/FacetUtils.java
new file mode 100644
index 00000000000..01ec9e4d953
--- /dev/null
+++ b/lucene/facet/src/java/org/apache/lucene/facet/FacetUtils.java
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.facet;
+
+import java.io.IOException;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.util.Bits;
+
+/**
+ * Utility class with a single method for getting a DocIdSetIterator that skips deleted docs
+ *
+ * @lucene.experimental
+ */
+public final class FacetUtils {
+
+ /** Do not instantiate this class */
+ private FacetUtils() {}
+
+ /**
+ * Wrap the given DocIdSetIterator and liveDocs into another DocIdSetIterator that returns
+ * non-deleted documents during iteration. This is useful for computing facet counts on match-all
+ * style queries that need to exclude deleted documents.
+ *
+ * <p>{@link org.apache.lucene.search.ConjunctionUtils} could be better home for this method if we
+ * can identify use cases outside facets module.
+ *
+ * <p>Making this class pkg-private unfortunately limits the visibility of this method to {@link
+ * org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetCounts} and {@link
+ * org.apache.lucene.facet.sortedset.ConcurrentSortedSetDocValuesFacetCounts} classes as Java does
+ * not allow pkg-private classes to be visible to sub-packages.
+ *
+ * @param it {@link DocIdSetIterator} being wrapped
+ * @param liveDocs {@link Bits} containing set bits for non-deleted docs
+ * @return wrapped iterator
+ */
+ public static DocIdSetIterator liveDocsDISI(DocIdSetIterator it, Bits liveDocs) {
+
+ return new DocIdSetIterator() {
+ @Override
+ public int docID() {
+ return it.docID();
+ }
+
+ private int doNext(int doc) throws IOException {
+ assert doc == it.docID();
+ // Find next document that is not deleted until we exhaust all documents
+ while (doc != NO_MORE_DOCS && liveDocs.get(doc) == false) {
+ doc = it.nextDoc();
+ }
+ return doc;
+ }
+
+ @Override
+ public int nextDoc() throws IOException {
+ return doNext(it.nextDoc());
+ }
+
+ @Override
+ public int advance(int target) throws IOException {
+ return doNext(it.advance(target));
+ }
+
+ @Override
+ public long cost() {
+ return it.cost();
+ }
+ };
+ }
+}
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/LongValueFacetCounts.java b/lucene/facet/src/java/org/apache/lucene/facet/LongValueFacetCounts.java
index 56e0286a926..4a4590cbea9 100644
--- a/lucene/facet/src/java/org/apache/lucene/facet/LongValueFacetCounts.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/LongValueFacetCounts.java
@@ -34,6 +34,7 @@ import org.apache.lucene.search.ConjunctionUtils;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.LongValues;
import org.apache.lucene.search.LongValuesSource;
+import org.apache.lucene.util.Bits;
import org.apache.lucene.util.InPlaceMergeSorter;
import org.apache.lucene.util.PriorityQueue;
@@ -201,15 +202,24 @@ public class LongValueFacetCounts extends Facets {
SortedNumericDocValues multiValues = DocValues.getSortedNumeric(context.reader(), field);
NumericDocValues singleValues = DocValues.unwrapSingleton(multiValues);
+ Bits liveDocs = context.reader().getLiveDocs();
+
+ DocIdSetIterator valuesIt = singleValues != null ? singleValues : multiValues;
+ valuesIt = (liveDocs != null) ? FacetUtils.liveDocsDISI(valuesIt, liveDocs) : valuesIt;
+
if (singleValues != null) {
- while (singleValues.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
+ for (int doc = valuesIt.nextDoc();
+ doc != DocIdSetIterator.NO_MORE_DOCS;
+ doc = valuesIt.nextDoc()) {
totCount++;
increment(singleValues.longValue());
}
} else {
- while (multiValues.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
+ for (int doc = valuesIt.nextDoc();
+ doc != DocIdSetIterator.NO_MORE_DOCS;
+ doc = valuesIt.nextDoc()) {
int limit = multiValues.docValueCount();
if (limit > 0) {
totCount++;
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/StringValueFacetCounts.java b/lucene/facet/src/java/org/apache/lucene/facet/StringValueFacetCounts.java
index 8dc223ccba4..4d51fed475c 100644
--- a/lucene/facet/src/java/org/apache/lucene/facet/StringValueFacetCounts.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/StringValueFacetCounts.java
@@ -33,6 +33,7 @@ import org.apache.lucene.index.SortedSetDocValues;
import org.apache.lucene.search.ConjunctionUtils;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.MatchAllDocsQuery;
+import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.LongValues;
@@ -272,7 +273,7 @@ public class StringValueFacetCounts extends Facets {
// Assuming the state is valid, ordinalMap should be null since we have one segment:
assert ordinalMap == null;
- countOneSegment(docValues, hits.context.ord, hits);
+ countOneSegment(docValues, hits.context.ord, hits, null);
} else {
// Validate state before doing anything else. We only check the first segment since they
@@ -291,7 +292,7 @@ public class StringValueFacetCounts extends Facets {
MultiDocValues.MultiSortedSetDocValues multiValues =
(MultiDocValues.MultiSortedSetDocValues) docValues;
- countOneSegment(multiValues.values[i], hits.context.ord, hits);
+ countOneSegment(multiValues.values[i], hits.context.ord, hits, null);
}
}
}
@@ -309,7 +310,7 @@ public class StringValueFacetCounts extends Facets {
assert ordinalMap == null;
LeafReaderContext context = leaves.get(0);
- countOneSegment(docValues, context.ord, null);
+ countOneSegment(docValues, context.ord, null, context.reader().getLiveDocs());
} else {
// Since we have more than one segment, we should have a non-null ordinalMap and docValues
// should be a MultiSortedSetDocValues instance:
@@ -321,13 +322,16 @@ public class StringValueFacetCounts extends Facets {
for (int i = 0; i < numLeaves; i++) {
LeafReaderContext context = leaves.get(i);
- countOneSegment(multiValues.values[i], context.ord, null);
+ countOneSegment(multiValues.values[i], context.ord, null, context.reader().getLiveDocs());
}
}
}
private void countOneSegment(
- SortedSetDocValues multiValues, int segmentOrd, FacetsCollector.MatchingDocs hits)
+ SortedSetDocValues multiValues,
+ int segmentOrd,
+ FacetsCollector.MatchingDocs hits,
+ Bits liveDocs)
throws IOException {
// It's slightly more efficient to work against SortedDocValues if the field is actually
@@ -339,7 +343,7 @@ public class StringValueFacetCounts extends Facets {
// all doc values for this segment:
DocIdSetIterator it;
if (hits == null) {
- it = valuesIt;
+ it = (liveDocs != null) ? FacetUtils.liveDocsDISI(valuesIt, liveDocs) : valuesIt;
} else {
it = ConjunctionUtils.intersectIterators(Arrays.asList(hits.bits.iterator(), valuesIt));
}
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/sortedset/ConcurrentSortedSetDocValuesFacetCounts.java b/lucene/facet/src/java/org/apache/lucene/facet/sortedset/ConcurrentSortedSetDocValuesFacetCounts.java
index 59efa801f41..09b305b9c3e 100644
--- a/lucene/facet/src/java/org/apache/lucene/facet/sortedset/ConcurrentSortedSetDocValuesFacetCounts.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/sortedset/ConcurrentSortedSetDocValuesFacetCounts.java
@@ -29,6 +29,7 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicIntegerArray;
import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.facet.FacetUtils;
import org.apache.lucene.facet.Facets;
import org.apache.lucene.facet.FacetsCollector;
import org.apache.lucene.facet.FacetsCollector.MatchingDocs;
@@ -49,6 +50,7 @@ import org.apache.lucene.index.SortedSetDocValues;
import org.apache.lucene.search.ConjunctionUtils;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.MatchAllDocsQuery;
+import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.LongValues;
@@ -159,13 +161,16 @@ public class ConcurrentSortedSetDocValuesFacetCounts extends Facets {
final MatchingDocs hits;
final OrdinalMap ordinalMap;
final int segOrd;
+ final Bits liveDocs;
public CountOneSegment(
LeafReader leafReader, MatchingDocs hits, OrdinalMap ordinalMap, int segOrd) {
+ assert leafReader != null;
this.leafReader = leafReader;
this.hits = hits;
this.ordinalMap = ordinalMap;
this.segOrd = segOrd;
+ this.liveDocs = leafReader.getLiveDocs();
}
@Override
@@ -193,7 +198,7 @@ public class ConcurrentSortedSetDocValuesFacetCounts extends Facets {
DocIdSetIterator it;
if (hits == null) {
// count all
- it = valuesIt;
+ it = (liveDocs != null) ? FacetUtils.liveDocsDISI(valuesIt, liveDocs) : valuesIt;
} else {
it = ConjunctionUtils.intersectIterators(Arrays.asList(hits.bits.iterator(), valuesIt));
}
diff --git a/lucene/facet/src/java/org/apache/lucene/facet/sortedset/SortedSetDocValuesFacetCounts.java b/lucene/facet/src/java/org/apache/lucene/facet/sortedset/SortedSetDocValuesFacetCounts.java
index f0a573eedda..19ff96ffa52 100644
--- a/lucene/facet/src/java/org/apache/lucene/facet/sortedset/SortedSetDocValuesFacetCounts.java
+++ b/lucene/facet/src/java/org/apache/lucene/facet/sortedset/SortedSetDocValuesFacetCounts.java
@@ -24,6 +24,7 @@ import java.util.Comparator;
import java.util.List;
import java.util.Map;
import org.apache.lucene.facet.FacetResult;
+import org.apache.lucene.facet.FacetUtils;
import org.apache.lucene.facet.Facets;
import org.apache.lucene.facet.FacetsCollector;
import org.apache.lucene.facet.FacetsCollector.MatchingDocs;
@@ -44,6 +45,7 @@ import org.apache.lucene.index.SortedSetDocValues;
import org.apache.lucene.search.ConjunctionUtils;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.MatchAllDocsQuery;
+import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.LongValues;
@@ -152,7 +154,8 @@ public class SortedSetDocValuesFacetCounts extends Facets {
}
private void countOneSegment(
- OrdinalMap ordinalMap, LeafReader reader, int segOrd, MatchingDocs hits) throws IOException {
+ OrdinalMap ordinalMap, LeafReader reader, int segOrd, MatchingDocs hits, Bits liveDocs)
+ throws IOException {
SortedSetDocValues multiValues = DocValues.getSortedSet(reader, field);
if (multiValues == null) {
// nothing to count
@@ -166,7 +169,7 @@ public class SortedSetDocValuesFacetCounts extends Facets {
DocIdSetIterator it;
if (hits == null) {
- it = valuesIt;
+ it = (liveDocs != null) ? FacetUtils.liveDocsDISI(valuesIt, liveDocs) : valuesIt;
} else {
it = ConjunctionUtils.intersectIterators(Arrays.asList(hits.bits.iterator(), valuesIt));
}
@@ -272,7 +275,7 @@ public class SortedSetDocValuesFacetCounts extends Facets {
"the SortedSetDocValuesReaderState provided to this class does not match the reader being searched; you must create a new SortedSetDocValuesReaderState every time you open a new IndexReader");
}
- countOneSegment(ordinalMap, hits.context.reader(), hits.context.ord, hits);
+ countOneSegment(ordinalMap, hits.context.reader(), hits.context.ord, hits, null);
}
}
@@ -291,7 +294,9 @@ public class SortedSetDocValuesFacetCounts extends Facets {
}
for (LeafReaderContext context : state.getReader().leaves()) {
- countOneSegment(ordinalMap, context.reader(), context.ord, null);
+
+ countOneSegment(
+ ordinalMap, context.reader(), context.ord, null, context.reader().getLiveDocs());
}
}
diff --git a/lucene/facet/src/test/org/apache/lucene/facet/TestFacetUtils.java b/lucene/facet/src/test/org/apache/lucene/facet/TestFacetUtils.java
new file mode 100644
index 00000000000..910a329c276
--- /dev/null
+++ b/lucene/facet/src/test/org/apache/lucene/facet/TestFacetUtils.java
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.facet;
+
+import java.io.IOException;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.document.NumericDocValuesField;
+import org.apache.lucene.document.StringField;
+import org.apache.lucene.index.DocValues;
+import org.apache.lucene.index.NumericDocValues;
+import org.apache.lucene.index.RandomIndexWriter;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.search.DocIdSetIterator;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.MatchAllDocsQuery;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.FixedBitSet;
+import org.apache.lucene.util.IOUtils;
+import org.apache.lucene.util.LuceneTestCase;
+
+public class TestFacetUtils extends LuceneTestCase {
+
+ public void testBasic() throws IOException {
+ Directory dir = newDirectory();
+ RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
+ int maxDocs = 256;
+
+ // Create up to maxDocs number of documents with (id, val) fields having
+ // string and numeric representation of the same value
+ for (int i = 0; i < maxDocs; i++) {
+ Document doc = new Document();
+ doc.add(new StringField("id", String.valueOf(i), Field.Store.NO));
+ doc.add(new NumericDocValuesField("val", i));
+ writer.addDocument(doc);
+ }
+
+ // Pick a random number of docs to delete.
+ // Actual deleted docs might be less than this because
+ // of duplicates generated by random()
+ int numDocsToDelete = random().nextInt(maxDocs);
+ FixedBitSet deletedDocs = new FixedBitSet(maxDocs);
+
+ // Delete a random number of randomly picked documents
+ // Record what we deleted to make sure we don't find them
+ // during iteration
+ int actualDocsDeleted = 0;
+ for (int i = 0; i < numDocsToDelete; i++) {
+ int deletedDocId = random().nextInt(maxDocs);
+ if (deletedDocs.get(deletedDocId) == true) {
+ // we already deleted and recorded this doc-id
+ continue;
+ }
+ deletedDocs.set(deletedDocId);
+ writer.deleteDocuments(new Term("id", deletedDocId + ""));
+ actualDocsDeleted++;
+ }
+
+ IndexSearcher searcher = newSearcher(writer.getReader());
+ FacetsCollector fc = new FacetsCollector();
+ searcher.search(new MatchAllDocsQuery(), fc);
+
+ int visitedDocs = 0;
+ DocIdSetIterator disi;
+
+ for (FacetsCollector.MatchingDocs m : fc.getMatchingDocs()) {
+ NumericDocValues numericDV = DocValues.getNumeric(m.context.reader(), "val");
+ Bits liveDocs = m.context.reader().getLiveDocs();
+ // Only use the liveDocsDISI if liveDocs is not null
+ disi = (liveDocs == null) ? numericDV : FacetUtils.liveDocsDISI(numericDV, liveDocs);
+
+ // Iterating over disi should only give values for live docs
+ while (disi.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
+ visitedDocs++;
+ int val = (int) numericDV.longValue();
+ assertTrue("Deleted doc " + val + " found during iteration", deletedDocs.get(val) == false);
+ }
+ }
+
+ assertTrue(
+ "Visited document ["
+ + visitedDocs
+ + "] != Live documents ["
+ + (maxDocs - actualDocsDeleted)
+ + "]",
+ visitedDocs == maxDocs - actualDocsDeleted);
+ writer.close();
+ IOUtils.close(searcher.getIndexReader(), dir);
+ }
+}
diff --git a/lucene/facet/src/test/org/apache/lucene/facet/TestLongValueFacetCounts.java b/lucene/facet/src/test/org/apache/lucene/facet/TestLongValueFacetCounts.java
index aab1c15e6b0..08d70dcbcb7 100644
--- a/lucene/facet/src/test/org/apache/lucene/facet/TestLongValueFacetCounts.java
+++ b/lucene/facet/src/test/org/apache/lucene/facet/TestLongValueFacetCounts.java
@@ -24,11 +24,14 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntPoint;
import org.apache.lucene.document.NumericDocValuesField;
import org.apache.lucene.document.SortedNumericDocValuesField;
+import org.apache.lucene.document.StringField;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.RandomIndexWriter;
+import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.LongValuesSource;
import org.apache.lucene.search.MatchAllDocsQuery;
@@ -71,6 +74,31 @@ public class TestLongValueFacetCounts extends LuceneTestCase {
d.close();
}
+ // See: LUCENE-10070
+ public void testCountAll() throws Exception {
+ Directory d = newDirectory();
+ RandomIndexWriter w = new RandomIndexWriter(random(), d);
+
+ for (int i = 0; i < 10; i++) {
+ Document doc = new Document();
+ doc.add(new StringField("id", String.valueOf(i), Field.Store.NO));
+ doc.add(new NumericDocValuesField("field", i % 2));
+ w.addDocument(doc);
+ }
+
+ w.deleteDocuments(new Term("id", "0"));
+
+ IndexReader r = w.getReader();
+ w.close();
+
+ LongValueFacetCounts facets = new LongValueFacetCounts("field", r);
+
+ FacetResult result = facets.getAllChildrenSortByValue();
+ assertEquals("dim=field path=[] value=9 childCount=2\n 0 (4)\n 1 (5)\n", result.toString());
+ r.close();
+ d.close();
+ }
+
public void testOnlyBigLongs() throws Exception {
Directory d = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), d);
diff --git a/lucene/facet/src/test/org/apache/lucene/facet/TestStringValueFacetCounts.java b/lucene/facet/src/test/org/apache/lucene/facet/TestStringValueFacetCounts.java
index 3d27f28ccf5..2dc4ccc44bf 100644
--- a/lucene/facet/src/test/org/apache/lucene/facet/TestStringValueFacetCounts.java
+++ b/lucene/facet/src/test/org/apache/lucene/facet/TestStringValueFacetCounts.java
@@ -25,10 +25,13 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
import org.apache.lucene.document.SortedDocValuesField;
import org.apache.lucene.document.SortedSetDocValuesField;
+import org.apache.lucene.document.StringField;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.RandomIndexWriter;
+import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.store.Directory;
@@ -74,6 +77,38 @@ public class TestStringValueFacetCounts extends FacetTestCase {
IOUtils.close(searcher.getIndexReader(), dir);
}
+ // See: LUCENE-10070
+ public void testCountAll() throws Exception {
+
+ Directory dir = newDirectory();
+ RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
+
+ Document doc = new Document();
+ doc.add(new StringField("id", "0", Field.Store.NO));
+ doc.add(new SortedSetDocValuesField("field", new BytesRef("foo")));
+ writer.addDocument(doc);
+
+ doc = new Document();
+ doc.add(new StringField("id", "1", Field.Store.NO));
+ doc.add(new SortedSetDocValuesField("field", new BytesRef("foo")));
+ writer.addDocument(doc);
+
+ writer.deleteDocuments(new Term("id", "0"));
+
+ IndexSearcher searcher = newSearcher(writer.getReader());
+ writer.close();
+
+ StringDocValuesReaderState state =
+ new StringDocValuesReaderState(searcher.getIndexReader(), "field");
+
+ StringValueFacetCounts facets = new StringValueFacetCounts(state);
+ assertEquals(
+ "dim=field path=[] value=1 childCount=1\n foo (1)",
+ facets.getTopChildren(10, "field").toString().trim());
+
+ IOUtils.close(searcher.getIndexReader(), dir);
+ }
+
public void testBasicSingleValuedUsingSortedDoc() throws Exception {
Directory dir = newDirectory();
diff --git a/lucene/facet/src/test/org/apache/lucene/facet/sortedset/TestSortedSetDocValuesFacets.java b/lucene/facet/src/test/org/apache/lucene/facet/sortedset/TestSortedSetDocValuesFacets.java
index 59fc41e588b..71b2b6431d1 100644
--- a/lucene/facet/src/test/org/apache/lucene/facet/sortedset/TestSortedSetDocValuesFacets.java
+++ b/lucene/facet/src/test/org/apache/lucene/facet/sortedset/TestSortedSetDocValuesFacets.java
@@ -28,6 +28,7 @@ import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
+import org.apache.lucene.document.StringField;
import org.apache.lucene.facet.DrillDownQuery;
import org.apache.lucene.facet.FacetResult;
import org.apache.lucene.facet.FacetTestCase;
@@ -106,6 +107,58 @@ public class TestSortedSetDocValuesFacets extends FacetTestCase {
IOUtils.close(searcher.getIndexReader(), dir);
}
+ // See: LUCENE-10070
+ public void testCountAll() throws Exception {
+ Directory dir = newDirectory();
+
+ FacetsConfig config = new FacetsConfig();
+ RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
+
+ Document doc = new Document();
+ doc.add(new StringField("id", "0", Field.Store.NO));
+ doc.add(new SortedSetDocValuesFacetField("a", "foo"));
+ writer.addDocument(config.build(doc));
+
+ doc = new Document();
+ doc.add(new StringField("id", "1", Field.Store.NO));
+ doc.add(new SortedSetDocValuesFacetField("a", "bar"));
+ writer.addDocument(config.build(doc));
+
+ writer.deleteDocuments(new Term("id", "0"));
+
+ // NRT open
+ IndexSearcher searcher = newSearcher(writer.getReader());
+
+ // Per-top-reader state:
+ SortedSetDocValuesReaderState state =
+ new DefaultSortedSetDocValuesReaderState(searcher.getIndexReader());
+
+ Facets facets = new SortedSetDocValuesFacetCounts(state);
+
+ assertEquals(
+ "dim=a path=[] value=1 childCount=1\n bar (1)\n",
+ facets.getTopChildren(10, "a").toString());
+
+ ExecutorService exec =
+ new ThreadPoolExecutor(
+ 1,
+ TestUtil.nextInt(random(), 2, 6),
+ Long.MAX_VALUE,
+ TimeUnit.MILLISECONDS,
+ new LinkedBlockingQueue<Runnable>(),
+ new NamedThreadFactory("TestIndexSearcher"));
+
+ facets = new ConcurrentSortedSetDocValuesFacetCounts(state, exec);
+
+ assertEquals(
+ "dim=a path=[] value=1 childCount=1\n bar (1)\n",
+ facets.getTopChildren(10, "a").toString());
+
+ writer.close();
+ IOUtils.close(searcher.getIndexReader(), dir);
+ exec.shutdownNow();
+ }
+
public void testBasicSingleValued() throws Exception {
Directory dir = newDirectory();