org.elasticsearch.search.aggregations.LeafBucketCollector Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.LeafBucketCollector. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GlobalOrdinalsStringTermsAggregator.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx,
        final LeafBucketCollector sub) throws IOException {

    globalOrds = valuesSource.globalOrdinalsValues(ctx);

    if (acceptedGlobalOrdinals == null && includeExclude != null) {
        acceptedGlobalOrdinals = includeExclude.acceptedGlobalOrdinals(globalOrds, valuesSource);
    }

    if (acceptedGlobalOrdinals != null) {
        globalOrds = new FilteredOrdinals(globalOrds, acceptedGlobalOrdinals);
    }

    return newCollector(globalOrds, sub);
}
 
Example #2
Source File: AvgAggregator.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@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);
        }
    };
}
 
Example #3
Source File: MissingAggregator.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx,
        final LeafBucketCollector sub) throws IOException {

    final Bits docsWithValue;
    if (valuesSource != null) {
        docsWithValue = valuesSource.docsWithValue(ctx);
    } else {
        docsWithValue = new Bits.MatchNoBits(ctx.reader().maxDoc());
    }
    return new LeafBucketCollectorBase(sub, docsWithValue) {
        @Override
        public void collect(int doc, long bucket) throws IOException {
            if (docsWithValue != null && !docsWithValue.get(doc)) {
                collectBucket(sub, doc, bucket);
            }
        }
    };
}
 
Example #4
Source File: BestBucketsDeferringCollector.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx) throws IOException {
    finishLeaf();

    context = ctx;
    docDeltas = PackedLongValues.packedBuilder(PackedInts.DEFAULT);
    buckets = PackedLongValues.packedBuilder(PackedInts.DEFAULT);

    return new LeafBucketCollector() {
        int lastDoc = 0;

        @Override
        public void collect(int doc, long bucket) throws IOException {
            docDeltas.add(doc - lastDoc);
            buckets.add(bucket);
            lastDoc = doc;
            maxBucket = Math.max(maxBucket, bucket);
        }
    };
}
 
Example #5
Source File: SumAggregator.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@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 {
            sums = bigArrays.grow(sums, bucket + 1);
            values.setDocument(doc);
            final int valuesCount = values.count();
            double sum = 0;
            for (int i = 0; i < valuesCount; i++) {
                sum += values.valueAt(i);
            }
            sums.increment(bucket, sum);
        }
    };
}
 
Example #6
Source File: MaxAggregator.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@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);
        }

    };
}
 
Example #7
Source File: BestDocsDeferringCollector.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public void replayRelatedMatches(ScoreDoc[] sd) throws IOException {
    final LeafBucketCollector leafCollector = deferred.getLeafCollector(readerContext);
    leafCollector.setScorer(this);

    currentScore = 0;
    currentDocId = -1;
    if (maxDocId < 0) {
        return;
    }
    for (ScoreDoc scoreDoc : sd) {
        // Doc ids from TopDocCollector are root-level Reader so
        // need rebasing
        int rebased = scoreDoc.doc - readerContext.docBase;
        if ((rebased >= 0) && (rebased <= maxDocId)) {
            currentScore = scoreDoc.score;
            currentDocId = rebased;
            // We stored the bucket ID in Lucene's shardIndex property
            // for convenience. 
            leafCollector.collect(rebased, scoreDoc.shardIndex);
        }
    }

}
 
Example #8
Source File: ValueCountAggregator.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@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 SortedBinaryDocValues values = valuesSource.bytesValues(ctx);
    return new LeafBucketCollectorBase(sub, values) {

        @Override
        public void collect(int doc, long bucket) throws IOException {
            counts = bigArrays.grow(counts, bucket + 1);
            values.setDocument(doc);
            counts.increment(bucket, values.count());
        }

    };
}
 
Example #9
Source File: MinAggregator.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@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.MIN.select(allValues, Double.POSITIVE_INFINITY);
    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);
            }
            final double value = values.get(doc);
            double min = mins.get(bucket);
            min = Math.min(min, value);
            mins.set(bucket, min);
        }

    };
}
 
