Skip to content

Apache Lucene open-source search software

Disabling point range collector if doc values not indexed (#14559)

Ankit Jain38fc3688c5e3
diff --git a/lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/HistogramCollectorBenchmark.java b/lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/HistogramCollectorBenchmark.java
index 86b2ccc2f17..c2f960fe822 100644
--- a/lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/HistogramCollectorBenchmark.java
+++ b/lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/HistogramCollectorBenchmark.java
@@ -75,6 +75,8 @@ public class HistogramCollectorBenchmark {
if (params.pointEnabled) {
// Adding indexed point field to verify multi range collector
doc.add(new LongPoint("f", value));
+ // Doc values need to be enabled for histogram collection
+ doc.add(NumericDocValuesField.indexedField("f", value));
} else {
doc.add(NumericDocValuesField.indexedField("f", value));
}
diff --git a/lucene/sandbox/src/java/org/apache/lucene/sandbox/facet/plain/histograms/HistogramCollector.java b/lucene/sandbox/src/java/org/apache/lucene/sandbox/facet/plain/histograms/HistogramCollector.java
index 4b77cc7dd64..0abcd32e031 100644
--- a/lucene/sandbox/src/java/org/apache/lucene/sandbox/facet/plain/histograms/HistogramCollector.java
+++ b/lucene/sandbox/src/java/org/apache/lucene/sandbox/facet/plain/histograms/HistogramCollector.java
@@ -73,6 +73,13 @@ final class HistogramCollector implements Collector {
throw new CollectionTerminatedException();
}
+ // Doc values should be indexed to enable histogram collection
+ if (fi.getDocValuesType() != DocValuesType.NUMERIC
+ && fi.getDocValuesType() != DocValuesType.SORTED_NUMERIC) {
+ throw new IllegalStateException(
+ "Expected numeric field, but got doc-value type: " + fi.getDocValuesType());
+ }
+
// We can use multi range traversal logic to collect the histogram on numeric
// field indexed as point for MATCH_ALL cases. In future, this can be extended
// for Point Range Query cases as well
@@ -92,12 +99,6 @@ final class HistogramCollector implements Collector {
}
}
- if (fi.getDocValuesType() != DocValuesType.NUMERIC
- && fi.getDocValuesType() != DocValuesType.SORTED_NUMERIC) {
- throw new IllegalStateException(
- "Expected numeric field, but got doc-value type: " + fi.getDocValuesType());
- }
-
SortedNumericDocValues values = DocValues.getSortedNumeric(context.reader(), field);
NumericDocValues singleton = DocValues.unwrapSingleton(values);
if (singleton == null) {
diff --git a/lucene/sandbox/src/test/org/apache/lucene/sandbox/facet/plain/histograms/TestHistogramCollectorManager.java b/lucene/sandbox/src/test/org/apache/lucene/sandbox/facet/plain/histograms/TestHistogramCollectorManager.java
index 7632031f041..ab817f5c88a 100644
--- a/lucene/sandbox/src/test/org/apache/lucene/sandbox/facet/plain/histograms/TestHistogramCollectorManager.java
+++ b/lucene/sandbox/src/test/org/apache/lucene/sandbox/facet/plain/histograms/TestHistogramCollectorManager.java
@@ -125,23 +125,8 @@ public class TestHistogramCollectorManager extends LuceneTestCase {
public void testMultiRangePointTreeCollector() throws IOException {
Directory dir = newDirectory();
- IndexWriter w = new IndexWriter(dir, new IndexWriterConfig());
-
- long[] values = new long[5000];
-
- for (int i = 0; i < values.length; i++) {
- values[i] = random().nextInt(0, 5000); // Generates a random integer
- }
-
- for (long value : values) {
- Document doc = new Document();
- // Adding indexed point field to verify multi range collector
- doc.add(new LongPoint("f", value));
- w.addDocument(doc);
- }
-
- DirectoryReader reader = DirectoryReader.open(w);
- w.close();
+ long[] values = generateRandomData(5000);
+ DirectoryReader reader = indexNumericData(dir, values, true);
IndexSearcher searcher = newSearcher(reader);
// Validate the MATCH_ALL case
@@ -222,6 +207,50 @@ public class TestHistogramCollectorManager extends LuceneTestCase {
dir.close();
}
+ public void testHistogramCollectorExceptionWithoutDocValues() throws IOException {
+ Directory dir = newDirectory();
+ long[] values = generateRandomData(5000);
+ DirectoryReader reader = indexNumericData(dir, values, false);
+ IndexSearcher searcher = newSearcher(reader);
+
+ // Validate that exception is thrown when doc values is disabled on numeric field
+ expectThrows(
+ IllegalStateException.class,
+ () -> searcher.search(new MatchAllDocsQuery(), new HistogramCollectorManager("f", 1000)));
+
+ reader.close();
+ dir.close();
+ }
+
+ private long[] generateRandomData(int bound) {
+ long[] values = new long[bound];
+ for (int i = 0; i < values.length; i++) {
+ values[i] = random().nextInt(0, bound); // Generates a random integer
+ }
+ return values;
+ }
+
+ private DirectoryReader indexNumericData(Directory dir, long[] values, boolean docValueEnabled)
+ throws IOException {
+ IndexWriter w = new IndexWriter(dir, new IndexWriterConfig());
+
+ for (long value : values) {
+ Document doc = new Document();
+ // Adding indexed point field to verify multi range collector
+ doc.add(new LongPoint("f", value));
+ if (docValueEnabled) {
+ // Doc values need to be enabled for histogram collection
+ doc.add(new NumericDocValuesField("f", value));
+ }
+ w.addDocument(doc);
+ }
+
+ DirectoryReader reader = DirectoryReader.open(w);
+ w.close();
+
+ return reader;
+ }
+
private void doTestSkipIndex(IndexWriterConfig cfg) throws IOException {
Directory dir = newDirectory();
IndexWriter w = new IndexWriter(dir, cfg);