Search in sources :

Example 6 with MinAggregationBuilder

use of org.elasticsearch.search.aggregations.metrics.min.MinAggregationBuilder in project elasticsearch by elastic.

the class SamplerAggregatorTests method testSampler.

/**
     * Uses the sampler aggregation to find the minimum value of a field out of the top 3 scoring documents in a search.
     */
public void testSampler() throws IOException {
    TextFieldType textFieldType = new TextFieldType();
    textFieldType.setIndexAnalyzer(new NamedAnalyzer("foo", AnalyzerScope.GLOBAL, new StandardAnalyzer()));
    MappedFieldType numericFieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
    numericFieldType.setName("int");
    IndexWriterConfig indexWriterConfig = newIndexWriterConfig();
    indexWriterConfig.setMaxBufferedDocs(100);
    // flush on open to have a single segment with predictable docIds
    indexWriterConfig.setRAMBufferSizeMB(100);
    try (Directory dir = newDirectory();
        IndexWriter w = new IndexWriter(dir, indexWriterConfig)) {
        for (long value : new long[] { 7, 3, -10, -6, 5, 50 }) {
            Document doc = new Document();
            StringBuilder text = new StringBuilder();
            for (int i = 0; i < value; i++) {
                text.append("good ");
            }
            doc.add(new Field("text", text.toString(), textFieldType));
            doc.add(new SortedNumericDocValuesField("int", value));
            w.addDocument(doc);
        }
        SamplerAggregationBuilder aggBuilder = new SamplerAggregationBuilder("sampler").shardSize(3).subAggregation(new MinAggregationBuilder("min").field("int"));
        try (IndexReader reader = DirectoryReader.open(w)) {
            assertEquals("test expects a single segment", 1, reader.leaves().size());
            IndexSearcher searcher = new IndexSearcher(reader);
            Sampler sampler = searchAndReduce(searcher, new TermQuery(new Term("text", "good")), aggBuilder, textFieldType, numericFieldType);
            Min min = sampler.getAggregations().get("min");
            assertEquals(5.0, min.getValue(), 0);
        }
    }
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) NamedAnalyzer(org.elasticsearch.index.analysis.NamedAnalyzer) Term(org.apache.lucene.index.Term) TextFieldType(org.elasticsearch.index.mapper.TextFieldMapper.TextFieldType) Document(org.apache.lucene.document.Document) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Field(org.apache.lucene.document.Field) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) Min(org.elasticsearch.search.aggregations.metrics.min.Min) IndexWriter(org.apache.lucene.index.IndexWriter) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) MinAggregationBuilder(org.elasticsearch.search.aggregations.metrics.min.MinAggregationBuilder) IndexReader(org.apache.lucene.index.IndexReader) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Directory(org.apache.lucene.store.Directory)

Example 7 with MinAggregationBuilder

use of org.elasticsearch.search.aggregations.metrics.min.MinAggregationBuilder in project elasticsearch by elastic.

the class GlobalAggregatorTests method testCase.

// Note that `global`'s fancy support for ignoring the query comes from special code in AggregationPhase. We don't test that here.
private void testCase(CheckedConsumer<RandomIndexWriter, IOException> buildIndex, BiConsumer<InternalGlobal, InternalMin> 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);
    GlobalAggregationBuilder aggregationBuilder = new GlobalAggregationBuilder("_name");
    aggregationBuilder.subAggregation(new MinAggregationBuilder("in_global").field("number"));
    MappedFieldType fieldType = new NumberFieldMapper.NumberFieldType(NumberFieldMapper.NumberType.LONG);
    fieldType.setName("number");
    try (GlobalAggregator aggregator = createAggregator(aggregationBuilder, indexSearcher, fieldType)) {
        try {
            aggregator.preCollection();
            indexSearcher.search(new MatchAllDocsQuery(), aggregator);
            aggregator.postCollection();
            InternalGlobal result = (InternalGlobal) aggregator.buildAggregation(0L);
            verify.accept(result, (InternalMin) result.getAggregations().asMap().get("in_global"));
        } finally {
            IOUtils.close(aggregator.subAggregators());
        }
    }
    indexReader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) InternalGlobal(org.elasticsearch.search.aggregations.bucket.global.InternalGlobal) IndexReader(org.apache.lucene.index.IndexReader) MinAggregationBuilder(org.elasticsearch.search.aggregations.metrics.min.MinAggregationBuilder) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) GlobalAggregationBuilder(org.elasticsearch.search.aggregations.bucket.global.GlobalAggregationBuilder) GlobalAggregator(org.elasticsearch.search.aggregations.bucket.global.GlobalAggregator)

Aggregations

MinAggregationBuilder (org.elasticsearch.search.aggregations.metrics.min.MinAggregationBuilder)7 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)3 IndexReader (org.apache.lucene.index.IndexReader)2 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 Directory (org.apache.lucene.store.Directory)2 GlobalAggregationBuilder (org.elasticsearch.search.aggregations.bucket.global.GlobalAggregationBuilder)2 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)1 Document (org.apache.lucene.document.Document)1 Field (org.apache.lucene.document.Field)1 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 Term (org.apache.lucene.index.Term)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 TermQuery (org.apache.lucene.search.TermQuery)1 NamedAnalyzer (org.elasticsearch.index.analysis.NamedAnalyzer)1 TextFieldType (org.elasticsearch.index.mapper.TextFieldMapper.TextFieldType)1 SearchPlugin (org.elasticsearch.plugins.SearchPlugin)1 AggregationSpec (org.elasticsearch.plugins.SearchPlugin.AggregationSpec)1