org.elasticsearch.search.aggregations.metrics.avg.InternalAvg Java Examples

The following examples show how to use org.elasticsearch.search.aggregations.metrics.avg.InternalAvg. 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: PersonRepositoryTest.java    From spring-boot-demo with MIT License 6 votes vote down vote up
/**
 * 测试聚合,测试平均年龄
 */
@Test
public void agg() {
    // 构造查询条件
    NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
    // 不查询任何结果
    queryBuilder.withSourceFilter(new FetchSourceFilter(new String[]{""}, null));

    // 平均年龄
    queryBuilder.addAggregation(AggregationBuilders.avg("avg").field("age"));

    log.info("【queryBuilder】= {}", JSONUtil.toJsonStr(queryBuilder.build()));

    AggregatedPage<Person> people = (AggregatedPage<Person>) repo.search(queryBuilder.build());
    double avgAge = ((InternalAvg) people.getAggregation("avg")).getValue();
    log.info("【avgAge】= {}", avgAge);
}
 
Example #2
Source File: PersonRepositoryTest.java    From spring-boot-demo with MIT License 6 votes vote down vote up
/**
 * 测试聚合,测试平均年龄
 */
@Test
public void agg() {
    // 构造查询条件
    NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
    // 不查询任何结果
    queryBuilder.withSourceFilter(new FetchSourceFilter(new String[]{""}, null));

    // 平均年龄
    queryBuilder.addAggregation(AggregationBuilders.avg("avg").field("age"));

    log.info("【queryBuilder】= {}", JSONUtil.toJsonStr(queryBuilder.build()));

    AggregatedPage<Person> people = (AggregatedPage<Person>) repo.search(queryBuilder.build());
    double avgAge = ((InternalAvg) people.getAggregation("avg")).getValue();
    log.info("【avgAge】= {}", avgAge);
}
 
Example #3
Source File: PersonRepositoryTest.java    From spring-boot-demo with MIT License 6 votes vote down vote up
/**
 * 测试聚合,测试平均年龄
 */
@Test
public void agg() {
    // 构造查询条件
    NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
    // 不查询任何结果
    queryBuilder.withSourceFilter(new FetchSourceFilter(new String[]{""}, null));

    // 平均年龄
    queryBuilder.addAggregation(AggregationBuilders.avg("avg").field("age"));

    log.info("【queryBuilder】= {}", JSONUtil.toJsonStr(queryBuilder.build()));

    AggregatedPage<Person> people = (AggregatedPage<Person>) repo.search(queryBuilder.build());
    double avgAge = ((InternalAvg) people.getAggregation("avg")).getValue();
    log.info("【avgAge】= {}", avgAge);
}
 
Example #4
Source File: Utils.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
public static Map<String, Number> toStats(Aggregation statAggregation) {
    if (statAggregation instanceof InternalExtendedStats) {
        return Utils.createStatsResponse((InternalExtendedStats) statAggregation);
    }
    else if (statAggregation instanceof InternalStats) {
        return Utils.createStatsResponse((InternalStats) statAggregation);
    }
    else if (statAggregation instanceof InternalMax) {
        return Utils.createStatResponse((InternalMax) statAggregation);
    }
    else if (statAggregation instanceof InternalMin) {
        return Utils.createStatResponse((InternalMin) statAggregation);
    }
    else if (statAggregation instanceof InternalAvg) {
        return Utils.createStatResponse((InternalAvg) statAggregation);
    }
    else if (statAggregation instanceof InternalSum) {
        return Utils.createStatResponse((InternalSum) statAggregation);
    }
    else if (statAggregation instanceof InternalValueCount) {
        return Utils.createStatResponse((InternalValueCount) statAggregation);
    }
    return new HashMap<>();
}
 
Example #5
Source File: PurchaseInfoServiceImpl.java    From ChengFeng1.5 with MIT License 5 votes vote down vote up
@Override
public List<PurchaseInfoVo> getPurchaseInfo() {
    NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder();
    // 不查询任何结果
    builder.withSourceFilter(new FetchSourceFilter(new String[]{""}, null));


    builder.addAggregation(
            AggregationBuilders.terms("subtitle").field("subtitle")
                    .subAggregation(AggregationBuilders.avg("sales").field("sales")));
    // 2、查询,需要把结果强转为AggregatedPage类型
    AggregatedPage<PurchaseInfoDto> aggPage =
            (AggregatedPage<PurchaseInfoDto>) this.purchaseInfoRepository.search(builder.build());

    StringTerms agg = (StringTerms) aggPage.getAggregation("subtitle");
    // 3.2、获取桶
    List<StringTerms.Bucket> buckets = agg.getBuckets();
    List<PurchaseInfoVo> purchaseInfoVos= Lists.newArrayList();
    // 3.3、遍历
    for (StringTerms.Bucket bucket : buckets) {
        // 3.4、获取桶中的key,即品牌名称
        System.out.println(bucket.getKeyAsString());
        InternalAvg sales= (InternalAvg) bucket.getAggregations().asMap().get("sales");

        purchaseInfoVos.add(new PurchaseInfoVo(bucket.getKeyAsString(),sales.getValue()));
    }
    return purchaseInfoVos;
}
 
Example #6
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 #7
Source File: Utils.java    From foxtrot with Apache License 2.0 4 votes vote down vote up
public static Map<String, Number> createStatResponse(InternalAvg statAggregation) {
    return ImmutableMap.of(AVG, statAggregation.getValue());
}