org.elasticsearch.search.aggregations.metrics.cardinality.InternalCardinality Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.metrics.cardinality.InternalCardinality. 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 5 votes vote down vote up
/**
 * Parses an ES aggregation into a set of ResultRows
 * @param agg
 * @return
 * @throws SQLException
 */
public void parseAggregation(Aggregation agg, ESResultSet rs) throws SQLException{
	if(agg instanceof Terms){
		dfsAggregations((Terms)agg, rs, rs.getNewRow());
	}else if (agg instanceof InternalFilter){
		processFilterAgg((InternalFilter)agg, rs);
	}else if (agg instanceof InternalCardinality){
		processCardinalityAgg((InternalCardinality)agg, rs);
	}else throw new SQLException ("Unknown aggregation type "+agg.getClass().getName());
}
 
Example #2
Source File: ElasticsearchGraphQueryIterable.java    From vertexium with Apache License 2.0 5 votes vote down vote up
private static CardinalityResult reduceCardinalityResults(ElasticsearchSearchQueryBase query, List<Aggregation> aggs) {
    if (aggs.size() == 0) {
        return new CardinalityResult(0);
    }
    if (aggs.size() == 1) {
        Aggregation agg = aggs.get(0);
        if (agg instanceof InternalCardinality) {
            return new CardinalityResult(((InternalCardinality) agg).getValue());
        } else {
            throw new VertexiumException("Unhandled aggregation result type: " + agg.getClass().getName());
        }
    }
    throw new VertexiumException("Cannot reduce multiple " + CardinalityAggregation.class + "(count: " + aggs.size() + ")");
}
 
Example #3
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);
	}
}
 
Example #4
Source File: SearchAggregationParser.java    From sql4es with Apache License 2.0 4 votes vote down vote up
private void processCardinalityAgg(InternalCardinality agg, ESResultSet rs) {
	List<Object> row = rs.getNewRow();
	Column column = rs.getHeading().getColumnByLabel(agg.getName());
	row.set(column.getIndex(), agg.value());
	rs.add(row);
}
 
Example #5
Source File: ElasticsearchGraphQueryIterable.java    From vertexium with Apache License 2.0 4 votes vote down vote up
private static AggregationResult reduceAggregationResults(ElasticsearchSearchQueryBase query, org.vertexium.query.Aggregation queryAgg, List<Aggregation> resultAggs) {
    if (resultAggs.size() == 0) {
        throw new VertexiumException("Cannot reduce zero sized aggregation list");
    }

    Object aggType = queryAgg;
    if (queryAgg == null) {
        Aggregation first = resultAggs.get(0);
        if (first.getName().endsWith(AGGREGATION_HAS_NOT_SUFFIX)) {
            if (resultAggs.size() > 1) {
                first = resultAggs.get(1);
            } else {
                throw new VertexiumException("Unhandled aggregation. Found HasNot filter with no associated aggregations and no query aggregation.");
            }
        }
        aggType = first;
    }

    if (aggType instanceof HistogramAggregation || aggType instanceof CalendarFieldAggregation ||
        aggType instanceof InternalHistogram || aggType instanceof InternalDateHistogram) {
        return reduceHistogramResults(query, resultAggs);
    }
    if (aggType instanceof RangeAggregation || aggType instanceof InternalRange) {
        return reduceRangeResults(query, resultAggs);
    }
    if (aggType instanceof PercentilesAggregation || aggType instanceof Percentiles) {
        return reducePercentilesResults(query, resultAggs);
    }
    if (aggType instanceof TermsAggregation || aggType instanceof InternalTerms) {
        return reduceTermsResults(query, resultAggs);
    }
    if (aggType instanceof GeohashAggregation || aggType instanceof InternalGeoHashGrid) {
        return reduceGeohashResults(query, resultAggs);
    }
    if (aggType instanceof StatisticsAggregation || aggType instanceof InternalExtendedStats) {
        return reduceStatisticsResults(resultAggs);
    }
    if (aggType instanceof CardinalityAggregation || aggType instanceof InternalCardinality) {
        return reduceCardinalityResults(query, resultAggs);
    }
    throw new VertexiumException("Unhandled aggregation type: " + aggType.getClass().getName());
}