Search in sources :

Example 16 with LeafBucketCollectorBase

use of org.elasticsearch.search.aggregations.LeafBucketCollectorBase in project elasticsearch by elastic.

the class TopHitsAggregator method getLeafCollector.

@Override
public LeafBucketCollector getLeafCollector(final LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException {
    for (LongObjectPagedHashMap.Cursor<TopDocsAndLeafCollector> cursor : topDocsCollectors) {
        cursor.value.leafCollector = cursor.value.topLevelCollector.getLeafCollector(ctx);
    }
    return new LeafBucketCollectorBase(sub, null) {

        Scorer scorer;

        @Override
        public void setScorer(Scorer scorer) throws IOException {
            this.scorer = scorer;
            for (LongObjectPagedHashMap.Cursor<TopDocsAndLeafCollector> cursor : topDocsCollectors) {
                cursor.value.leafCollector.setScorer(scorer);
            }
            super.setScorer(scorer);
        }

        @Override
        public void collect(int docId, long bucket) throws IOException {
            TopDocsAndLeafCollector collectors = topDocsCollectors.get(bucket);
            if (collectors == null) {
                SortAndFormats sort = subSearchContext.sort();
                int topN = subSearchContext.from() + subSearchContext.size();
                if (sort == null) {
                    for (RescoreSearchContext rescoreContext : context.rescore()) {
                        topN = Math.max(rescoreContext.window(), topN);
                    }
                }
                // In the QueryPhase we don't need this protection, because it is build into the IndexSearcher,
                // but here we create collectors ourselves and we need prevent OOM because of crazy an offset and size.
                topN = Math.min(topN, subSearchContext.searcher().getIndexReader().maxDoc());
                TopDocsCollector<?> topLevelCollector;
                if (sort == null) {
                    topLevelCollector = TopScoreDocCollector.create(topN);
                } else {
                    topLevelCollector = TopFieldCollector.create(sort.sort, topN, true, subSearchContext.trackScores(), subSearchContext.trackScores());
                }
                collectors = new TopDocsAndLeafCollector(topLevelCollector);
                collectors.leafCollector = collectors.topLevelCollector.getLeafCollector(ctx);
                collectors.leafCollector.setScorer(scorer);
                topDocsCollectors.put(bucket, collectors);
            }
            collectors.leafCollector.collect(docId);
        }
    };
}
Also used : RescoreSearchContext(org.elasticsearch.search.rescore.RescoreSearchContext) LongObjectPagedHashMap(org.elasticsearch.common.util.LongObjectPagedHashMap) LeafBucketCollectorBase(org.elasticsearch.search.aggregations.LeafBucketCollectorBase) Scorer(org.apache.lucene.search.Scorer) SortAndFormats(org.elasticsearch.search.sort.SortAndFormats)

Example 17 with LeafBucketCollectorBase

use of org.elasticsearch.search.aggregations.LeafBucketCollectorBase in project elasticsearch by elastic.

the class ScriptedMetricAggregator method getLeafCollector.

@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException {
    final LeafSearchScript leafMapScript = mapScript.getLeafSearchScript(ctx);
    return new LeafBucketCollectorBase(sub, mapScript) {

        @Override
        public void collect(int doc, long bucket) throws IOException {
            assert bucket == 0 : bucket;
            leafMapScript.setDocument(doc);
            leafMapScript.run();
        }
    };
}
Also used : LeafSearchScript(org.elasticsearch.script.LeafSearchScript) LeafBucketCollectorBase(org.elasticsearch.search.aggregations.LeafBucketCollectorBase)

Example 18 with LeafBucketCollectorBase

use of org.elasticsearch.search.aggregations.LeafBucketCollectorBase in project elasticsearch by elastic.

the class StatsAggregator method getLeafCollector.

@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException {
    if (valuesSource == null) {
        return LeafBucketCollector.NO_OP_COLLECTOR;
    }
    final BigArrays bigArrays = context.bigArrays();
    final SortedNumericDoubleValues values = valuesSource.doubleValues(ctx);
    return new LeafBucketCollectorBase(sub, values) {

        @Override
        public void collect(int doc, long bucket) throws IOException {
            if (bucket >= counts.size()) {
                final long from = counts.size();
                final long overSize = BigArrays.overSize(bucket + 1);
                counts = bigArrays.resize(counts, overSize);
                sums = bigArrays.resize(sums, overSize);
                mins = bigArrays.resize(mins, overSize);
                maxes = bigArrays.resize(maxes, overSize);
                mins.fill(from, overSize, Double.POSITIVE_INFINITY);
                maxes.fill(from, overSize, Double.NEGATIVE_INFINITY);
            }
            values.setDocument(doc);
            final int valuesCount = values.count();
            counts.increment(bucket, valuesCount);
            double sum = 0;
            double min = mins.get(bucket);
            double max = maxes.get(bucket);
            for (int i = 0; i < valuesCount; i++) {
                double value = values.valueAt(i);
                sum += value;
                min = Math.min(min, value);
                max = Math.max(max, value);
            }
            sums.increment(bucket, sum);
            mins.set(bucket, min);
            maxes.set(bucket, max);
        }
    };
}
Also used : BigArrays(org.elasticsearch.common.util.BigArrays) LeafBucketCollectorBase(org.elasticsearch.search.aggregations.LeafBucketCollectorBase) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues)