Example #10
Source File: BestDocsDeferringCollector.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx) throws IOException {
    perSegCollector = new PerSegmentCollects(ctx);
    entries.add(perSegCollector);

    // Deferring collector
    return new LeafBucketCollector() {
        @Override
        public void setScorer(Scorer scorer) throws IOException {
            perSegCollector.setScorer(scorer);
        }

        @Override
        public void collect(int doc, long bucket) throws IOException {
            perSegCollector.collect(doc, bucket);
        }
    };
}
 
Example #11
Source File: FiltersAggregator.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx,
        final LeafBucketCollector sub) throws IOException {
    // no need to provide deleted docs to the filter
    final Bits[] bits = new Bits[filters.length];
    for (int i = 0; i < filters.length; ++i) {
        bits[i] = Lucene.asSequentialAccessBits(ctx.reader().maxDoc(), filters[i].scorer(ctx));
    }
    return new LeafBucketCollectorBase(sub, null) {
        @Override
        public void collect(int doc, long bucket) throws IOException {
            boolean matched = false;
            for (int i = 0; i < bits.length; i++) {
                if (bits[i].get(doc)) {
                    collectBucket(sub, doc, bucketOrd(bucket, i));
                    matched = true;
                }
            }
            if (showOtherBucket && !matched) {
                collectBucket(sub, doc, bucketOrd(bucket, bits.length));
            }
        }
    };
}
 
Example #12
Source File: GlobalOrdinalsSignificantTermsAggregator.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx,
        final LeafBucketCollector sub) throws IOException {
    return new LeafBucketCollectorBase(super.getLeafCollector(ctx, sub), null) {
        @Override
        public void collect(int doc, long bucket) throws IOException {
            assert bucket == 0;
            numCollectedDocs++;
            globalOrds.setDocument(doc);
            final int numOrds = globalOrds.cardinality();
            for (int i = 0; i < numOrds; i++) {
                final long globalOrd = globalOrds.ordAt(i);
                long bucketOrd = bucketOrds.add(globalOrd);
                if (bucketOrd < 0) {
                    bucketOrd = -1 - bucketOrd;
                    collectExistingBucket(sub, doc, bucketOrd);
                } else {
                    collectBucket(sub, doc, bucketOrd);
                }
            }
        }
    };
}
 
Example #13
Source File: LongTermsAggregator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx,
        final LeafBucketCollector sub) throws IOException {
    final SortedNumericDocValues values = getValues(valuesSource, ctx);
    return new LeafBucketCollectorBase(sub, values) {
        @Override
        public void collect(int doc, long owningBucketOrdinal) throws IOException {
            assert owningBucketOrdinal == 0;
            values.setDocument(doc);
            final int valuesCount = values.count();

            long previous = Long.MAX_VALUE;
            for (int i = 0; i < valuesCount; ++i) {
                final long val = values.valueAt(i);
                if (previous != val || i == 0) {
                    if ((longFilter == null) || (longFilter.accept(val))) {
                        long bucketOrdinal = bucketOrds.add(val);
                        if (bucketOrdinal < 0) { // already seen
                            bucketOrdinal = - 1 - bucketOrdinal;
                            collectExistingBucket(sub, doc, bucketOrdinal);
                        } else {
                            collectBucket(sub, doc, bucketOrdinal);
                        }
                    }

                    previous = val;
                }
            }
        }
    };
}
 
Example #14
Source File: FilterAggregator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx,
        final LeafBucketCollector sub) throws IOException {
    // no need to provide deleted docs to the filter
    final Bits bits = Lucene.asSequentialAccessBits(ctx.reader().maxDoc(), filter.scorer(ctx));
    return new LeafBucketCollectorBase(sub, null) {
        @Override
        public void collect(int doc, long bucket) throws IOException {
            if (bits.get(doc)) {
                collectBucket(sub, doc, bucket);
            }
        }
    };
}
 
