Search in sources :

Example 1 with SortedDocValuesStats

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());
            }
        }
    }
}
Also used : SortedDocValuesStats(org.apache.lucene.search.DocValuesStats.SortedDocValuesStats) DirectoryReader(org.apache.lucene.index.DirectoryReader) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) IndexWriter(org.apache.lucene.index.IndexWriter) StringField(org.apache.lucene.document.StringField) SortedDocValuesField(org.apache.lucene.document.SortedDocValuesField) BytesRef(org.apache.lucene.util.BytesRef) Directory(org.apache.lucene.store.Directory)

Aggregations

Document (org.apache.lucene.document.Document)1 SortedDocValuesField (org.apache.lucene.document.SortedDocValuesField)1 StringField (org.apache.lucene.document.StringField)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 Term (org.apache.lucene.index.Term)1 SortedDocValuesStats (org.apache.lucene.search.DocValuesStats.SortedDocValuesStats)1 Directory (org.apache.lucene.store.Directory)1 BytesRef (org.apache.lucene.util.BytesRef)1