org.elasticsearch.search.aggregations.bucket.BucketUtils Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.bucket.BucketUtils. 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: PathHierarchyAggregatorFactory.java    From elasticsearch-aggregation-pathhierarchy with MIT License 6 votes vote down vote up
@Override
protected Aggregator doCreateInternal(
        ValuesSource valuesSource, SearchContext searchContext, Aggregator parent,
        boolean collectsFromSingleBucket, List<PipelineAggregator> pipelineAggregators,
        Map<String, Object> metaData) throws IOException {

    ValuesSource valuesSourceBytes = new HierarchyValuesSource(valuesSource, separator, minDepth, maxDepth, keepBlankPath);
    PathHierarchyAggregator.BucketCountThresholds bucketCountThresholds = new
            PathHierarchyAggregator.BucketCountThresholds(this.bucketCountThresholds);
    if (!InternalOrder.isKeyOrder(order)
            && bucketCountThresholds.getShardSize() == PathHierarchyAggregationBuilder.DEFAULT_BUCKET_COUNT_THRESHOLDS.getShardSize()) {
        // The user has not made a shardSize selection. Use default
        // heuristic to avoid any wrong-ranking caused by distributed
        // counting
        bucketCountThresholds.setShardSize(BucketUtils.suggestShardSideQueueSize(bucketCountThresholds.getRequiredSize()));
    }
    bucketCountThresholds.ensureValidity();
    return new PathHierarchyAggregator(
            name, factories, searchContext,
            valuesSourceBytes, order, minDocCount, bucketCountThresholds, separator, minDepth,
            parent, pipelineAggregators, metaData);
}
 
Example #2
Source File: DateHierarchyAggregatorFactory.java    From elasticsearch-aggregation-pathhierarchy with MIT License 6 votes vote down vote up
@Override
protected Aggregator doCreateInternal(
        ValuesSource.Numeric valuesSource, SearchContext searchContext, Aggregator parent,
        boolean collectsFromSingleBucket, List<PipelineAggregator> pipelineAggregators,
        Map<String, Object> metaData) throws IOException {

    DateHierarchyAggregator.BucketCountThresholds bucketCountThresholds = new
            DateHierarchyAggregator.BucketCountThresholds(this.bucketCountThresholds);
    if (!InternalOrder.isKeyOrder(order)
            && bucketCountThresholds.getShardSize() == DateHierarchyAggregationBuilder.DEFAULT_BUCKET_COUNT_THRESHOLDS.getShardSize()) {
        // The user has not made a shardSize selection. Use default
        // heuristic to avoid any wrong-ranking caused by distributed
        // counting
        bucketCountThresholds.setShardSize(BucketUtils.suggestShardSideQueueSize(bucketCountThresholds.getRequiredSize()));
    }
    bucketCountThresholds.ensureValidity();
    return new DateHierarchyAggregator(
            name, factories, searchContext,
            valuesSource, order, minDocCount, bucketCountThresholds, roundingsInfo,
            parent, pipelineAggregators, metaData);
}
 
Example #3
Source File: SignificantTermsParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {
    SignificantTermsParametersParser aggParser = new SignificantTermsParametersParser(significanceHeuristicParserMapper);
    ValuesSourceParser vsParser = ValuesSourceParser.any(aggregationName, SignificantStringTerms.TYPE, context)
            .scriptable(false)
            .formattable(true)
            .build();
    IncludeExclude.Parser incExcParser = new IncludeExclude.Parser();
    aggParser.parse(aggregationName, parser, context, vsParser, incExcParser);

    TermsAggregator.BucketCountThresholds bucketCountThresholds = aggParser.getBucketCountThresholds();
    if (bucketCountThresholds.getShardSize() == aggParser.getDefaultBucketCountThresholds().getShardSize()) {
        //The user has not made a shardSize selection .
        //Use default heuristic to avoid any wrong-ranking caused by distributed counting
        //but request double the usual amount.
        //We typically need more than the number of "top" terms requested by other aggregations
        //as the significance algorithm is in less of a position to down-select at shard-level -
        //some of the things we want to find have only one occurrence on each shard and as
        // such are impossible to differentiate from non-significant terms at that early stage.
        bucketCountThresholds.setShardSize(2 * BucketUtils.suggestShardSideQueueSize(bucketCountThresholds.getRequiredSize(), context.numberOfShards()));
    }

    bucketCountThresholds.ensureValidity();
    SignificanceHeuristic significanceHeuristic = aggParser.getSignificanceHeuristic();
    if (significanceHeuristic == null) {
        significanceHeuristic = JLHScore.INSTANCE;
    }
    return new SignificantTermsAggregatorFactory(aggregationName, vsParser.config(), bucketCountThresholds, aggParser.getIncludeExclude(), aggParser.getExecutionHint(), aggParser.getFilter(), significanceHeuristic);
}
 