Example #15
Source File: GlobalOrdinalsStringTermsAggregator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
protected LeafBucketCollector newCollector(final RandomAccessOrds ords, final LeafBucketCollector sub) {
    grow(ords.getValueCount());
    final SortedDocValues singleValues = DocValues.unwrapSingleton(ords);
    if (singleValues != null) {
        return new LeafBucketCollectorBase(sub, ords) {
            @Override
            public void collect(int doc, long bucket) throws IOException {
                assert bucket == 0;
                final int ord = singleValues.getOrd(doc);
                if (ord >= 0) {
                    collectExistingBucket(sub, doc, ord);
                }
            }
        };
    } else {
        return new LeafBucketCollectorBase(sub, ords) {
            @Override
            public void collect(int doc, long bucket) throws IOException {
                assert bucket == 0;
                ords.setDocument(doc);
                final int numOrds = ords.cardinality();
                for (int i = 0; i < numOrds; i++) {
                    final long globalOrd = ords.ordAt(i);
                    collectExistingBucket(sub, doc, globalOrd);
                }
            }
        };
    }
}
 
Example #16
Source File: GlobalOrdinalsStringTermsAggregator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected LeafBucketCollector newCollector(final RandomAccessOrds ords, LeafBucketCollector sub) {
    segmentDocCounts = context.bigArrays().grow(segmentDocCounts, 1 + ords.getValueCount());
    assert sub == LeafBucketCollector.NO_OP_COLLECTOR;
    final SortedDocValues singleValues = DocValues.unwrapSingleton(ords);
    if (singleValues != null) {
        return new LeafBucketCollectorBase(sub, ords) {
            @Override
            public void collect(int doc, long bucket) throws IOException {
                assert bucket == 0;
                final int ord = singleValues.getOrd(doc);
                segmentDocCounts.increment(ord + 1, 1);
            }
        };
    } else {
        return new LeafBucketCollectorBase(sub, ords) {
            @Override
            public void collect(int doc, long bucket) throws IOException {
                assert bucket == 0;
                ords.setDocument(doc);
                final int numOrds = ords.cardinality();
                for (int i = 0; i < numOrds; i++) {
                    final long segmentOrd = ords.ordAt(i);
                    segmentDocCounts.increment(segmentOrd + 1, 1);
                }
            }
        };
    }
}
 
Example #17
Source File: GlobalOrdinalsStringTermsAggregator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx,
        final LeafBucketCollector sub) throws IOException {
    if (segmentOrds != null) {
        mapSegmentCountsToGlobalCounts();
    }

    globalOrds = valuesSource.globalOrdinalsValues(ctx);
    segmentOrds = valuesSource.ordinalsValues(ctx);
    return newCollector(segmentOrds, sub);
}
 
Example #18
Source File: GlobalAggregator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx,
        final LeafBucketCollector sub) throws IOException {
    return new LeafBucketCollectorBase(sub, null) {
        @Override
        public void collect(int doc, long bucket) throws IOException {
            assert bucket == 0 : "global aggregator can only be a top level aggregator";
            collectBucket(sub, doc, bucket);
        }
    };
}
 
Example #19
Source File: GeoHashGridAggregator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx,
        final LeafBucketCollector sub) throws IOException {
    final SortedNumericDocValues values = valuesSource.longValues(ctx);
    return new LeafBucketCollectorBase(sub, null) {
        @Override
        public void collect(int doc, long bucket) throws IOException {
            assert bucket == 0;
            values.setDocument(doc);
            final int valuesCount = values.count();

            long previous = Long.MAX_VALUE;
            for (int i = 0; i < valuesCount; ++i) {
                final long val = values.valueAt(i);
                if (previous != val || i == 0) {
                    long bucketOrdinal = bucketOrds.add(val);
                    if (bucketOrdinal < 0) { // already seen
                        bucketOrdinal = - 1 - bucketOrdinal;
                        collectExistingBucket(sub, doc, bucketOrdinal);
                    } else {
                        collectBucket(sub, doc, bucketOrdinal);
                    }
                    previous = val;
                }
            }
        }
    };
}
 
