use of org.apache.lucene.search.DocValuesStats.SortedDocValuesStats in project lucene-solr by apache.
the class TestDocValuesStatsCollector method testDocsWithSortedValues.
public void testDocsWithSortedValues() throws IOException {
try (Directory dir = newDirectory();
IndexWriter indexWriter = new IndexWriter(dir, newIndexWriterConfig())) {
String field = "sorted";
int numDocs = TestUtil.nextInt(random(), 1, 100);
BytesRef[] docValues = new BytesRef[numDocs];
for (int i = 0; i < numDocs; i++) {
Document doc = new Document();
if (random().nextBoolean()) {
// not all documents have a value
BytesRef val = TestUtil.randomBinaryTerm(random());
doc.add(new SortedDocValuesField(field, val));
doc.add(new StringField("id", "doc" + i, Store.NO));
docValues[i] = val;
}
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);
SortedDocValuesStats stats = new SortedDocValuesStats(field);
searcher.search(new MatchAllDocsQuery(), new DocValuesStatsCollector(stats));
int expCount = (int) nonNull(docValues).count();
assertEquals(expCount, stats.count());
int numDocsWithoutField = (int) isNull(docValues).count();
assertEquals(computeExpMissing(numDocsWithoutField, numDocs, reader), stats.missing());
if (stats.count() > 0) {
assertEquals(nonNull(docValues).min(BytesRef::compareTo).get(), stats.min());
assertEquals(nonNull(docValues).max(BytesRef::compareTo).get(), stats.max());
}
}
}
}
Aggregations