Example 19 with LeafBucketCollectorBase

use of org.elasticsearch.search.aggregations.LeafBucketCollectorBase in project elasticsearch by elastic.

the class AvgAggregator method getLeafCollector.

@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException {
    if (valuesSource == null) {
        return LeafBucketCollector.NO_OP_COLLECTOR;
    }
    final BigArrays bigArrays = context.bigArrays();
    final SortedNumericDoubleValues values = valuesSource.doubleValues(ctx);
    return new LeafBucketCollectorBase(sub, values) {

        @Override
        public void collect(int doc, long bucket) throws IOException {
            counts = bigArrays.grow(counts, bucket + 1);
            sums = bigArrays.grow(sums, bucket + 1);
            values.setDocument(doc);
            final int valueCount = values.count();
            counts.increment(bucket, valueCount);
            double sum = 0;
            for (int i = 0; i < valueCount; i++) {
                sum += values.valueAt(i);
            }
            sums.increment(bucket, sum);
        }
    };
}
Also used : BigArrays(org.elasticsearch.common.util.BigArrays) LeafBucketCollectorBase(org.elasticsearch.search.aggregations.LeafBucketCollectorBase) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues)

Example 20 with LeafBucketCollectorBase

use of org.elasticsearch.search.aggregations.LeafBucketCollectorBase in project elasticsearch by elastic.

the class MaxAggregator method getLeafCollector.

@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException {
    if (valuesSource == null) {
        return LeafBucketCollector.NO_OP_COLLECTOR;
    }
    final BigArrays bigArrays = context.bigArrays();
    final SortedNumericDoubleValues allValues = valuesSource.doubleValues(ctx);
    final NumericDoubleValues values = MultiValueMode.MAX.select(allValues, Double.NEGATIVE_INFINITY);
    return new LeafBucketCollectorBase(sub, allValues) {

        @Override
        public void collect(int doc, long bucket) throws IOException {
            if (bucket >= maxes.size()) {
                long from = maxes.size();
                maxes = bigArrays.grow(maxes, bucket + 1);
                maxes.fill(from, maxes.size(), Double.NEGATIVE_INFINITY);
            }
            final double value = values.get(doc);
            double max = maxes.get(bucket);
            max = Math.max(max, value);
            maxes.set(bucket, max);
        }
    };
}
Also used : BigArrays(org.elasticsearch.common.util.BigArrays) LeafBucketCollectorBase(org.elasticsearch.search.aggregations.LeafBucketCollectorBase) NumericDoubleValues(org.elasticsearch.index.fielddata.NumericDoubleValues) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues)

Aggregations

LeafBucketCollectorBase (org.elasticsearch.search.aggregations.LeafBucketCollectorBase)23 BigArrays (org.elasticsearch.common.util.BigArrays)12 SortedNumericDoubleValues (org.elasticsearch.index.fielddata.SortedNumericDoubleValues)10 SortedNumericDocValues (org.apache.lucene.index.SortedNumericDocValues)3 NumericDoubleValues (org.elasticsearch.index.fielddata.NumericDoubleValues)3 Scorer (org.apache.lucene.search.Scorer)2 BitSet (org.apache.lucene.util.BitSet)2 GeoPoint (org.elasticsearch.common.geo.GeoPoint)2 MultiGeoPointValues (org.elasticsearch.index.fielddata.MultiGeoPointValues)2 SortedBinaryDocValues (org.elasticsearch.index.fielddata.SortedBinaryDocValues)2 LongIntHashMap (com.carrotsearch.hppc.LongIntHashMap)1 DoubleHistogram (org.HdrHistogram.DoubleHistogram)1 IndexReaderContext (org.apache.lucene.index.IndexReaderContext)1 SortedDocValues (org.apache.lucene.index.SortedDocValues)1 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 Weight (org.apache.lucene.search.Weight)1 BytesRef (org.apache.lucene.util.BytesRef)1 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)1 LongObjectPagedHashMap (org.elasticsearch.common.util.LongObjectPagedHashMap)1