use of org.apache.lucene.search.DocValuesStats.SortedDoubleDocValuesStats in project lucene-solr by apache.
the class TestDocValuesStatsCollector method testDocsWithMultipleDoubleValues.
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) {
for (int i = 0; i < numDocs; i++) {
if (random().nextBoolean()) {
indexWriter.deleteDocuments(new Term("id", "doc" + i));
docValues[i] = null;
}
}
}
try (DirectoryReader reader = DirectoryReader.open(indexWriter)) {
IndexSearcher searcher = new IndexSearcher(reader);
SortedDoubleDocValuesStats stats = new SortedDoubleDocValuesStats(field);
searcher.search(new MatchAllDocsQuery(), new DocValuesStatsCollector(stats));
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().longValue(), 0.00001);
assertEquals(sumStats.getMin(), stats.min().longValue(), 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);
}
}
}
}
Aggregations