use of org.apache.lucene.search.DocValuesStats.SortedLongDocValuesStats in project lucene-solr by apache.
the class TestDocValuesStatsCollector method testDocsWithMultipleLongValues.
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) {
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);
SortedLongDocValuesStats stats = new SortedLongDocValuesStats(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) {
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);
}
}
}
}
Aggregations