Search in sources :

Example 1 with SortedSetDocValuesStats

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

Aggregations

Document (org.apache.lucene.document.Document)1 SortedSetDocValuesField (org.apache.lucene.document.SortedSetDocValuesField)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 SortedSetDocValuesStats (org.apache.lucene.search.DocValuesStats.SortedSetDocValuesStats)1 Directory (org.apache.lucene.store.Directory)1 BytesRef (org.apache.lucene.util.BytesRef)1