Search in sources :

Example 86 with Directory

use of org.apache.lucene.store.Directory in project elasticsearch by elastic.

the class MaxAggregatorTests method testCase.

private void testCase(Query query, CheckedConsumer<RandomIndexWriter, IOException> buildIndex, Consumer<InternalMax> verify) throws IOException {
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    buildIndex.accept(indexWriter);
    indexWriter.close();
    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
    MaxAggregationBuilder aggregationBuilder = new MaxAggregationBuilder("_name").field("number");
    MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
    fieldType.setName("number");
    try (MaxAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) {
        aggregator.preCollection();
        indexSearcher.search(query, aggregator);
        aggregator.postCollection();
        verify.accept((InternalMax) aggregator.buildAggregation(0L));
    }
    indexReader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) MaxAggregator(org.elasticsearch.search.aggregations.metrics.max.MaxAggregator) MaxAggregationBuilder(org.elasticsearch.search.aggregations.metrics.max.MaxAggregationBuilder) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 87 with Directory

use of org.apache.lucene.store.Directory in project elasticsearch by elastic.

the class StatsAggregatorTests method testCase.

public void testCase(MappedFieldType ft, CheckedConsumer<RandomIndexWriter, IOException> buildIndex, Consumer<InternalStats> verify) throws IOException {
    try (Directory directory = newDirectory();
        RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
        buildIndex.accept(indexWriter);
        try (IndexReader reader = indexWriter.getReader()) {
            IndexSearcher searcher = new IndexSearcher(reader);
            StatsAggregationBuilder aggBuilder = new StatsAggregationBuilder("my_agg").field("field");
            InternalStats stats = search(searcher, new MatchAllDocsQuery(), aggBuilder, ft);
            verify.accept(stats);
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) StatsAggregationBuilder(org.elasticsearch.search.aggregations.metrics.stats.StatsAggregationBuilder) InternalStats(org.elasticsearch.search.aggregations.metrics.stats.InternalStats) IndexReader(org.apache.lucene.index.IndexReader) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 88 with Directory

use of org.apache.lucene.store.Directory in project elasticsearch by elastic.

the class ValueCountAggregatorTests method testCase.

private void testCase(Query query, ValueType valueType, CheckedConsumer<RandomIndexWriter, IOException> indexer, Consumer<ValueCount> verify) throws IOException {
    try (Directory directory = newDirectory()) {
        try (RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory)) {
            indexer.accept(indexWriter);
        }
        try (IndexReader indexReader = DirectoryReader.open(directory)) {
            IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
            MappedFieldType fieldType = createMappedFieldType(valueType);
            fieldType.setName(FIELD_NAME);
            fieldType.setHasDocValues(true);
            ValueCountAggregationBuilder aggregationBuilder = new ValueCountAggregationBuilder("_name", valueType);
            aggregationBuilder.field(FIELD_NAME);
            try (ValueCountAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) {
                aggregator.preCollection();
                indexSearcher.search(query, aggregator);
                aggregator.postCollection();
                verify.accept((ValueCount) aggregator.buildAggregation(0L));
            }
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 89 with Directory

use of org.apache.lucene.store.Directory in project elasticsearch by elastic.

the class HDRPercentileRanksAggregatorTests method testSimple.

public void testSimple() throws IOException {
    try (Directory dir = newDirectory();
        RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
        for (double value : new double[] { 3, 0.2, 10 }) {
            Document doc = new Document();
            doc.add(new SortedNumericDocValuesField("field", NumericUtils.doubleToSortableLong(value)));
            w.addDocument(doc);
        }
        PercentileRanksAggregationBuilder aggBuilder = new PercentileRanksAggregationBuilder("my_agg").field("field").method(PercentilesMethod.HDR).values(0.1, 0.5, 12);
        MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE);
        fieldType.setName("field");
        try (IndexReader reader = w.getReader()) {
            IndexSearcher searcher = new IndexSearcher(reader);
            PercentileRanks ranks = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType);
            Iterator<Percentile> rankIterator = ranks.iterator();
            Percentile rank = rankIterator.next();
            assertEquals(0.1, rank.getValue(), 0d);
            assertThat(rank.getPercent(), Matchers.equalTo(0d));
            rank = rankIterator.next();
            assertEquals(0.5, rank.getValue(), 0d);
            assertThat(rank.getPercent(), Matchers.greaterThan(0d));
            assertThat(rank.getPercent(), Matchers.lessThan(100d));
            rank = rankIterator.next();
            assertEquals(12, rank.getValue(), 0d);
            assertThat(rank.getPercent(), Matchers.equalTo(100d));
            assertFalse(rankIterator.hasNext());
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Percentile(org.elasticsearch.search.aggregations.metrics.percentiles.Percentile) PercentileRanksAggregationBuilder(org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanksAggregationBuilder) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexReader(org.apache.lucene.index.IndexReader) PercentileRanks(org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 90 with Directory

use of org.apache.lucene.store.Directory in project elasticsearch by elastic.

the class TDigestPercentileRanksAggregatorTests method testSimple.

public void testSimple() throws IOException {
    try (Directory dir = newDirectory();
        RandomIndexWriter w = new RandomIndexWriter(random(), dir)) {
        for (double value : new double[] { 3, 0.2, 10 }) {
            Document doc = new Document();
            doc.add(new SortedNumericDocValuesField("field", NumericUtils.doubleToSortableLong(value)));
            w.addDocument(doc);
        }
        PercentileRanksAggregationBuilder aggBuilder = new PercentileRanksAggregationBuilder("my_agg").field("field").method(PercentilesMethod.TDIGEST).values(0.1, 0.5, 12);
        MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE);
        fieldType.setName("field");
        try (IndexReader reader = w.getReader()) {
            IndexSearcher searcher = new IndexSearcher(reader);
            PercentileRanks ranks = search(searcher, new MatchAllDocsQuery(), aggBuilder, fieldType);
            Iterator<Percentile> rankIterator = ranks.iterator();
            Percentile rank = rankIterator.next();
            assertEquals(0.1, rank.getValue(), 0d);
            // TODO: Fix T-Digest: this assertion should pass but we currently get ~15
            // https://github.com/elastic/elasticsearch/issues/14851
            // assertThat(rank.getPercent(), Matchers.equalTo(0d));
            rank = rankIterator.next();
            assertEquals(0.5, rank.getValue(), 0d);
            assertThat(rank.getPercent(), Matchers.greaterThan(0d));
            assertThat(rank.getPercent(), Matchers.lessThan(100d));
            rank = rankIterator.next();
            assertEquals(12, rank.getValue(), 0d);
            // TODO: Fix T-Digest: this assertion should pass but we currently get ~59
            // https://github.com/elastic/elasticsearch/issues/14851
            // assertThat(rank.getPercent(), Matchers.equalTo(100d));
            assertFalse(rankIterator.hasNext());
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Percentile(org.elasticsearch.search.aggregations.metrics.percentiles.Percentile) PercentileRanksAggregationBuilder(org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanksAggregationBuilder) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) IndexReader(org.apache.lucene.index.IndexReader) PercentileRanks(org.elasticsearch.search.aggregations.metrics.percentiles.PercentileRanks) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Aggregations

Directory (org.apache.lucene.store.Directory)2194 Document (org.apache.lucene.document.Document)1374 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)816 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)669 IndexReader (org.apache.lucene.index.IndexReader)590 IndexSearcher (org.apache.lucene.search.IndexSearcher)383 BytesRef (org.apache.lucene.util.BytesRef)376 RAMDirectory (org.apache.lucene.store.RAMDirectory)360 Term (org.apache.lucene.index.Term)325 StringField (org.apache.lucene.document.StringField)313 IndexWriter (org.apache.lucene.index.IndexWriter)312 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)274 TextField (org.apache.lucene.document.TextField)259 Field (org.apache.lucene.document.Field)257 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)257 Test (org.junit.Test)237 FSDirectory (org.apache.lucene.store.FSDirectory)221 Analyzer (org.apache.lucene.analysis.Analyzer)193 DirectoryReader (org.apache.lucene.index.DirectoryReader)193 TopDocs (org.apache.lucene.search.TopDocs)174