ES聚合操作

ES 聚合操作网上资料很乱,现简述一下工作中比较常用的方式( es 5 )

单聚合

select sum(memory) from courier_app_statitics group by cid where day>=20200716 and city_id=3101

与此等价的代码

TransportClient transportClient = getTransportClient("xxxxxx:9300");
		
		BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
		boolQueryBuilder.must(QueryBuilders.matchQuery("city_id", 3101));
		RangeQueryBuilder rangeQueryBuilder = new RangeQueryBuilder("day").gte(20200716);
		boolQueryBuilder.must(rangeQueryBuilder);
		SearchRequestBuilder srb = transportClient
				.prepareSearch()
				.setIndices("index").setTypes("type")
				.setQuery(boolQueryBuilder);
		
		SumAggregationBuilder sumCancelOrders = AggregationBuilders.sum("cid").field("memory");
		srb.addAggregation(sumCancelOrders);
		
		SearchResponse searchResponse = srb.get();
		InternalSum sum = searchResponse.getAggregations().get("cid");
		System.out.println("sum.getValue() = " + sum + "==" + (int) sum.getValue());


/**
	 * @param ESAddress
	 * @return
	 * @throws UnknownHostException
	 */
	public static TransportClient getTransportClient(String ESAddress) throws UnknownHostException {
		System.setProperty("es.set.netty.runtime.available.processors", "false");
		Settings settings = Settings.builder()
				//集群名称
//				.put("cluster.name", "onesearch")
				.put("client.transport.ignore_cluster_name", true)
				//自动嗅探
				.put("client.transport.sniff", true)
				.build();
		PreBuiltTransportClient preBuiltTransportClient = new PreBuiltTransportClient(settings);
		LOGGER.info("ESAddress {}", ESAddress);
		for (String s : ESAddress.split(",", -1)) {
			String[] split = s.split(":", -1);
			preBuiltTransportClient.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(split[0]), Integer.parseInt(split[1])));
		}
		return preBuiltTransportClient;
	}
	

多聚合

select sum(memory) as six_total_memory ,sum(age) as six_total_ages	from courier_app_statitics group by cid where day>=20200716 and city_id=3101

与此等价的代码


TermsAggregationBuilder field = AggregationBuilders.terms("cid").field("cid");
		SumAggregationBuilder sumCancelOrders = AggregationBuilders.sum("six_total_memory").field("memory");
		field.subAggregation(sumCancelOrders);
		SumAggregationBuilder sumFinishOrders = AggregationBuilders.sum("six_total_ages").field("age");
		field.subAggregation(sumFinishOrders);
		srb.addAggregation(field);
//		srb.setSize(0);
		
		
		SearchResponse searchResponse = srb.get();
		Aggregations aggregations = searchResponse.getAggregations();
		Terms terms = aggregations.get("cid");
		for (int i = 0; i < terms.getBuckets().size(); i++) {
			//statistics
			List<? extends Terms.Bucket> buckets = terms.getBuckets();
			String id = terms.getBuckets().get(i).getKey().toString();//id
			InternalSum sum = terms.getBuckets().get(i).getAggregations().get("six_total_memory");//数量
			InternalSum sum1 = terms.getBuckets().get(i).getAggregations().get("six_total_ages");//数量
			System.out.println("==" + id + "------six_total_memory " + sum.getValue() + " six_total_ages " + sum1.getValue());
		}

你可能感兴趣的:(工作之行,es,java,聚合,es,sum,java,es聚合java,es,sum,group,es,sum,where)