use of org.apache.lucene.search.DocValuesStats.SortedSetDocValuesStats in project lucene-solr by apache.
the class TestDocValuesStatsCollector method testDocsWithSortedSetValues.
public void testDocsWithSortedSetValues() 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
int numValues = TestUtil.nextInt(random(), 1, 5);
docValues[i] = new BytesRef[numValues];
for (int j = 0; j < numValues; j++) {
BytesRef val = TestUtil.randomBinaryTerm(random());
doc.add(new SortedSetDocValuesField(field, val));
docValues[i][j] = val;
}
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);
SortedSetDocValuesStats stats = new SortedSetDocValuesStats(field);
TotalHitCountCollector totalHitCount = new TotalHitCountCollector();
searcher.search(new MatchAllDocsQuery(), MultiCollector.wrap(totalHitCount, 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).flatMap(Arrays::stream).min(BytesRef::compareTo).get(), stats.min());
assertEquals(nonNull(docValues).flatMap(Arrays::stream).max(BytesRef::compareTo).get(), stats.max());
}
}
}
}
Aggregations