Exemplo n.º 1
0
 public TermsAggregator(
     String name,
     AggregatorFactories factories,
     AggregationContext context,
     Aggregator parent,
     BucketCountThresholds bucketCountThresholds,
     Terms.Order order,
     SubAggCollectionMode collectMode,
     List<Reducer> reducers,
     Map<String, Object> metaData)
     throws IOException {
   super(name, factories, context, parent, reducers, metaData);
   this.bucketCountThresholds = bucketCountThresholds;
   this.order = InternalOrder.validate(order, this);
   this.collectMode = collectMode;
   // Don't defer any child agg if we are dependent on it for pruning results
   if (order instanceof Aggregation) {
     AggregationPath path = ((Aggregation) order).path();
     aggsUsedForSorting.add(path.resolveTopmostAggregator(this));
   } else if (order instanceof CompoundOrder) {
     CompoundOrder compoundOrder = (CompoundOrder) order;
     for (Terms.Order orderElement : compoundOrder.orderElements()) {
       if (orderElement instanceof Aggregation) {
         AggregationPath path = ((Aggregation) orderElement).path();
         aggsUsedForSorting.add(path.resolveTopmostAggregator(this));
       }
     }
   }
 }
  @Override
  public InternalAggregation buildAggregation(long owningBucketOrdinal) {
    assert owningBucketOrdinal == 0;
    List<InternalHistogram.Bucket> buckets =
        new ArrayList<InternalHistogram.Bucket>((int) bucketOrds.size());
    for (long i = 0; i < bucketOrds.capacity(); ++i) {
      final long ord = bucketOrds.id(i);
      if (ord < 0) {
        continue; // slot is not allocated
      }
      buckets.add(
          histogramFactory.createBucket(
              rounding.valueForKey(bucketOrds.key(i)),
              bucketDocCount(ord),
              bucketAggregations(ord),
              valuesSource.formatter()));
    }

    CollectionUtil.introSort(buckets, order.comparator());

    // value source will be null for unmapped fields
    ValueFormatter formatter = valuesSource != null ? valuesSource.formatter() : null;
    InternalHistogram.EmptyBucketInfo emptyBucketInfo =
        minDocCount == 0
            ? new InternalHistogram.EmptyBucketInfo(rounding, buildEmptySubAggregations())
            : null;
    return histogramFactory.create(
        name, buckets, order, minDocCount, emptyBucketInfo, formatter, keyed);
  }