Example #20
Source File: HistogramAggregator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx,
        final LeafBucketCollector sub) throws IOException {
    if (valuesSource == null) {
        return LeafBucketCollector.NO_OP_COLLECTOR;
    }
    final SortedNumericDocValues values = valuesSource.longValues(ctx);
    return new LeafBucketCollectorBase(sub, values) {
        @Override
        public void collect(int doc, long bucket) throws IOException {
            assert bucket == 0;
            values.setDocument(doc);
            final int valuesCount = values.count();

            long previousKey = Long.MIN_VALUE;
            for (int i = 0; i < valuesCount; ++i) {
                long value = values.valueAt(i);
                long key = rounding.roundKey(value);
                assert key >= previousKey;
                if (key == previousKey) {
                    continue;
                }
                long bucketOrd = bucketOrds.add(key);
                if (bucketOrd < 0) { // already seen
                    bucketOrd = -1 - bucketOrd;
                    collectExistingBucket(sub, doc, bucketOrd);
                } else {
                    collectBucket(sub, doc, bucketOrd);
                }
                previousKey = key;
            }
        }
    };
}
 
Example #21
Source File: ReverseNestedAggregator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException {
    // In ES 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);
            }
        }
    };
}
 
Example #22
Source File: StringTermsAggregator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx,
        final LeafBucketCollector sub) throws IOException {
    final SortedBinaryDocValues values = valuesSource.bytesValues(ctx);
    return new LeafBucketCollectorBase(sub, values) {
        final BytesRefBuilder previous = new BytesRefBuilder();

        @Override
        public void collect(int doc, long bucket) throws IOException {
            assert bucket == 0;
            values.setDocument(doc);
            final int valuesCount = values.count();

            // SortedBinaryDocValues don't guarantee uniqueness so we need to take care of dups
            previous.clear();
            for (int i = 0; i < valuesCount; ++i) {
                final BytesRef bytes = values.valueAt(i);
                if (includeExclude != null && !includeExclude.accept(bytes)) {
                    continue;
                }
                if (previous.get().equals(bytes)) {
                    continue;
                }
                long bucketOrdinal = bucketOrds.add(bytes);
                if (bucketOrdinal < 0) { // already seen
                    bucketOrdinal = - 1 - bucketOrdinal;
                    collectExistingBucket(sub, doc, bucketOrdinal);
                } else {
                    collectBucket(sub, doc, bucketOrdinal);
                }
                previous.copyBytes(bytes);
            }
        }
    };
}
 
Example #23
Source File: ParentToChildrenAggregator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected void doPostCollection() throws IOException {
    IndexReader indexReader = context().searchContext().searcher().getIndexReader();
    for (LeafReaderContext ctx : indexReader.leaves()) {
        Scorer childDocsScorer = childFilter.scorer(ctx);
        if (childDocsScorer == null) {
            continue;
        }
        DocIdSetIterator childDocsIter = childDocsScorer.iterator();

        final LeafBucketCollector sub = collectableSubAggregators.getLeafCollector(ctx);
        final SortedDocValues globalOrdinals = valuesSource.globalOrdinalsValues(parentType, ctx);

        // Set the scorer, since we now replay only the child docIds
        sub.setScorer(ConstantScorer.create(childDocsIter, null, 1f));

        final Bits liveDocs = ctx.reader().getLiveDocs();
        for (int docId = childDocsIter.nextDoc(); docId != DocIdSetIterator.NO_MORE_DOCS; docId = childDocsIter.nextDoc()) {
            if (liveDocs != null && liveDocs.get(docId) == false) {
                continue;
            }
            long globalOrdinal = globalOrdinals.getOrd(docId);
            if (globalOrdinal != -1) {
                long bucketOrd = parentOrdToBuckets.get(globalOrdinal);
                if (bucketOrd != -1) {
                    collectBucket(sub, docId, bucketOrd);
                    if (multipleBucketsPerParentOrd) {
                        long[] otherBucketOrds = parentOrdToOtherBuckets.get(globalOrdinal);
                        if (otherBucketOrds != null) {
                            for (long otherBucketOrd : otherBucketOrds) {
                                collectBucket(sub, docId, otherBucketOrd);
                            }
                        }
                    }
                }
            }
        }
    }
}
 
