Search in sources :

Example 11 with Directory

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

the class TermsAggregatorTests method testMixLongAndDouble.

public void testMixLongAndDouble() throws Exception {
    for (TermsAggregatorFactory.ExecutionMode executionMode : TermsAggregatorFactory.ExecutionMode.values()) {
        TermsAggregationBuilder aggregationBuilder = new TermsAggregationBuilder("_name", ValueType.LONG).executionHint(executionMode.toString()).field("number").order(Terms.Order.term(true));
        List<InternalAggregation> aggs = new ArrayList<>();
        int numLongs = randomIntBetween(1, 3);
        for (int i = 0; i < numLongs; i++) {
            final Directory dir;
            try (IndexReader reader = createIndexWithLongs()) {
                dir = ((DirectoryReader) reader).directory();
                IndexSearcher searcher = new IndexSearcher(reader);
                MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
                fieldType.setName("number");
                fieldType.setHasDocValues(true);
                aggs.add(buildInternalAggregation(aggregationBuilder, fieldType, searcher));
            }
            dir.close();
        }
        int numDoubles = randomIntBetween(1, 3);
        for (int i = 0; i < numDoubles; i++) {
            final Directory dir;
            try (IndexReader reader = createIndexWithDoubles()) {
                dir = ((DirectoryReader) reader).directory();
                IndexSearcher searcher = new IndexSearcher(reader);
                MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.DOUBLE);
                fieldType.setName("number");
                fieldType.setHasDocValues(true);
                aggs.add(buildInternalAggregation(aggregationBuilder, fieldType, searcher));
            }
            dir.close();
        }
        InternalAggregation.ReduceContext ctx = new InternalAggregation.ReduceContext(new MockBigArrays(Settings.EMPTY, new NoneCircuitBreakerService()), null, true);
        for (InternalAggregation internalAgg : aggs) {
            InternalAggregation mergedAggs = internalAgg.doReduce(aggs, ctx);
            assertTrue(mergedAggs instanceof DoubleTerms);
            long expected = numLongs + numDoubles;
            List<Terms.Bucket> buckets = ((DoubleTerms) mergedAggs).getBuckets();
            assertEquals(4, buckets.size());
            assertEquals("1.0", buckets.get(0).getKeyAsString());
            assertEquals(expected, buckets.get(0).getDocCount());
            assertEquals("10.0", buckets.get(1).getKeyAsString());
            assertEquals(expected * 2, buckets.get(1).getDocCount());
            assertEquals("100.0", buckets.get(2).getKeyAsString());
            assertEquals(expected * 2, buckets.get(2).getDocCount());
            assertEquals("1000.0", buckets.get(3).getKeyAsString());
            assertEquals(expected, buckets.get(3).getDocCount());
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) ArrayList(java.util.ArrayList) MockBigArrays(org.elasticsearch.common.util.MockBigArrays) InternalAggregation(org.elasticsearch.search.aggregations.InternalAggregation) IndexReader(org.apache.lucene.index.IndexReader) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) Directory(org.apache.lucene.store.Directory) NoneCircuitBreakerService(org.elasticsearch.indices.breaker.NoneCircuitBreakerService)

Example 12 with Directory

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

the class TermsAggregatorTests method createIndexWithLongs.

private IndexReader createIndexWithLongs() throws IOException {
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    Document document = new Document();
    document.add(new SortedNumericDocValuesField("number", 10));
    document.add(new SortedNumericDocValuesField("number", 100));
    indexWriter.addDocument(document);
    document = new Document();
    document.add(new SortedNumericDocValuesField("number", 1));
    document.add(new SortedNumericDocValuesField("number", 100));
    indexWriter.addDocument(document);
    document = new Document();
    document.add(new SortedNumericDocValuesField("number", 10));
    document.add(new SortedNumericDocValuesField("number", 1000));
    indexWriter.addDocument(document);
    indexWriter.close();
    return DirectoryReader.open(directory);
}
Also used : SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Document(org.apache.lucene.document.Document) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 13 with Directory

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

the class TermsAggregatorTests method createIndexWithDoubles.

private IndexReader createIndexWithDoubles() throws IOException {
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    Document document = new Document();
    document.add(new SortedNumericDocValuesField("number", NumericUtils.doubleToSortableLong(10.0d)));
    document.add(new SortedNumericDocValuesField("number", NumericUtils.doubleToSortableLong(100.0d)));
    indexWriter.addDocument(document);
    document = new Document();
    document.add(new SortedNumericDocValuesField("number", NumericUtils.doubleToSortableLong(1.0d)));
    document.add(new SortedNumericDocValuesField("number", NumericUtils.doubleToSortableLong(100.0d)));
    indexWriter.addDocument(document);
    document = new Document();
    document.add(new SortedNumericDocValuesField("number", NumericUtils.doubleToSortableLong(10.0d)));
    document.add(new SortedNumericDocValuesField("number", NumericUtils.doubleToSortableLong(1000.0d)));
    indexWriter.addDocument(document);
    indexWriter.close();
    return DirectoryReader.open(directory);
}
Also used : SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Document(org.apache.lucene.document.Document) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 14 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 15 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)

Aggregations

Directory (org.apache.lucene.store.Directory)2188 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)381 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)309 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)271 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)236 FSDirectory (org.apache.lucene.store.FSDirectory)215 DirectoryReader (org.apache.lucene.index.DirectoryReader)193 Analyzer (org.apache.lucene.analysis.Analyzer)192 FieldType (org.apache.lucene.document.FieldType)173