天天看點

elasticsearch(八)調用TransportClient統計查詢結果處理

聚合查詢後,使用不同的聚合擷取值的方式也不相同。

針對上篇第一查詢處理

//聚合處理
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 總和, 平均值, 最大值,最小值
AbstractAggregationBuilder valueCountAggregationBuilder = AggregationBuilders.count("count").field("name");
AbstractAggregationBuilder sumAggregationBuilder = AggregationBuilders.sum("sum").field("score");
AbstractAggregationBuilder avgAggregationBuilder = AggregationBuilders.avg("avg").field("score");
AbstractAggregationBuilder maxAggregationBuilder = AggregationBuilders.max("max").field("score");
AbstractAggregationBuilder minAggregationBuilder = AggregationBuilders.min("min").field("score");

		sourceBuilder.aggregation(valueCountAggregationBuilder).aggregation(sumAggregationBuilder).aggregation(avgAggregationBuilder)
	.aggregation(maxAggregationBuilder).aggregation(minAggregationBuilder);

try {
	//查詢索引對象
	SearchRequest searchRequest = new SearchRequest(index);
	searchRequest.types(type);
	searchRequest.source(sourceBuilder);
	SearchResponse searchResponse = client.search(searchRequest).get();
	System.out.println(searchResponse);

	InternalValueCount valueCount = searchResponse.getAggregations().get("count");
	System.out.println(valueCount.getValue());

	InternalSum internalSum = searchResponse.getAggregations().get("sum");
	System.out.println(internalSum.getValue());
			
	InternalAvg internalAvg = searchResponse.getAggregations().get("avg");
	System.out.println(internalAvg.getValue());
			
	InternalMin internalMin = searchResponse.getAggregations().get("min");
	System.out.println(internalMin.getValue());
			
	InternalMax internalMax = searchResponse.getAggregations().get("max");
	System.out.println(internalMax.getValue());

} catch (InterruptedException | ExecutionException e) {
	e.printStackTrace();
}
           

第二和第三 分組查詢個查詢處理

//聚合處理
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 總和, 平均值, 最大值,最小值
AbstractAggregationBuilder sumAggregationBuilder = AggregationBuilders.sum("sum").field("score");
AbstractAggregationBuilder avgAggregationBuilder = AggregationBuilders.avg("avg").field("score");
AbstractAggregationBuilder maxAggregationBuilder = AggregationBuilders.max("max").field("score");
AbstractAggregationBuilder minAggregationBuilder = AggregationBuilders.min("min").field("score");

//		sourceBuilder.aggregation(valueCountAggregationBuilder).aggregation(sumAggregationBuilder).aggregation(avgAggregationBuilder)
//				.aggregation(maxAggregationBuilder).aggregation(minAggregationBuilder);

//此處定義分割線友善後面處理
String SEPARATOR = "<!!>";
//重點1 分組
TermsAggregationBuilder aggregation = AggregationBuilders.terms("name").field("name").order(Terms.Order.aggregation("name", true));

String scriptStr = "doc['name'].value +'" + SEPARATOR + "' + doc['age'].value" + SEPARATOR + "' + doc['sex'].value";

Script script = new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, scriptStr, new HashMap<>());

//重點2 子聚合添加script
aggregation.script(script).subAggregation(sumAggregationBuilder).subAggregation(avgAggregationBuilder).
		subAggregation(maxAggregationBuilder).subAggregation(minAggregationBuilder);
//重點3 添加aggregation到sourceBuilder
sourceBuilder.aggregation(aggregation);
try {
	//查詢索引對象
	SearchRequest searchRequest = new SearchRequest(index);
	searchRequest.types(type);
	searchRequest.source(sourceBuilder);
	SearchResponse searchResponse = client.search(searchRequest).get();
	System.out.println(searchResponse);

	List<Aggregation> aggregations = searchResponse.getAggregations().asList();
	StringTerms stringTerms = (StringTerms) aggregations.get(0);
	for (StringTerms.Bucket bucket : stringTerms.getBuckets()) {
		// 分組的字段使用#号分割
		String key = bucket.getKeyAsString();
		//多字段分組處理
		if (key.contains(SEPARATOR)) {
			String arr[] = key.split(SEPARATOR);
			String name = arr[0];
			String age = arr[1];
			String sex = arr[2];
		} else {
			//單字段分組處理
			String name = key;
		}

		//
		InternalValueCount valueCount = bucket.getAggregations().get("count");
		System.out.println(valueCount.getValue());

		InternalSum internalSum = bucket.getAggregations().get("sum");
		System.out.println(internalSum.getValue());

		InternalAvg internalAvg = bucket.getAggregations().get("avg");
		System.out.println(internalAvg.getValue());

		InternalMin internalMin = bucket.getAggregations().get("min");
		System.out.println(internalMin.getValue());

		InternalMax internalMax = bucket.getAggregations().get("max");
		System.out.println(internalMax.getValue());
	}
} catch (InterruptedException | ExecutionException e) {
	e.printStackTrace();
}