Example #24
Source File: ParentToChildrenAggregator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx,
        final LeafBucketCollector sub) throws IOException {
    if (valuesSource == null) {
        return LeafBucketCollector.NO_OP_COLLECTOR;
    }

    final SortedDocValues globalOrdinals = valuesSource.globalOrdinalsValues(parentType, ctx);
    assert globalOrdinals != null;
    Scorer parentScorer = parentFilter.scorer(ctx);
    final Bits parentDocs = Lucene.asSequentialAccessBits(ctx.reader().maxDoc(), parentScorer);
    return new LeafBucketCollector() {

        @Override
        public void collect(int docId, long bucket) throws IOException {
            if (parentDocs.get(docId)) {
                long globalOrdinal = globalOrdinals.getOrd(docId);
                if (globalOrdinal != -1) {
                    if (parentOrdToBuckets.get(globalOrdinal) == -1) {
                        parentOrdToBuckets.set(globalOrdinal, bucket);
                    } else {
                        long[] bucketOrds = parentOrdToOtherBuckets.get(globalOrdinal);
                        if (bucketOrds != null) {
                            bucketOrds = Arrays.copyOf(bucketOrds, bucketOrds.length + 1);
                            bucketOrds[bucketOrds.length - 1] = bucket;
                            parentOrdToOtherBuckets.put(globalOrdinal, bucketOrds);
                        } else {
                            parentOrdToOtherBuckets.put(globalOrdinal, new long[]{bucket});
                        }
                        multipleBucketsPerParentOrd = true;
                    }
                }
            }
        }
    };
}
 
Example #25
Source File: DateHierarchyAggregator.java    From elasticsearch-aggregation-pathhierarchy with MIT License 5 votes vote down vote up
/**
 * The collector collects the docs, including or not some score (depending of the including of a Scorer) in the
 * collect() process.
 *
 * The LeafBucketCollector is a "Per-leaf bucket collector". It collects docs for the account of buckets.
 */
@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, LeafBucketCollector sub) throws IOException {
    if (valuesSource == null) {
        return LeafBucketCollector.NO_OP_COLLECTOR;
    }
    final SortedNumericDocValues values = valuesSource.longValues(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();

                for (int i = 0; i < valuesCount; ++i) {
                    long value = values.nextValue();
                    String path = "";
                    for (DateHierarchyAggregationBuilder.RoundingInfo roundingInfo: roundingsInfo) {
                        // A little hacky: Add a microsecond to avoid collision between min dates interval
                        // Since interval cannot be set to microsecond, it is not a problem
                        long roundedValue = roundingInfo.rounding.round(value);
                        path += roundingInfo.format.format(roundedValue).toString();
                        long bucketOrd = bucketOrds.add(new BytesRef(path));
                        if (bucketOrd < 0) { // already seen
                            bucketOrd = -1 - bucketOrd;
                            collectExistingBucket(sub, doc, bucketOrd);
                        } else {
                            collectBucket(sub, doc, bucketOrd);
                        }
                        path += "/";
                    }
                }
            }
        }
    };
}
 
Example #26
Source File: PathHierarchyAggregator.java    From elasticsearch-aggregation-pathhierarchy with MIT License 5 votes vote down vote up
/**
 * The collector collects the docs, including or not some score (depending of the including of a Scorer) in the
 * collect() process.
 *
 * The LeafBucketCollector is a "Per-leaf bucket collector". It collects docs for the account of buckets.
 */
