public boolean nextPosition() {
   if (++position >= values.ordinals().getNumOrds()) {
     return false;
   }
   current = values.getValueByOrd(position);
   return true;
 }
Esempio n. 2
0
 @Override
 public void collect(int doc) throws IOException {
   if (values != null) {
     long ord = ordinals.getOrd(doc);
     long parentIdx = parentIdsIndex.get(ord);
     if (parentIdx < 0) {
       final BytesRef bytes = values.getValueByOrd(ord);
       final int hash = values.currentValueHash();
       parentIdx = parentIds.add(bytes, hash);
       if (parentIdx < 0) {
         parentIdx = -parentIdx - 1;
         doScore(parentIdx);
       } else {
         scores = bigArrays.grow(scores, parentIdx + 1);
         scores.set(parentIdx, scorer.score());
       }
       parentIdsIndex.set(ord, parentIdx);
     } else {
       doScore(parentIdx);
     }
   }
 }
  @Override
  public InternalAggregation buildAggregation(long owningBucketOrdinal) {
    if (globalOrdinals == null) { // no context in this reader
      return buildEmptyAggregation();
    }

    final int size;
    if (bucketCountThresholds.getMinDocCount() == 0) {
      // if minDocCount == 0 then we can end up with more buckets then maxBucketOrd() returns
      size = (int) Math.min(globalOrdinals.getMaxOrd(), bucketCountThresholds.getShardSize());
    } else {
      size = (int) Math.min(maxBucketOrd(), bucketCountThresholds.getShardSize());
    }
    BucketPriorityQueue ordered = new BucketPriorityQueue(size, order.comparator(this));
    OrdBucket spare = new OrdBucket(-1, 0, null);
    for (long globalTermOrd = Ordinals.MIN_ORDINAL;
        globalTermOrd < globalOrdinals.getMaxOrd();
        ++globalTermOrd) {
      if (includeExclude != null && !acceptedGlobalOrdinals.get(globalTermOrd)) {
        continue;
      }
      final long bucketOrd = getBucketOrd(globalTermOrd);
      final long bucketDocCount = bucketOrd < 0 ? 0 : bucketDocCount(bucketOrd);
      if (bucketCountThresholds.getMinDocCount() > 0 && bucketDocCount == 0) {
        continue;
      }
      spare.globalOrd = globalTermOrd;
      spare.bucketOrd = bucketOrd;
      spare.docCount = bucketDocCount;
      if (bucketCountThresholds.getShardMinDocCount() <= spare.docCount) {
        spare = (OrdBucket) ordered.insertWithOverflow(spare);
        if (spare == null) {
          spare = new OrdBucket(-1, 0, null);
        }
      }
    }

    // Get the top buckets
    final InternalTerms.Bucket[] list = new InternalTerms.Bucket[ordered.size()];
    long survivingBucketOrds[] = new long[ordered.size()];
    for (int i = ordered.size() - 1; i >= 0; --i) {
      final OrdBucket bucket = (OrdBucket) ordered.pop();
      survivingBucketOrds[i] = bucket.bucketOrd;
      BytesRef scratch = new BytesRef();
      copy(globalValues.getValueByOrd(bucket.globalOrd), scratch);
      list[i] = new StringTerms.Bucket(scratch, bucket.docCount, null);
      list[i].bucketOrd = bucket.bucketOrd;
    }
    // replay any deferred collections
    runDeferredCollections(survivingBucketOrds);
    // Now build the aggs
    for (int i = 0; i < list.length; i++) {
      Bucket bucket = list[i];
      bucket.aggregations =
          bucket.docCount == 0 ? bucketEmptyAggregations() : bucketAggregations(bucket.bucketOrd);
    }

    return new StringTerms(
        name,
        order,
        bucketCountThresholds.getRequiredSize(),
        bucketCountThresholds.getMinDocCount(),
        Arrays.asList(list));
  }