Search in sources :

Example 6 with LeafBucketCollectorBase

use of org.opensearch.search.aggregations.LeafBucketCollectorBase in project OpenSearch by opensearch-project.

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);
    final CompensatedSum kahanSummation = new CompensatedSum(0, 0);
    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);
                compensations = bigArrays.resize(compensations, 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);
            }
            if (values.advanceExact(doc)) {
                final int valuesCount = values.docValueCount();
                counts.increment(bucket, valuesCount);
                double min = mins.get(bucket);
                double max = maxes.get(bucket);
                // Compute the sum of double values with Kahan summation algorithm which is more
                // accurate than naive summation.
                double sum = sums.get(bucket);
                double compensation = compensations.get(bucket);
                kahanSummation.reset(sum, compensation);
                for (int i = 0; i < valuesCount; i++) {
                    double value = values.nextValue();
                    kahanSummation.add(value);
                    min = Math.min(min, value);
                    max = Math.max(max, value);
                }
                sums.set(bucket, kahanSummation.value());
                compensations.set(bucket, kahanSummation.delta());
                mins.set(bucket, min);
                maxes.set(bucket, max);
            }
        }
    };
}
Also used : BigArrays(org.opensearch.common.util.BigArrays) LeafBucketCollectorBase(org.opensearch.search.aggregations.LeafBucketCollectorBase) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues)

Example 7 with LeafBucketCollectorBase

use of org.opensearch.search.aggregations.LeafBucketCollectorBase in project OpenSearch by opensearch-project.

the class NumericHistogramAggregator method getLeafCollector.

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

        @Override
        public void collect(int doc, long owningBucketOrd) throws IOException {
            if (values.advanceExact(doc)) {
                final int valuesCount = values.docValueCount();
                double previousKey = Double.NEGATIVE_INFINITY;
                for (int i = 0; i < valuesCount; ++i) {
                    double value = values.nextValue();
                    double key = Math.floor((value - offset) / interval);
                    assert key >= previousKey;
                    if (key == previousKey) {
                        continue;
                    }
                    if (hardBounds == null || hardBounds.contain(key * interval)) {
                        long bucketOrd = bucketOrds.add(owningBucketOrd, Double.doubleToLongBits(key));
                        if (bucketOrd < 0) {
                            // already seen
                            bucketOrd = -1 - bucketOrd;
                            collectExistingBucket(sub, doc, bucketOrd);
                        } else {
                            collectBucket(sub, doc, bucketOrd);
                        }
                    }
                    previousKey = key;
                }
            }
        }
    };
}
Also used : LeafBucketCollectorBase(org.opensearch.search.aggregations.LeafBucketCollectorBase) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues)

Example 8 with LeafBucketCollectorBase

use of org.opensearch.search.aggregations.LeafBucketCollectorBase in project OpenSearch by opensearch-project.

the class ReverseNestedAggregator method getLeafCollector.

@Override
protected LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException {
    // In OpenSearch if parent is deleted, then also the children are deleted, so the child docs this agg receives
    // must belong to parent docs that is alive. For this reason acceptedDocs can be null here.
    final BitSet parentDocs = parentBitsetProducer.getBitSet(ctx);
    if (parentDocs == null) {
        return LeafBucketCollector.NO_OP_COLLECTOR;
    }
    final LongIntHashMap bucketOrdToLastCollectedParentDoc = new LongIntHashMap(32);
    return new LeafBucketCollectorBase(sub, null) {

        @Override
        public void collect(int childDoc, long bucket) throws IOException {
            // fast forward to retrieve the parentDoc this childDoc belongs to
            final int parentDoc = parentDocs.nextSetBit(childDoc);
            assert childDoc <= parentDoc && parentDoc != DocIdSetIterator.NO_MORE_DOCS;
            int keySlot = bucketOrdToLastCollectedParentDoc.indexOf(bucket);
            if (bucketOrdToLastCollectedParentDoc.indexExists(keySlot)) {
                int lastCollectedParentDoc = bucketOrdToLastCollectedParentDoc.indexGet(keySlot);
                if (parentDoc > lastCollectedParentDoc) {
                    collectBucket(sub, parentDoc, bucket);
                    bucketOrdToLastCollectedParentDoc.indexReplace(keySlot, parentDoc);
                }
            } else {
                collectBucket(sub, parentDoc, bucket);
                bucketOrdToLastCollectedParentDoc.indexInsert(keySlot, bucket, parentDoc);
            }
        }
    };
}
Also used : LongIntHashMap(com.carrotsearch.hppc.LongIntHashMap) BitSet(org.apache.lucene.util.BitSet) LeafBucketCollectorBase(org.opensearch.search.aggregations.LeafBucketCollectorBase)

