org.elasticsearch.search.aggregations.metrics.InternalNumericMetricsAggregation Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.metrics.InternalNumericMetricsAggregation. 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: SearchAggregationParser.java    From sql4es with Apache License 2.0 6 votes vote down vote up
/**
 * Parse an aggregation performed without grouping.
 * @param filter
 * @param rs
 * @throws SQLException
 */
private void processFilterAgg(InternalFilter filter, ESResultSet rs) throws SQLException{
	//String name = global.getName(); // we do not care about the global name for now
	List<Object> row = rs.getNewRow();
	Column count = null;
	for(Column c : rs.getHeading().columns())
		if(c.getOp() == Operation.COUNT) count = c;
	
	if(count != null){
		row.set(count.getIndex(), filter.getDocCount());
	}
	for(Aggregation agg : filter.getAggregations()){
		if(agg instanceof InternalNumericMetricsAggregation.SingleValue){
			InternalNumericMetricsAggregation.SingleValue numericAgg = 
					(InternalNumericMetricsAggregation.SingleValue)agg;
			String name =numericAgg.getName();
			Column column = rs.getHeading().getColumnByLabel(name);
			if(column == null){
				throw new SQLException("Unable to identify column for "+name);
			}
			row.set(column.getIndex(), numericAgg.value());
		}else throw new SQLException("Unable to parse aggregation of type "+agg.getClass());
	}
	rs.add(row);
}
 
Example #2
Source File: BucketHelpers.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public static Double resolveBucketValue(InternalMultiBucketAggregation<?, ? extends InternalMultiBucketAggregation.Bucket> agg,
        InternalMultiBucketAggregation.Bucket bucket, List<String> aggPathAsList, GapPolicy gapPolicy) {
    try {
        Object propertyValue = bucket.getProperty(agg.getName(), aggPathAsList);
        if (propertyValue == null) {
            throw new AggregationExecutionException(DerivativeParser.BUCKETS_PATH.getPreferredName()
                    + " must reference either a number value or a single value numeric metric aggregation");
        } else {
            double value;
            if (propertyValue instanceof Number) {
                value = ((Number) propertyValue).doubleValue();
            } else if (propertyValue instanceof InternalNumericMetricsAggregation.SingleValue) {
                value = ((InternalNumericMetricsAggregation.SingleValue) propertyValue).value();
            } else {
                throw new AggregationExecutionException(DerivativeParser.BUCKETS_PATH.getPreferredName()
                        + " must reference either a number value or a single value numeric metric aggregation, got: "
                        + propertyValue.getClass().getCanonicalName());
            }
            // doc count never has missing values so gap policy doesn't apply here
            boolean isDocCountProperty = aggPathAsList.size() == 1 && "_count".equals(aggPathAsList.get(0));
            if (Double.isInfinite(value) || Double.isNaN(value) || (bucket.getDocCount() == 0 && !isDocCountProperty)) {
                switch (gapPolicy) {
                case INSERT_ZEROS:
                    return 0.0;
                case SKIP:
                default:
                    return Double.NaN;
                }
            } else {
                return value;
            }
        }
    } catch (InvalidAggregationPathException e) {
        return null;
    }
}
 
Example #3
Source File: SearchResult.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
private Object covenValue(Aggregation value) throws SqlParseException {
	if (value instanceof InternalNumericMetricsAggregation.SingleValue) {
		return ((InternalNumericMetricsAggregation.SingleValue) value).value();
	} else if (value instanceof InternalValueCount) {
		return ((InternalValueCount) value).getValue();
	} else if (value instanceof InternalTopHits) {
		return (value);
	} else if (value instanceof LongTerms) {
		return value;
	} else {
		throw new SqlParseException("unknow this agg type " + value.getClass());
	}
}
 
Example #4
Source File: SearchAggregationParser.java    From sql4es with Apache License 2.0 4 votes vote down vote up
/**
 * Parse an aggregation result based on one or more aggregated terms
 * @param terms
 * @param rs
 * @param row
 * @throws SQLException
 */
private void dfsAggregations(Terms terms, ESResultSet rs, List<Object> row) throws SQLException{
	List<Object> currentRow = Utils.clone(row);
	String columnName = terms.getName();
	if(!rs.getHeading().hasLabel(columnName)) throw new SQLException("Unable to identify column for aggregation named "+columnName);
	Column aggCol = rs.getHeading().getColumnByLabel(columnName);
	for(Terms.Bucket bucket : terms.getBuckets()){
		if (bucket instanceof StringTerms.Bucket) {
			aggCol.setSqlType(Types.VARCHAR);
		} else if (bucket instanceof LongTerms.Bucket) {
			aggCol.setSqlType(Types.TIMESTAMP);
			//ToDO: chack Timestamp
		}
		boolean metricAggs = false;
		List<Aggregation> aggs = bucket.getAggregations().asList();
		if(aggs.size() == 0){
			currentRow.set(aggCol.getIndex(), bucket.getKey());
			metricAggs = true;
		}else for(Aggregation agg : bucket.getAggregations().asList()){
			if(agg instanceof Terms){
				currentRow.set(aggCol.getIndex(), bucket.getKey());
				dfsAggregations((Terms)agg, rs, currentRow);
			}else{
				if(metricAggs == false){
					currentRow.set(aggCol.getIndex(), bucket.getKey());
					metricAggs = true;
				}
				String metricName = agg.getName();
				if(!rs.getHeading().hasLabel(metricName)) throw new SQLException("Unable to identify column for aggregation named "+metricName);
				Column metricCol = rs.getHeading().getColumnByLabel(metricName);
				// ToDo: check it
                   if (agg instanceof InternalAvg) {
                       currentRow.set(metricCol.getIndex(), ((InternalAvg) agg).getValue());
                   } else if (agg instanceof InternalCardinality) {
                       currentRow.set(metricCol.getIndex(), ((InternalCardinality) agg).getValue());
                   } else if (agg instanceof InternalMax) {
                       currentRow.set(metricCol.getIndex(), ((InternalMax) agg).getValue());
                   } else if (agg instanceof InternalMin) {
                       currentRow.set(metricCol.getIndex(), ((InternalMin) agg).getValue());
                   } else if (agg instanceof Percentile) {
                       currentRow.set(metricCol.getIndex(), ((Percentile) agg).getValue());
                   } else if (agg instanceof InternalSum) {
                       currentRow.set(metricCol.getIndex(), ((InternalSum) agg).getValue());
                   } else if (agg instanceof InternalValueCount) {
                       currentRow.set(metricCol.getIndex(), ((InternalValueCount) agg).getValue());
                   } else if (agg instanceof InternalNumericMetricsAggregation.SingleValue) {
                       currentRow.set(metricCol.getIndex(), ((InternalNumericMetricsAggregation.SingleValue) agg).getValueAsString());
                   } else {
                       // ToDo: I don't know (
                       currentRow.set(metricCol.getIndex(), agg.getName());
                   }
			}
		}
		if(metricAggs){
			rs.add(currentRow);
			currentRow = Utils.clone(row);
		}
		currentRow = Utils.clone(row);
	}
}