@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, LeafBucketCollector sub) throws IOException {
    if (valuesSource == null) {
        return LeafBucketCollector.NO_OP_COLLECTOR;
    }
    final SortedBinaryDocValues values = valuesSource.bytesValues(ctx);
    return new LeafBucketCollectorBase(sub, values) {
        final BytesRefBuilder previous = new BytesRefBuilder();
        /**
         * Collect the given doc in the given bucket.
         * Called once for every document matching a query, with the unbased document number.
         */
        @Override
        public void collect(int doc, long owningBucketOrdinal) throws IOException {
            assert owningBucketOrdinal == 0;
            if (values.advanceExact(doc)) {
                final int valuesCount = values.docValueCount();
                previous.clear();

                // SortedBinaryDocValues don't guarantee uniqueness so we need to take care of dups
                for (int i = 0; i < valuesCount; ++i) {
                    final BytesRef bytesValue = values.nextValue();
                    if (i > 0 && previous.get().equals(bytesValue)) {
                        continue;
                    }
                    long bucketOrdinal = bucketOrds.add(bytesValue);
                    if (bucketOrdinal < 0) { // already seen
                        bucketOrdinal = - 1 - bucketOrdinal;
                        collectExistingBucket(sub, doc, bucketOrdinal);
                    } else {
                        collectBucket(sub, doc, bucketOrdinal);
                    }
                    previous.copyBytes(bytesValue);
                }
            }
        }
    };
}
 
Example #27
Source File: SignificantLongTermsAggregator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx,
        final LeafBucketCollector sub) throws IOException {
    return new LeafBucketCollectorBase(super.getLeafCollector(ctx, sub), null) {
        @Override
        public void collect(int doc, long bucket) throws IOException {
            super.collect(doc, bucket);
            numCollectedDocs++;
        }
    };
}
 
Example #28
Source File: GlobalOrdinalsSignificantTermsAggregator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx,
        final LeafBucketCollector sub) throws IOException {
    return new LeafBucketCollectorBase(super.getLeafCollector(ctx, sub), null) {
        @Override
        public void collect(int doc, long bucket) throws IOException {
            super.collect(doc, bucket);
            numCollectedDocs++;
        }
    };
}
 
Example #29
Source File: SignificantStringTermsAggregator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx,
        final LeafBucketCollector sub) throws IOException {
    return new LeafBucketCollectorBase(super.getLeafCollector(ctx, sub), null) {
        @Override
        public void collect(int doc, long bucket) throws IOException {
            super.collect(doc, bucket);
            numCollectedDocs++;
        }
    };
}
 
Example #30
Source File: GeoCentroidAggregator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, LeafBucketCollector sub) throws IOException {
    if (valuesSource == null) {
        return LeafBucketCollector.NO_OP_COLLECTOR;
    }
    final BigArrays bigArrays = context.bigArrays();
    final MultiGeoPointValues values = valuesSource.geoPointValues(ctx);
    return new LeafBucketCollectorBase(sub, values) {
        @Override
        public void collect(int doc, long bucket) throws IOException {
            centroids = bigArrays.grow(centroids, bucket + 1);
            counts = bigArrays.grow(counts, bucket + 1);

            values.setDocument(doc);
            final int valueCount = values.count();
            if (valueCount > 0) {
                double[] pt = new double[2];
                // get the previously accumulated number of counts
                long prevCounts = counts.get(bucket);
                // increment by the number of points for this document
                counts.increment(bucket, valueCount);
                // get the previous GeoPoint if a moving avg was computed
                if (prevCounts > 0) {
                    final GeoPoint centroid = GeoPoint.fromIndexLong(centroids.get(bucket));
                    pt[0] = centroid.lon();
                    pt[1] = centroid.lat();
                }
                // update the moving average
                for (int i = 0; i < valueCount; ++i) {
                    GeoPoint value = values.valueAt(i);
                    pt[0] = pt[0] + (value.getLon() - pt[0]) / ++prevCounts;
                    pt[1] = pt[1] + (value.getLat() - pt[1]) / prevCounts;
                }
                centroids.set(bucket, GeoEncodingUtils.mortonHash(pt[0], pt[1]));
            }
        }
    };
}