Apache Lucene open-source search software
Introduce DocValuesStatsCollectorManager (#15653)
gaobinlong0210af757c54
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex a6b4913def3..276d20ac039 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -326,6 +326,8 @@ Improvementsbuffer was controlled by an explicit parameter (set as `resultSimilarity` - `traversalSimilarity`), which is nowdeprecated. (Kaival Parikh)+* GITHUB#15653: Introduce DocValuesStatsCollectorManager to parallelize search when using DocValuesStatsCollector. (Binlong Gao)+* GITHUB#15976: Store DocValuesSkipper data in their own file. (Alan Woodward)* GITHUB#15574, GITHUB#15995: Introduce TopGroupsCollectorManager to parallelize search when using TopGroupsCollector. (Binlong Gao)diff --git a/lucene/misc/src/java/org/apache/lucene/misc/search/DocValuesStats.java b/lucene/misc/src/java/org/apache/lucene/misc/search/DocValuesStats.javaindex 88053685878..41c80c238e1 100644--- a/lucene/misc/src/java/org/apache/lucene/misc/search/DocValuesStats.java+++ b/lucene/misc/src/java/org/apache/lucene/misc/search/DocValuesStats.java@@ -28,8 +28,8 @@ import org.apache.lucene.util.BytesRef;/** Holds statistics for a DocValues field. */public abstract class DocValuesStats<T> {- private int missing = 0;- private int count = 0;+ protected int missing = 0;+ protected int count = 0;protected final String field;@@ -73,6 +73,26 @@ public abstract class DocValuesStats<T> {++missing;}+ /**+ * Merges the statistics from {@code other} into this instance. After the call, this instance+ * reflects the combined statistics of both segments as if they had been collected together.+ * Subclasses that track additional statistics (e.g. mean, variance, sum) must override this+ * method, call {@code super.merge(other)}, and combine their own fields accordingly.+ *+ * @param other the stats instance to merge into this one; must be of the same concrete type+ */+ @SuppressWarnings({"unchecked", "rawtypes"})+ protected void merge(DocValuesStats<T> other) {+ count += other.count;+ missing += other.missing;+ if (other.min != null && (min == null || ((Comparable) other.min).compareTo(min) < 0)) {+ min = other.min;+ }+ if (other.max != null && (max == null || ((Comparable) other.max).compareTo(max) > 0)) {+ max = other.max;+ }+ }+/** The field for which these stats were computed. */public final String field() {return field;@@ -142,6 +162,45 @@ public abstract class DocValuesStats<T> {* might overflow.*/public abstract T sum();++ @SuppressWarnings({"unchecked", "rawtypes"})+ @Override+ protected void merge(DocValuesStats<T> other) {+ NumericDocValuesStats<T> otherStats = (NumericDocValuesStats<T>) other;++ this.missing += otherStats.missing();+ if (otherStats.count() == 0) {+ return;+ }+ if (this.count() == 0) {+ this.count = otherStats.count();+ this.min = otherStats.min();+ this.max = otherStats.max();+ this.mean = otherStats.mean();+ this.variance = otherStats.variance;+ return;+ }++ int totalCount = this.count() + otherStats.count();+ double combinedMean = (sum().doubleValue() + otherStats.sum().doubleValue()) / totalCount;+ double targetDelta = this.mean - combinedMean;+ double sourceDelta = otherStats.mean() - combinedMean;++ this.variance =+ this.variance+ + otherStats.variance+ + targetDelta * targetDelta * this.count()+ + sourceDelta * sourceDelta * otherStats.count();+ this.mean = combinedMean;+ this.count = totalCount;++ if (((Comparable) otherStats.min()).compareTo(min()) < 0) {+ this.min = otherStats.min();+ }+ if (((Comparable) otherStats.max()).compareTo(max()) > 0) {+ this.max = otherStats.max();+ }+ }}/** Holds DocValues statistics for a numeric field storing {@code long} values. */@@ -173,6 +232,12 @@ public abstract class DocValuesStats<T> {public Long sum() {return sum;}++ @Override+ protected void merge(DocValuesStats<Long> other) {+ super.merge(other);+ sum += ((NumericDocValuesStats<Long>) other).sum();+ }}/** Holds DocValues statistics for a numeric field storing {@code double} values. */@@ -205,6 +270,12 @@ public abstract class DocValuesStats<T> {public Double sum() {return sum;}++ @Override+ protected void merge(DocValuesStats<Double> other) {+ super.merge(other);+ this.sum += ((NumericDocValuesStats<Double>) other).sum();+ }}/** Holds statistics for a sorted-numeric DocValues field. */@@ -258,6 +329,49 @@ public abstract class DocValuesStats<T> {* might overflow.*/public abstract T sum();++ @SuppressWarnings({"unchecked", "rawtypes"})+ @Override+ protected void merge(DocValuesStats<T> other) {+ SortedNumericDocValuesStats<T> otherStats = (SortedNumericDocValuesStats<T>) other;++ this.missing += otherStats.missing();++ if (otherStats.count() == 0) {+ return;+ }+ if (this.count() == 0) {+ this.count = otherStats.count();+ this.min = otherStats.min();+ this.max = otherStats.max();+ this.mean = otherStats.mean();+ this.variance = otherStats.variance;+ this.valuesCount = otherStats.valuesCount();+ return;+ }++ long totalValuesCount = this.valuesCount + otherStats.valuesCount;+ double combinedMean =+ (sum().doubleValue() + otherStats.sum().doubleValue()) / totalValuesCount;+ double targetDelta = this.mean - combinedMean;+ double sourceDelta = otherStats.mean() - combinedMean;++ this.variance =+ this.variance+ + otherStats.variance+ + targetDelta * targetDelta * this.valuesCount+ + sourceDelta * sourceDelta * otherStats.valuesCount;+ this.mean = combinedMean;+ this.valuesCount = totalValuesCount;+ this.count += otherStats.count();++ if (((Comparable) otherStats.min()).compareTo(min()) < 0) {+ this.min = otherStats.min();+ }+ if (((Comparable) otherStats.max()).compareTo(max()) > 0) {+ this.max = otherStats.max();+ }+ }}/** Holds DocValues statistics for a sorted-numeric field storing {@code long} values. */@@ -296,6 +410,12 @@ public abstract class DocValuesStats<T> {public Long sum() {return sum;}++ @Override+ protected void merge(DocValuesStats<Long> other) {+ super.merge(other);+ sum += ((SortedNumericDocValuesStats<Long>) other).sum();+ }}/** Holds DocValues statistics for a sorted-numeric field storing {@code double} values. */@@ -335,6 +455,12 @@ public abstract class DocValuesStats<T> {public Double sum() {return sum;}++ @Override+ protected void merge(DocValuesStats<Double> other) {+ super.merge(other);+ this.sum += ((SortedNumericDocValuesStats<Double>) other).sum();+ }}private static BytesRef copyFrom(BytesRef src, BytesRef dest) {diff --git a/lucene/misc/src/java/org/apache/lucene/misc/search/DocValuesStatsCollector.java b/lucene/misc/src/java/org/apache/lucene/misc/search/DocValuesStatsCollector.javaindex 29f31651e5d..67ae6d6e8de 100644--- a/lucene/misc/src/java/org/apache/lucene/misc/search/DocValuesStatsCollector.java+++ b/lucene/misc/src/java/org/apache/lucene/misc/search/DocValuesStatsCollector.java@@ -68,4 +68,9 @@ public class DocValuesStatsCollector implements Collector {public ScoreMode scoreMode() {return ScoreMode.COMPLETE_NO_SCORES;}++ /** Returns the statistics computed by this collector. */+ DocValuesStats<?> getStats() {+ return stats;+ }}diff --git a/lucene/misc/src/java/org/apache/lucene/misc/search/DocValuesStatsCollectorManager.java b/lucene/misc/src/java/org/apache/lucene/misc/search/DocValuesStatsCollectorManager.javanew file mode 100644index 00000000000..0ae708e0ef1--- /dev/null+++ b/lucene/misc/src/java/org/apache/lucene/misc/search/DocValuesStatsCollectorManager.java@@ -0,0 +1,69 @@+/*+ * 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.misc.search;++import java.io.IOException;+import java.util.Collection;+import java.util.function.Supplier;+import org.apache.lucene.search.CollectorManager;++/**+ * A {@link CollectorManager} implementation for {@link DocValuesStatsCollector}.+ *+ * <p>Example usage:+ *+ * <pre class="prettyprint">+ * IndexSearcher searcher = ...; // your IndexSearcher+ * DocValuesStatsCollectorManager<Long, DocValuesStats.LongDocValuesStats> manager =+ * new DocValuesStatsCollectorManager<>(() -> new DocValuesStats.LongDocValuesStats("price"));+ * DocValuesStats.LongDocValuesStats stats = searcher.search(new MatchAllDocsQuery(), manager);+ * </pre>+ *+ * @param <T> the type of the DocValues field value+ * @param <S> the type of {@link DocValuesStats}+ */+public class DocValuesStatsCollectorManager<T, S extends DocValuesStats<T>>+ implements CollectorManager<DocValuesStatsCollector, S> {++ private final Supplier<S> statsSupplier;++ /**+ * Creates a new DocValuesStatsCollectorManager.+ *+ * @param statsSupplier a supplier that creates new stats instances for each collector+ */+ public DocValuesStatsCollectorManager(Supplier<S> statsSupplier) {+ this.statsSupplier = statsSupplier;+ }++ @Override+ public DocValuesStatsCollector newCollector() throws IOException {+ return new DocValuesStatsCollector(statsSupplier.get());+ }++ @Override+ @SuppressWarnings("unchecked")+ public S reduce(Collection<DocValuesStatsCollector> collectors) throws IOException {+ S merged = statsSupplier.get();+ for (DocValuesStatsCollector collector : collectors) {+ S stats = (S) collector.getStats();+ merged.merge(stats);+ }++ return merged;+ }+}diff --git a/lucene/misc/src/test/org/apache/lucene/misc/search/TestDocValuesStatsCollector.java b/lucene/misc/src/test/org/apache/lucene/misc/search/TestDocValuesStatsCollector.javaindex d0288183601..dbf86fcaa86 100644--- a/lucene/misc/src/test/org/apache/lucene/misc/search/TestDocValuesStatsCollector.java+++ b/lucene/misc/src/test/org/apache/lucene/misc/search/TestDocValuesStatsCollector.java@@ -35,7 +35,6 @@ import org.apache.lucene.document.SortedSetDocValuesField;import org.apache.lucene.document.StringField;import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.IndexReader;-import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.Term;import org.apache.lucene.misc.search.DocValuesStats.DoubleDocValuesStats;import org.apache.lucene.misc.search.DocValuesStats.LongDocValuesStats;@@ -46,6 +45,7 @@ import org.apache.lucene.misc.search.DocValuesStats.SortedSetDocValuesStats;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.MatchAllDocsQuery;import org.apache.lucene.store.Directory;+import org.apache.lucene.tests.index.RandomIndexWriter;import org.apache.lucene.tests.util.LuceneTestCase;import org.apache.lucene.tests.util.TestUtil;import org.apache.lucene.util.BytesRef;@@ -55,16 +55,18 @@ public class TestDocValuesStatsCollector extends LuceneTestCase {public void testNoDocsWithField() throws IOException {try (Directory dir = newDirectory();- IndexWriter indexWriter = new IndexWriter(dir, newIndexWriterConfig())) {+ RandomIndexWriter indexWriter = new RandomIndexWriter(random(), dir)) {int numDocs = TestUtil.nextInt(random(), 1, 100);for (int i = 0; i < numDocs; i++) {indexWriter.addDocument(new Document());}- try (DirectoryReader reader = DirectoryReader.open(indexWriter)) {- IndexSearcher searcher = new IndexSearcher(reader);- LongDocValuesStats stats = new LongDocValuesStats("foo");- searcher.search(MatchAllDocsQuery.INSTANCE, new DocValuesStatsCollector(stats));+ try (DirectoryReader reader = indexWriter.getReader()) {+ IndexSearcher searcher = newSearcher(reader);+ LongDocValuesStats stats =+ searcher.search(+ MatchAllDocsQuery.INSTANCE,+ new DocValuesStatsCollectorManager<>(() -> new LongDocValuesStats("foo")));assertEquals(0, stats.count());assertEquals(numDocs, stats.missing());@@ -74,17 +76,19 @@ public class TestDocValuesStatsCollector extends LuceneTestCase {public void testOneDoc() throws IOException {try (Directory dir = newDirectory();- IndexWriter indexWriter = new IndexWriter(dir, newIndexWriterConfig())) {+ RandomIndexWriter indexWriter = new RandomIndexWriter(random(), dir)) {String field = "numeric";Document doc = new Document();doc.add(new NumericDocValuesField(field, 1));doc.add(new StringField("id", "doc1", Store.NO));indexWriter.addDocument(doc);- try (DirectoryReader reader = DirectoryReader.open(indexWriter)) {- IndexSearcher searcher = new IndexSearcher(reader);- LongDocValuesStats stats = new LongDocValuesStats(field);- searcher.search(MatchAllDocsQuery.INSTANCE, new DocValuesStatsCollector(stats));+ try (DirectoryReader reader = indexWriter.getReader()) {+ IndexSearcher searcher = newSearcher(reader);+ LongDocValuesStats stats =+ searcher.search(+ MatchAllDocsQuery.INSTANCE,+ new DocValuesStatsCollectorManager<>(() -> new LongDocValuesStats(field)));assertEquals(1, stats.count());assertEquals(0, stats.missing());@@ -99,218 +103,231 @@ public class TestDocValuesStatsCollector extends LuceneTestCase {}public void testDocsWithLongValues() throws IOException {- try (Directory dir = newDirectory();- IndexWriter indexWriter = new IndexWriter(dir, newIndexWriterConfig())) {- String field = "numeric";- int numDocs = TestUtil.nextInt(random(), 1, 100);- long[] docValues = new long[numDocs];- int nextVal = 1;- for (int i = 0; i < numDocs; i++) {- Document doc = new Document();- if (random().nextBoolean()) { // not all documents have a value- doc.add(new NumericDocValuesField(field, nextVal));- doc.add(new StringField("id", "doc" + i, Store.NO));- docValues[i] = nextVal;- ++nextVal;- }- indexWriter.addDocument(doc);- }-- // 20% of cases delete some docs- if (random().nextDouble() < 0.2) {+ try (Directory dir = newDirectory()) {+ try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), dir)) {+ String field = "numeric";+ int numDocs = TestUtil.nextInt(random(), 1, 100);+ long[] docValues = new long[numDocs];+ int nextVal = 1;for (int i = 0; i < numDocs; i++) {+ Document doc = new Document();if (random().nextBoolean()) {- indexWriter.deleteDocuments(new Term("id", "doc" + i));- docValues[i] = 0;+ doc.add(new NumericDocValuesField(field, nextVal));+ doc.add(new StringField("id", "doc" + i, Store.NO));+ docValues[i] = nextVal;+ ++nextVal;}+ indexWriter.addDocument(doc);}- }- try (DirectoryReader reader = DirectoryReader.open(indexWriter)) {- IndexSearcher searcher = new IndexSearcher(reader);- LongDocValuesStats stats = new LongDocValuesStats(field);- searcher.search(MatchAllDocsQuery.INSTANCE, new DocValuesStatsCollector(stats));+ // 20% of cases delete some docs+ if (random().nextDouble() < 0.2) {+ for (int i = 0; i < numDocs; i++) {+ if (random().nextBoolean()) {+ indexWriter.deleteDocuments(new Term("id", "doc" + i));+ docValues[i] = 0;+ }+ }+ }- int expCount = (int) Arrays.stream(docValues).filter(v -> v > 0).count();- assertEquals(expCount, stats.count());- int numDocsWithoutField = (int) getZeroValues(docValues).count();- assertEquals(computeExpMissing(numDocsWithoutField, numDocs, reader), stats.missing());- if (stats.count() > 0) {- LongSummaryStatistics sumStats = getPositiveValues(docValues).summaryStatistics();- assertEquals(sumStats.getMax(), stats.max().longValue());- assertEquals(sumStats.getMin(), stats.min().longValue());- assertEquals(sumStats.getAverage(), stats.mean(), 0.00001);- assertEquals(sumStats.getSum(), stats.sum().longValue());- double variance = computeVariance(docValues, stats.mean, stats.count());- assertEquals(variance, stats.variance(), 0.00001);- assertEquals(Math.sqrt(variance), stats.stdev(), 0.00001);+ try (DirectoryReader reader = indexWriter.getReader()) {+ IndexSearcher searcher = newSearcher(reader);+ LongDocValuesStats stats =+ searcher.search(+ MatchAllDocsQuery.INSTANCE,+ new DocValuesStatsCollectorManager<>(() -> new LongDocValuesStats(field)));++ int expCount = (int) Arrays.stream(docValues).filter(v -> v > 0).count();+ assertEquals(expCount, stats.count());+ int numDocsWithoutField = (int) getZeroValues(docValues).count();+ assertEquals(computeExpMissing(numDocsWithoutField, numDocs, reader), stats.missing());+ if (stats.count() > 0) {+ LongSummaryStatistics sumStats = getPositiveValues(docValues).summaryStatistics();+ assertEquals(sumStats.getMax(), stats.max().longValue());+ assertEquals(sumStats.getMin(), stats.min().longValue());+ assertEquals(sumStats.getAverage(), stats.mean(), 0.00001);+ assertEquals(sumStats.getSum(), stats.sum().longValue());+ double variance = computeVariance(docValues, stats.mean, stats.count());+ assertEquals(variance, stats.variance(), 0.00001);+ assertEquals(Math.sqrt(variance), stats.stdev(), 0.00001);+ }}}}}public void testDocsWithDoubleValues() throws IOException {- try (Directory dir = newDirectory();- IndexWriter indexWriter = new IndexWriter(dir, newIndexWriterConfig())) {- String field = "numeric";- int numDocs = TestUtil.nextInt(random(), 1, 100);- double[] docValues = new double[numDocs];- double nextVal = 1.0;- for (int i = 0; i < numDocs; i++) {- Document doc = new Document();- if (random().nextBoolean()) { // not all documents have a value- doc.add(new DoubleDocValuesField(field, nextVal));- doc.add(new StringField("id", "doc" + i, Store.NO));- docValues[i] = nextVal;- ++nextVal;- }- indexWriter.addDocument(doc);- }-- // 20% of cases delete some docs- if (random().nextDouble() < 0.2) {+ try (Directory dir = newDirectory()) {+ try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), dir)) {+ String field = "numeric";+ int numDocs = TestUtil.nextInt(random(), 1, 100);+ double[] docValues = new double[numDocs];+ double nextVal = 1.0;for (int i = 0; i < numDocs; i++) {+ Document doc = new Document();if (random().nextBoolean()) {- indexWriter.deleteDocuments(new Term("id", "doc" + i));- docValues[i] = 0;+ doc.add(new DoubleDocValuesField(field, nextVal));+ doc.add(new StringField("id", "doc" + i, Store.NO));+ docValues[i] = nextVal;+ ++nextVal;}+ indexWriter.addDocument(doc);}- }- try (DirectoryReader reader = DirectoryReader.open(indexWriter)) {- IndexSearcher searcher = new IndexSearcher(reader);- DoubleDocValuesStats stats = new DoubleDocValuesStats(field);- searcher.search(MatchAllDocsQuery.INSTANCE, new DocValuesStatsCollector(stats));+ // 20% of cases delete some docs+ if (random().nextDouble() < 0.2) {+ for (int i = 0; i < numDocs; i++) {+ if (random().nextBoolean()) {+ indexWriter.deleteDocuments(new Term("id", "doc" + i));+ docValues[i] = 0;+ }+ }+ }- int expCount = (int) Arrays.stream(docValues).filter(v -> v > 0).count();- assertEquals(expCount, stats.count());- int numDocsWithoutField = (int) getZeroValues(docValues).count();- assertEquals(computeExpMissing(numDocsWithoutField, numDocs, reader), stats.missing());- if (stats.count() > 0) {- DoubleSummaryStatistics sumStats = getPositiveValues(docValues).summaryStatistics();- assertEquals(sumStats.getMax(), stats.max().doubleValue(), 0.00001);- assertEquals(sumStats.getMin(), stats.min().doubleValue(), 0.00001);- assertEquals(sumStats.getAverage(), stats.mean(), 0.00001);- assertEquals(sumStats.getSum(), stats.sum(), 0.00001);- double variance = computeVariance(docValues, stats.mean, stats.count());- assertEquals(variance, stats.variance(), 0.00001);- assertEquals(Math.sqrt(variance), stats.stdev(), 0.00001);+ try (DirectoryReader reader = indexWriter.getReader()) {+ IndexSearcher searcher = newSearcher(reader);+ DoubleDocValuesStats stats =+ searcher.search(+ MatchAllDocsQuery.INSTANCE,+ new DocValuesStatsCollectorManager<>(() -> new DoubleDocValuesStats(field)));++ int expCount = (int) Arrays.stream(docValues).filter(v -> v > 0).count();+ assertEquals(expCount, stats.count());+ int numDocsWithoutField = (int) getZeroValues(docValues).count();+ assertEquals(computeExpMissing(numDocsWithoutField, numDocs, reader), stats.missing());+ if (stats.count() > 0) {+ DoubleSummaryStatistics sumStats = getPositiveValues(docValues).summaryStatistics();+ assertEquals(sumStats.getMax(), stats.max().doubleValue(), 0.00001);+ assertEquals(sumStats.getMin(), stats.min().doubleValue(), 0.00001);+ assertEquals(sumStats.getAverage(), stats.mean(), 0.00001);+ assertEquals(sumStats.getSum(), stats.sum(), 0.00001);+ double variance = computeVariance(docValues, stats.mean, stats.count());+ assertEquals(variance, stats.variance(), 0.00001);+ assertEquals(Math.sqrt(variance), stats.stdev(), 0.00001);+ }}}}}public void testDocsWithMultipleLongValues() throws IOException {- try (Directory dir = newDirectory();- IndexWriter indexWriter = new IndexWriter(dir, newIndexWriterConfig())) {- String field = "numeric";- int numDocs = TestUtil.nextInt(random(), 1, 100);- long[][] docValues = new long[numDocs][];- long nextVal = 1;- for (int i = 0; i < numDocs; i++) {- Document doc = new Document();- if (random().nextBoolean()) { // not all documents have a value- int numValues = TestUtil.nextInt(random(), 1, 5);- docValues[i] = new long[numValues];- for (int j = 0; j < numValues; j++) {- doc.add(new SortedNumericDocValuesField(field, nextVal));- docValues[i][j] = nextVal;- ++nextVal;- }- doc.add(new StringField("id", "doc" + i, Store.NO));- }- indexWriter.addDocument(doc);- }-- // 20% of cases delete some docs- if (random().nextDouble() < 0.2) {+ try (Directory dir = newDirectory()) {+ try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), dir)) {+ String field = "numeric";+ int numDocs = TestUtil.nextInt(random(), 1, 100);+ long[][] docValues = new long[numDocs][];+ long nextVal = 1;for (int i = 0; i < numDocs; i++) {+ Document doc = new Document();if (random().nextBoolean()) {- indexWriter.deleteDocuments(new Term("id", "doc" + i));- docValues[i] = null;+ int numValues = TestUtil.nextInt(random(), 1, 5);+ docValues[i] = new long[numValues];+ for (int j = 0; j < numValues; j++) {+ doc.add(new SortedNumericDocValuesField(field, nextVal));+ docValues[i][j] = nextVal;+ ++nextVal;+ }+ doc.add(new StringField("id", "doc" + i, Store.NO));}+ indexWriter.addDocument(doc);}- }- try (DirectoryReader reader = DirectoryReader.open(indexWriter)) {- IndexSearcher searcher = new IndexSearcher(reader);- SortedLongDocValuesStats stats = new SortedLongDocValuesStats(field);- searcher.search(MatchAllDocsQuery.INSTANCE, new DocValuesStatsCollector(stats));+ // 20% of cases delete some docs+ if (random().nextDouble() < 0.2) {+ for (int i = 0; i < numDocs; i++) {+ if (random().nextBoolean()) {+ indexWriter.deleteDocuments(new Term("id", "doc" + i));+ docValues[i] = null;+ }+ }+ }- assertEquals(nonNull(docValues).count(), stats.count());- int numDocsWithoutField = (int) isNull(docValues).count();- assertEquals(computeExpMissing(numDocsWithoutField, numDocs, reader), stats.missing());- if (stats.count() > 0) {- LongSummaryStatistics sumStats =- filterAndFlatValues(docValues, (v) -> v != null).summaryStatistics();- assertEquals(sumStats.getMax(), stats.max().longValue());- assertEquals(sumStats.getMin(), stats.min().longValue());- assertEquals(sumStats.getAverage(), stats.mean(), 0.00001);- assertEquals(sumStats.getSum(), stats.sum().longValue());- assertEquals(sumStats.getCount(), stats.valuesCount());- double variance =- computeVariance(- filterAndFlatValues(docValues, (v) -> v != null), stats.mean, stats.count());- assertEquals(variance, stats.variance(), 0.00001);- assertEquals(Math.sqrt(variance), stats.stdev(), 0.00001);+ try (DirectoryReader reader = indexWriter.getReader()) {+ IndexSearcher searcher = newSearcher(reader);+ SortedLongDocValuesStats stats =+ searcher.search(+ MatchAllDocsQuery.INSTANCE,+ new DocValuesStatsCollectorManager<>(() -> new SortedLongDocValuesStats(field)));++ assertEquals(nonNull(docValues).count(), stats.count());+ int numDocsWithoutField = (int) isNull(docValues).count();+ assertEquals(computeExpMissing(numDocsWithoutField, numDocs, reader), stats.missing());+ if (stats.count() > 0) {+ LongSummaryStatistics sumStats =+ filterAndFlatValues(docValues, (v) -> v != null).summaryStatistics();+ assertEquals(sumStats.getMax(), stats.max().longValue());+ assertEquals(sumStats.getMin(), stats.min().longValue());+ assertEquals(sumStats.getAverage(), stats.mean(), 0.00001);+ assertEquals(sumStats.getSum(), stats.sum().longValue());+ assertEquals(sumStats.getCount(), stats.valuesCount());+ double variance =+ computeVariance(+ filterAndFlatValues(docValues, (v) -> v != null), stats.mean, stats.count());+ assertEquals(variance, stats.variance(), 0.00001);+ assertEquals(Math.sqrt(variance), stats.stdev(), 0.00001);+ }}}}}public void testDocsWithMultipleDoubleValues() throws IOException {- try (Directory dir = newDirectory();- IndexWriter indexWriter = new IndexWriter(dir, newIndexWriterConfig())) {- String field = "numeric";- int numDocs = TestUtil.nextInt(random(), 1, 100);- double[][] docValues = new double[numDocs][];- double nextVal = 1;- for (int i = 0; i < numDocs; i++) {- Document doc = new Document();- if (random().nextBoolean()) { // not all documents have a value- int numValues = TestUtil.nextInt(random(), 1, 5);- docValues[i] = new double[numValues];- for (int j = 0; j < numValues; j++) {- doc.add(new SortedNumericDocValuesField(field, Double.doubleToRawLongBits(nextVal)));- docValues[i][j] = nextVal;- ++nextVal;- }- doc.add(new StringField("id", "doc" + i, Store.NO));- }- indexWriter.addDocument(doc);- }-- // 20% of cases delete some docs- if (random().nextDouble() < 0.2) {+ try (Directory dir = newDirectory()) {+ try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), dir)) {+ String field = "numeric";+ int numDocs = TestUtil.nextInt(random(), 1, 100);+ double[][] docValues = new double[numDocs][];+ double nextVal = 1;for (int i = 0; i < numDocs; i++) {+ Document doc = new Document();if (random().nextBoolean()) {- indexWriter.deleteDocuments(new Term("id", "doc" + i));- docValues[i] = null;+ int numValues = TestUtil.nextInt(random(), 1, 5);+ docValues[i] = new double[numValues];+ for (int j = 0; j < numValues; j++) {+ doc.add(new SortedNumericDocValuesField(field, Double.doubleToRawLongBits(nextVal)));+ docValues[i][j] = nextVal;+ ++nextVal;+ }+ doc.add(new StringField("id", "doc" + i, Store.NO));}+ indexWriter.addDocument(doc);}- }- try (DirectoryReader reader = DirectoryReader.open(indexWriter)) {- IndexSearcher searcher = new IndexSearcher(reader);- SortedDoubleDocValuesStats stats = new SortedDoubleDocValuesStats(field);- searcher.search(MatchAllDocsQuery.INSTANCE, new DocValuesStatsCollector(stats));+ // 20% of cases delete some docs+ if (random().nextDouble() < 0.2) {+ for (int i = 0; i < numDocs; i++) {+ if (random().nextBoolean()) {+ indexWriter.deleteDocuments(new Term("id", "doc" + i));+ docValues[i] = null;+ }+ }+ }- assertEquals(nonNull(docValues).count(), stats.count());- int numDocsWithoutField = (int) isNull(docValues).count();- assertEquals(computeExpMissing(numDocsWithoutField, numDocs, reader), stats.missing());- if (stats.count() > 0) {- DoubleSummaryStatistics sumStats =- filterAndFlatValues(docValues, (v) -> v != null).summaryStatistics();- assertEquals(sumStats.getMax(), stats.max(), 0.00001);- assertEquals(sumStats.getMin(), stats.min(), 0.00001);- assertEquals(sumStats.getAverage(), stats.mean(), 0.00001);- assertEquals(sumStats.getSum(), stats.sum().doubleValue(), 0.00001);- assertEquals(sumStats.getCount(), stats.valuesCount());- double variance =- computeVariance(- filterAndFlatValues(docValues, (v) -> v != null), stats.mean, stats.count());- assertEquals(variance, stats.variance(), 0.00001);- assertEquals(Math.sqrt(variance), stats.stdev(), 0.00001);+ try (DirectoryReader reader = indexWriter.getReader()) {+ IndexSearcher searcher = newSearcher(reader);+ SortedDoubleDocValuesStats stats =+ searcher.search(+ MatchAllDocsQuery.INSTANCE,+ new DocValuesStatsCollectorManager<>(+ () -> new SortedDoubleDocValuesStats(field)));++ assertEquals(nonNull(docValues).count(), stats.count());+ int numDocsWithoutField = (int) isNull(docValues).count();+ assertEquals(computeExpMissing(numDocsWithoutField, numDocs, reader), stats.missing());+ if (stats.count() > 0) {+ DoubleSummaryStatistics sumStats =+ filterAndFlatValues(docValues, (v) -> v != null).summaryStatistics();+ assertEquals(sumStats.getMax(), stats.max(), 0.00001);+ assertEquals(sumStats.getMin(), stats.min(), 0.00001);+ assertEquals(sumStats.getAverage(), stats.mean(), 0.00001);+ assertEquals(sumStats.getSum(), stats.sum().doubleValue(), 0.00001);+ assertEquals(sumStats.getCount(), stats.valuesCount());+ double variance =+ computeVariance(+ filterAndFlatValues(docValues, (v) -> v != null), stats.mean, stats.count());+ assertEquals(variance, stats.variance(), 0.00001);+ assertEquals(Math.sqrt(variance), stats.stdev(), 0.00001);+ }}}}@@ -318,7 +335,7 @@ public class TestDocValuesStatsCollector extends LuceneTestCase {public void testDocsWithSortedValues() throws IOException {try (Directory dir = newDirectory();- IndexWriter indexWriter = new IndexWriter(dir, newIndexWriterConfig())) {+ RandomIndexWriter indexWriter = new RandomIndexWriter(random(), dir)) {String field = "sorted";int numDocs = TestUtil.nextInt(random(), 1, 100);BytesRef[] docValues = new BytesRef[numDocs];@@ -343,10 +360,12 @@ public class TestDocValuesStatsCollector extends LuceneTestCase {}}- try (DirectoryReader reader = DirectoryReader.open(indexWriter)) {- IndexSearcher searcher = new IndexSearcher(reader);- SortedDocValuesStats stats = new SortedDocValuesStats(field);- searcher.search(MatchAllDocsQuery.INSTANCE, new DocValuesStatsCollector(stats));+ try (DirectoryReader reader = indexWriter.getReader()) {+ IndexSearcher searcher = newSearcher(reader);+ SortedDocValuesStats stats =+ searcher.search(+ MatchAllDocsQuery.INSTANCE,+ new DocValuesStatsCollectorManager<>(() -> new SortedDocValuesStats(field)));int expCount = (int) nonNull(docValues).count();assertEquals(expCount, stats.count());@@ -362,7 +381,7 @@ public class TestDocValuesStatsCollector extends LuceneTestCase {public void testDocsWithSortedSetValues() throws IOException {try (Directory dir = newDirectory();- IndexWriter indexWriter = new IndexWriter(dir, newIndexWriterConfig())) {+ RandomIndexWriter indexWriter = new RandomIndexWriter(random(), dir)) {String field = "sorted";int numDocs = TestUtil.nextInt(random(), 1, 100);BytesRef[][] docValues = new BytesRef[numDocs][];@@ -391,11 +410,12 @@ public class TestDocValuesStatsCollector extends LuceneTestCase {}}- try (DirectoryReader reader = DirectoryReader.open(indexWriter)) {- IndexSearcher searcher = new IndexSearcher(reader);- SortedSetDocValuesStats stats = new SortedSetDocValuesStats(field);-- searcher.search(MatchAllDocsQuery.INSTANCE, new DocValuesStatsCollector(stats));+ try (DirectoryReader reader = indexWriter.getReader()) {+ IndexSearcher searcher = newSearcher(reader);+ SortedSetDocValuesStats stats =+ searcher.search(+ MatchAllDocsQuery.INSTANCE,+ new DocValuesStatsCollectorManager<>(() -> new SortedSetDocValuesStats(field)));int expCount = (int) nonNull(docValues).count();assertEquals(expCount, stats.count());