org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder. 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: GroupParser.java    From sql4es with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a set of 'leaf aggregations' to the provided parent metric (i.e. count, sum, max etc)
 * @param parentAgg
 * @param heading
 * @param addCount
 */
@SuppressWarnings("rawtypes")
private void addMetrics(AggregationBuilder parentAgg, Heading heading, boolean addCount){
	for(Column metric : heading.columns()){
		AggregationBuilder agg = null;
	    if(metric.getOp() == Operation.AVG) {
               agg = AggregationBuilders.avg(metric.getAggName());
           } else if(addCount && metric.getOp() == Operation.COUNT) {
               agg = AggregationBuilders.count(metric.getAggName());
           } else if(metric.getOp() == Operation.MAX) {
               agg = AggregationBuilders.max(metric.getAggName());
           } else if(metric.getOp() == Operation.MIN) {
               agg = AggregationBuilders.min(metric.getAggName());
           } else if(metric.getOp() == Operation.SUM) {
               agg = AggregationBuilders.sum(metric.getAggName());
           }
		if (agg != null) {
	        String col = metric.getColumn();
	        // * or number
	        if ("*".equals(col) || col.matches("-?\\d+")) {
                   agg = ((ValuesSourceAggregationBuilder.LeafOnly)agg).script(new Script("1"));
               } else if (col.startsWith("script:")) {
                   agg = ((ValuesSourceAggregationBuilder.LeafOnly)agg).script(new Script(col.replaceAll("script:","")));
               } else {
                   agg = ((ValuesSourceAggregationBuilder.LeafOnly) agg).field(metric.getColumn());
               }
               parentAgg.subAggregation(agg);
           }
	}
}
 
Example #2
Source File: AggMaker.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
public MovFnPipelineAggregationBuilder makeMovingFieldAgg(MethodField field, AggregationBuilder parent) throws SqlParseException {
    //question 加到groupMap里是为了什么
    groupMap.put(field.getAlias(), new KVValue("FIELD", parent));

    String bucketPath = field.getParams().get(0).value.toString();
    int window = Integer.parseInt(field.getParams().get(1).value.toString());

    ValuesSourceAggregationBuilder builder;
    field.setAlias(fixAlias(field.getAlias()));
    switch (field.getName().toUpperCase()) {
        //added by xzb 增加 movingavg和rollingstd
        case "MOVINGAVG":
            MovFnPipelineAggregationBuilder mvAvg =
                    //PipelineAggregatorBuilders.movingFunction("movingAvgIncome", new Script("MovingFunctions.unweightedAvg(values)"), "incomeSum", 2);
                    PipelineAggregatorBuilders.movingFunction(field.getAlias(),  new Script("MovingFunctions.unweightedAvg(values)"), bucketPath, window);

            return mvAvg;

        case "ROLLINGSTD":
            MovFnPipelineAggregationBuilder stdDev =
                    //PipelineAggregatorBuilders.movingFunction("stdDevIncome", new Script("MovingFunctions.stdDev(values, MovingFunctions.unweightedAvg(values))"), "incomeSum", 2);
                    PipelineAggregatorBuilders.movingFunction(field.getAlias() , new Script("MovingFunctions.stdDev(values, MovingFunctions.unweightedAvg(values))"), bucketPath, window);

            return stdDev;
    }
    return  null;
}
 
Example #3
Source File: AggMaker.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
/**
 * Create aggregation according to the SQL function.
 * zhongshu-comment 根据sql中的函数来生成一些agg,例如sql中的count()、sum()函数,这是agg链中最里边的那个agg了,eg:
 *                  select a,b,count(c),sum(d) from tbl group by a,b
 * @param field  SQL function
 * @param parent parentAggregation
 * @return AggregationBuilder represents the SQL function
 * @throws SqlParseException in case of unrecognized function
 */