Example 9 with LeafBucketCollectorBase

use of org.opensearch.search.aggregations.LeafBucketCollectorBase in project OpenSearch by opensearch-project.

the class VariableWidthHistogramAggregator method getLeafCollector.

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

        @Override
        public void collect(int doc, long bucket) throws IOException {
            assert bucket == 0;
            if (values.advanceExact(doc)) {
                final int valuesCount = values.docValueCount();
                double prevVal = Double.NEGATIVE_INFINITY;
                for (int i = 0; i < valuesCount; ++i) {
                    double val = values.nextValue();
                    assert val >= prevVal;
                    if (val == prevVal) {
                        continue;
                    }
                    collector = collector.collectValue(sub, doc, val);
                }
            }
        }
    };
}
Also used : LeafBucketCollectorBase(org.opensearch.search.aggregations.LeafBucketCollectorBase) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues)

Example 10 with LeafBucketCollectorBase

use of org.opensearch.search.aggregations.LeafBucketCollectorBase in project OpenSearch by opensearch-project.

the class MinAggregator method getLeafCollector.

@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException {
    if (valuesSource == null) {
        if (parent == null) {
            return LeafBucketCollector.NO_OP_COLLECTOR;
        } else {
            // we have no parent and the values source is empty so we can skip collecting hits.
            throw new CollectionTerminatedException();
        }
    }
    if (pointConverter != null) {
        Number segMin = findLeafMinValue(ctx.reader(), pointField, pointConverter);
        if (segMin != null) {
            /*
                 * There is no parent aggregator (see {@link MinAggregator#getPointReaderOrNull}
                 * so the ordinal for the bucket is always 0.
                 */
            double min = mins.get(0);
            min = Math.min(min, segMin.doubleValue());
            mins.set(0, min);
            // the minimum value has been extracted, we don't need to collect hits on this segment.
            throw new CollectionTerminatedException();
        }
    }
    final BigArrays bigArrays = context.bigArrays();
    final SortedNumericDoubleValues allValues = valuesSource.doubleValues(ctx);
    final NumericDoubleValues values = MultiValueMode.MIN.select(allValues);
    return new LeafBucketCollectorBase(sub, allValues) {

        @Override
        public void collect(int doc, long bucket) throws IOException {
            if (bucket >= mins.size()) {
                long from = mins.size();
                mins = bigArrays.grow(mins, bucket + 1);
                mins.fill(from, mins.size(), Double.POSITIVE_INFINITY);
            }
            if (values.advanceExact(doc)) {
                final double value = values.doubleValue();
                double min = mins.get(bucket);
                min = Math.min(min, value);
                mins.set(bucket, min);
            }
        }
    };
}
Also used : BigArrays(org.opensearch.common.util.BigArrays) CollectionTerminatedException(org.apache.lucene.search.CollectionTerminatedException) LeafBucketCollectorBase(org.opensearch.search.aggregations.LeafBucketCollectorBase) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) NumericDoubleValues(org.opensearch.index.fielddata.NumericDoubleValues) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues)

Aggregations

LeafBucketCollectorBase (org.opensearch.search.aggregations.LeafBucketCollectorBase)27 BigArrays (org.opensearch.common.util.BigArrays)14 SortedNumericDoubleValues (org.opensearch.index.fielddata.SortedNumericDoubleValues)13 SortedNumericDocValues (org.apache.lucene.index.SortedNumericDocValues)4 SortedBinaryDocValues (org.opensearch.index.fielddata.SortedBinaryDocValues)4 BytesRef (org.apache.lucene.util.BytesRef)3 MultiGeoPointValues (org.opensearch.index.fielddata.MultiGeoPointValues)3 NumericDoubleValues (org.opensearch.index.fielddata.NumericDoubleValues)3 CollectionTerminatedException (org.apache.lucene.search.CollectionTerminatedException)2 BitSet (org.apache.lucene.util.BitSet)2 GeoPoint (org.opensearch.common.geo.GeoPoint)2 RangeFieldMapper (org.opensearch.index.mapper.RangeFieldMapper)2 RangeType (org.opensearch.index.mapper.RangeType)2 LongIntHashMap (com.carrotsearch.hppc.LongIntHashMap)1 LongObjectHashMap (com.carrotsearch.hppc.LongObjectHashMap)1 IOException (java.io.IOException)1 DoubleHistogram (org.HdrHistogram.DoubleHistogram)1 IndexReaderContext (org.apache.lucene.index.IndexReaderContext)1 SortedDocValues (org.apache.lucene.index.SortedDocValues)1 SortedSetDocValues (org.apache.lucene.index.SortedSetDocValues)1