Java Code Examples for org.elasticsearch.search.aggregations.AggregationBuilders#max()

The following examples show how to use org.elasticsearch.search.aggregations.AggregationBuilders#max() . 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 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 !");
    }
}