public AggregationBuilder makeFieldAgg(MethodField field, AggregationBuilder parent) throws SqlParseException {
    //question 加到groupMap里是为了什么
    groupMap.put(field.getAlias(), new KVValue("FIELD", parent));

    ValuesSourceAggregationBuilder builder;
    field.setAlias(fixAlias(field.getAlias()));
    switch (field.getName().toUpperCase()) {
        case "SUM":
            builder = AggregationBuilders.sum(field.getAlias());
            return addFieldToAgg(field, builder);
        case "MAX":
            builder = AggregationBuilders.max(field.getAlias());
            return addFieldToAgg(field, builder);
        case "MIN":
            builder = AggregationBuilders.min(field.getAlias());
            return addFieldToAgg(field, builder);
        case "AVG":
            builder = AggregationBuilders.avg(field.getAlias());
            return addFieldToAgg(field, builder);
        case "STATS":
            builder = AggregationBuilders.stats(field.getAlias());
            return addFieldToAgg(field, builder);
        case "EXTENDED_STATS":
            builder = AggregationBuilders.extendedStats(field.getAlias());
            return addFieldToAgg(field, builder);
        case "PERCENTILES":
            builder = AggregationBuilders.percentiles(field.getAlias());
            addSpecificPercentiles((PercentilesAggregationBuilder) builder, field.getParams());
            return addFieldToAgg(field, builder);
        case "PERCENTILE_RANKS":
            double[] rankVals = getSpecificPercentileRankVals(field.getParams());
            builder = AggregationBuilders.percentileRanks(field.getAlias(), rankVals);
            return addFieldToAgg(field, builder);
        case "TOPHITS":
            return makeTopHitsAgg(field);
        case "SCRIPTED_METRIC":
            return scriptedMetric(field);
        case "COUNT":
            groupMap.put(field.getAlias(), new KVValue("COUNT", parent));
            return addFieldToAgg(field, makeCountAgg(field));
        default:
            throw new SqlParseException("the agg function not to define !");
    }
}
 
Example #4
Source File: AggMaker.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
private AggregationBuilder addFieldToAgg(MethodField field, ValuesSourceAggregationBuilder builder) {
    KVValue kvValue = field.getParams().get(0);
    if (kvValue.key != null && kvValue.key.equals("script")) {
        if (kvValue.value instanceof MethodField) {
            return builder.script(new Script(((MethodField) kvValue.value).getParams().get(1).toString()));
        } else {
            return builder.script(new Script(kvValue.value.toString()));
        }

    } else if (kvValue.key != null && kvValue.value.toString().trim().startsWith("def")) {
        return builder.script(new Script(kvValue.value.toString()));
    } else if (kvValue.key != null && (kvValue.key.equals("nested") || kvValue.key.equals("reverse_nested"))) {
        NestedType nestedType = (NestedType) kvValue.value;

        builder.field(nestedType.field);

        AggregationBuilder nestedBuilder;

        String nestedAggName = nestedType.field + "@NESTED";

        if (nestedType.isReverse()) {
            if (nestedType.path != null && nestedType.path.startsWith("~")) {
                String realPath = nestedType.path.substring(1);
                nestedBuilder = AggregationBuilders.nested(nestedAggName,realPath);
                nestedBuilder = nestedBuilder.subAggregation(builder);
                return AggregationBuilders.reverseNested(nestedAggName + "_REVERSED").subAggregation(nestedBuilder);
            } else {
                ReverseNestedAggregationBuilder reverseNestedAggregationBuilder = AggregationBuilders.reverseNested(nestedAggName);
                if (nestedType.path!=null){
                    reverseNestedAggregationBuilder.path(nestedType.path);
                }
                nestedBuilder = reverseNestedAggregationBuilder;
            }
        } else {
            nestedBuilder = AggregationBuilders.nested(nestedAggName,nestedType.path);
        }

        return nestedBuilder.subAggregation(builder);
    } else if (kvValue.key != null && (kvValue.key.equals("children"))) {
        ChildrenType childrenType = (ChildrenType) kvValue.value;

        builder.field(childrenType.field);

        AggregationBuilder childrenBuilder;

        String childrenAggName = childrenType.field + "@CHILDREN";

        childrenBuilder = JoinAggregationBuilders.children(childrenAggName,childrenType.childType);

        return childrenBuilder;
    }

    return builder.field(kvValue.toString());
}