Example #4
Source File: TermsParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {
    TermsParametersParser aggParser = new TermsParametersParser();
    ValuesSourceParser vsParser = ValuesSourceParser.any(aggregationName, StringTerms.TYPE, context).scriptable(true).formattable(true).build();
    IncludeExclude.Parser incExcParser = new IncludeExclude.Parser();
    aggParser.parse(aggregationName, parser, context, vsParser, incExcParser);

    List<OrderElement> orderElements = aggParser.getOrderElements();
    List<Terms.Order> orders = new ArrayList<>(orderElements.size());
    for (OrderElement orderElement : orderElements) {
        orders.add(resolveOrder(orderElement.key(), orderElement.asc()));
    }
    Terms.Order order;
    if (orders.size() == 1 && (orders.get(0) == InternalOrder.TERM_ASC || orders.get(0) == InternalOrder.TERM_DESC))
    {
        // If order is only terms order then we don't need compound ordering
        order = orders.get(0);
    }
    else
    {
        // for all other cases we need compound order so term order asc can be added to make the order deterministic
        order = Order.compound(orders);
    }
    TermsAggregator.BucketCountThresholds bucketCountThresholds = aggParser.getBucketCountThresholds();
    if (!(order == InternalOrder.TERM_ASC || order == InternalOrder.TERM_DESC)
            && bucketCountThresholds.getShardSize() == aggParser.getDefaultBucketCountThresholds().getShardSize()) {
        // The user has not made a shardSize selection. Use default heuristic to avoid any wrong-ranking caused by distributed counting
        bucketCountThresholds.setShardSize(BucketUtils.suggestShardSideQueueSize(bucketCountThresholds.getRequiredSize(),
                context.numberOfShards()));
    }
    bucketCountThresholds.ensureValidity();
    return new TermsAggregatorFactory(aggregationName, vsParser.config(), order, bucketCountThresholds, aggParser.getIncludeExclude(), aggParser.getExecutionHint(), aggParser.getCollectionMode(), aggParser.showTermDocCountError());
}
 
Example #5
Source File: GeoHashGridParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {

    ValuesSourceParser vsParser = ValuesSourceParser.geoPoint(aggregationName, InternalGeoHashGrid.TYPE, context).build();

    int precision = GeoHashGridParams.DEFAULT_PRECISION;
    int requiredSize = GeoHashGridParams.DEFAULT_MAX_NUM_CELLS;
    int shardSize = -1;

    XContentParser.Token token;
    String currentFieldName = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (vsParser.token(currentFieldName, token, parser)) {
            continue;
        } else if (token == XContentParser.Token.VALUE_NUMBER ||
                token == XContentParser.Token.VALUE_STRING) { //Be lenient and also allow numbers enclosed in quotes
            if (context.parseFieldMatcher().match(currentFieldName, GeoHashGridParams.FIELD_PRECISION)) {
                precision = GeoHashGridParams.checkPrecision(parser.intValue());
            } else if (context.parseFieldMatcher().match(currentFieldName, GeoHashGridParams.FIELD_SIZE)) {
                requiredSize = parser.intValue();
            } else if (context.parseFieldMatcher().match(currentFieldName, GeoHashGridParams.FIELD_SHARD_SIZE)) {
                shardSize = parser.intValue();
            }
        } else if (token != XContentParser.Token.START_OBJECT) {
            throw new SearchParseException(context, "Unexpected token " + token + " in [" + aggregationName + "].",
                    parser.getTokenLocation());
        }
    }

    if (shardSize == 0) {
        shardSize = Integer.MAX_VALUE;
    }

    if (requiredSize == 0) {
        requiredSize = Integer.MAX_VALUE;
    }

    if (shardSize < 0) {
        //Use default heuristic to avoid any wrong-ranking caused by distributed counting
        shardSize = BucketUtils.suggestShardSideQueueSize(requiredSize, context.numberOfShards());
    }

    if (shardSize < requiredSize) {
        shardSize = requiredSize;
    }

    return new GeoGridFactory(aggregationName, vsParser.config(), precision, requiredSize, shardSize);

}