javanna commented on code in PR #15653:
URL: https://github.com/apache/lucene/pull/15653#discussion_r3217274892


##########
lucene/misc/src/java/org/apache/lucene/misc/search/DocValuesStatsCollectorManager.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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&lt;DocValuesStats.LongDocValuesStats&gt; 
manager =
+ *     new DocValuesStatsCollectorManager&lt;&gt;(() -&gt; new 
DocValuesStats.LongDocValuesStats("price"));
+ * DocValuesStats.LongDocValuesStats stats = searcher.search(new 
MatchAllDocsQuery(), manager);
+ * </pre>
+ *
+ * @param <S> the type of {@link DocValuesStats}

Review Comment:
   can you also add javadocs for  `T`?



##########
lucene/misc/src/java/org/apache/lucene/misc/search/DocValuesStatsCollector.java:
##########
@@ -68,4 +68,9 @@ public void collect(int doc) throws IOException {
   public ScoreMode scoreMode() {
     return ScoreMode.COMPLETE_NO_SCORES;
   }
+
+  /** Returns the statistics computed by this collector. */
+  public DocValuesStats<?> getStats() {

Review Comment:
   does this need to be public? Perhaps this could be package private given 
it's only needed by the collector manager that lives in the same package? 



##########
lucene/misc/src/test/org/apache/lucene/misc/search/TestDocValuesStatsCollector.java:
##########
@@ -99,226 +104,243 @@ public void testOneDoc() throws IOException {
   }
 
   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()) {
+      IndexWriterConfig config = newIndexWriterConfig();
+      try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), 
dir, config)) {
+        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()) {
+      IndexWriterConfig config = newIndexWriterConfig();

Review Comment:
   this seems a leftover, could we remove it and rely on the default index 
writer config in all these tests?



##########
lucene/misc/src/java/org/apache/lucene/misc/search/DocValuesStats.java:
##########
@@ -28,8 +28,8 @@
 /** Holds statistics for a DocValues field. */
 public abstract class DocValuesStats<T> {
 
-  private int missing = 0;
-  private int count = 0;
+  int missing = 0;
+  int count = 0;

Review Comment:
   I find that using package private here is not consistent with the rest of 
the members that we just made protected for potential subclasses within other 
packages to use. I would rather make these also protected, or use the available 
getter, setter etc methods but I think in this case it is not possible.



##########
lucene/misc/src/test/org/apache/lucene/misc/search/TestDocValuesStatsCollector.java:
##########
@@ -99,226 +104,243 @@ public void testOneDoc() throws IOException {
   }
 
   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()) {
+      IndexWriterConfig config = newIndexWriterConfig();
+      try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), 
dir, config)) {
+        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);

Review Comment:
   isn't using `newSearcher` the only change we should be making to all these 
tests?



##########
lucene/misc/src/java/org/apache/lucene/misc/search/DocValuesStatsCollectorManager.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * 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&lt;DocValuesStats.LongDocValuesStats&gt; 
manager =
+ *     new DocValuesStatsCollectorManager&lt;&gt;(() -&gt; new 
DocValuesStats.LongDocValuesStats("price"));
+ * DocValuesStats.LongDocValuesStats stats = searcher.search(new 
MatchAllDocsQuery(), manager);

Review Comment:
   I believe this usage example won't compile.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to