Search in sources :

Example 1 with LongDocValuesStats

use of org.apache.lucene.search.DocValuesStats.LongDocValuesStats in project lucene-solr by apache.

the class TestDocValuesStatsCollector method testOneDoc.

public void testOneDoc() throws IOException {
    try (Directory dir = newDirectory();
        IndexWriter indexWriter = new IndexWriter(dir, newIndexWriterConfig())) {
        String field = "numeric";
        Document doc = new Document();
        doc.add(new NumericDocValuesField(field, 1));
        doc.add(new StringField("id", "doc1", Store.NO));
        indexWriter.addDocument(doc);
        try (DirectoryReader reader = DirectoryReader.open(indexWriter)) {
            IndexSearcher searcher = new IndexSearcher(reader);
            LongDocValuesStats stats = new LongDocValuesStats(field);
            searcher.search(new MatchAllDocsQuery(), new DocValuesStatsCollector(stats));
            assertEquals(1, stats.count());
            assertEquals(0, stats.missing());
            assertEquals(1, stats.max().longValue());
            assertEquals(1, stats.min().longValue());
            assertEquals(1, stats.sum().longValue());
            assertEquals(1, stats.mean(), 0.0001);
            assertEquals(0, stats.variance(), 0.0001);
            assertEquals(0, stats.stdev(), 0.0001);
        }
    }
}
Also used : LongDocValuesStats(org.apache.lucene.search.DocValuesStats.LongDocValuesStats) SortedLongDocValuesStats(org.apache.lucene.search.DocValuesStats.SortedLongDocValuesStats) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) StringField(org.apache.lucene.document.StringField) Document(org.apache.lucene.document.Document) Directory(org.apache.lucene.store.Directory)

Example 2 with LongDocValuesStats

use of org.apache.lucene.search.DocValuesStats.LongDocValuesStats in project lucene-solr by apache.

the class TestDocValuesStatsCollector method testDocsWithLongValues.

public void testDocsWithLongValues() 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];
        int nextVal = 1;
        for (int i = 0; i < numDocs; i++) {
            Document doc = new Document();
            if (random().nextBoolean()) {
                // not all documents have a value
                doc.add(new NumericDocValuesField(field, nextVal));
                doc.add(new StringField("id", "doc" + i, Store.NO));
                docValues[i] = nextVal;
                ++nextVal;
            }
            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] = 0;
                }
            }
        }
        try (DirectoryReader reader = DirectoryReader.open(indexWriter)) {
            IndexSearcher searcher = new IndexSearcher(reader);
            LongDocValuesStats stats = new LongDocValuesStats(field);
            searcher.search(new MatchAllDocsQuery(), new DocValuesStatsCollector(stats));
            int expCount = (int) Arrays.stream(docValues).filter(v -> v > 0).count();
            assertEquals(expCount, stats.count());
            int numDocsWithoutField = (int) getZeroValues(docValues).count();
            assertEquals(computeExpMissing(numDocsWithoutField, numDocs, reader), stats.missing());
            if (stats.count() > 0) {
                LongSummaryStatistics sumStats = getPositiveValues(docValues).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());
                double variance = computeVariance(docValues, stats.mean, stats.count());
                assertEquals(variance, stats.variance(), 0.00001);
                assertEquals(Math.sqrt(variance), stats.stdev(), 0.00001);
            }
        }
    }
}
Also used : LongDocValuesStats(org.apache.lucene.search.DocValuesStats.LongDocValuesStats) SortedLongDocValuesStats(org.apache.lucene.search.DocValuesStats.SortedLongDocValuesStats) DirectoryReader(org.apache.lucene.index.DirectoryReader) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) LongSummaryStatistics(java.util.LongSummaryStatistics) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) IndexWriter(org.apache.lucene.index.IndexWriter) StringField(org.apache.lucene.document.StringField) Directory(org.apache.lucene.store.Directory)

Example 3 with LongDocValuesStats

use of org.apache.lucene.search.DocValuesStats.LongDocValuesStats in project lucene-solr by apache.

the class TestDocValuesStatsCollector method testNoDocsWithField.

public void testNoDocsWithField() throws IOException {
    try (Directory dir = newDirectory();
        IndexWriter indexWriter = new IndexWriter(dir, newIndexWriterConfig())) {
        int numDocs = TestUtil.nextInt(random(), 1, 100);
        for (int i = 0; i < numDocs; i++) {
            indexWriter.addDocument(new Document());
        }
        try (DirectoryReader reader = DirectoryReader.open(indexWriter)) {
            IndexSearcher searcher = new IndexSearcher(reader);
            LongDocValuesStats stats = new LongDocValuesStats("foo");
            searcher.search(new MatchAllDocsQuery(), new DocValuesStatsCollector(stats));
            assertEquals(0, stats.count());
            assertEquals(numDocs, stats.missing());
        }
    }
}
Also used : LongDocValuesStats(org.apache.lucene.search.DocValuesStats.LongDocValuesStats) SortedLongDocValuesStats(org.apache.lucene.search.DocValuesStats.SortedLongDocValuesStats) IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) Document(org.apache.lucene.document.Document) Directory(org.apache.lucene.store.Directory)

Aggregations

Document (org.apache.lucene.document.Document)3 DirectoryReader (org.apache.lucene.index.DirectoryReader)3 IndexWriter (org.apache.lucene.index.IndexWriter)3 LongDocValuesStats (org.apache.lucene.search.DocValuesStats.LongDocValuesStats)3 SortedLongDocValuesStats (org.apache.lucene.search.DocValuesStats.SortedLongDocValuesStats)3 Directory (org.apache.lucene.store.Directory)3 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)2 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)2 StringField (org.apache.lucene.document.StringField)2 LongSummaryStatistics (java.util.LongSummaryStatistics)1 Term (org.apache.lucene.index.Term)1