ElasticSearch在Java中实现聚合操作

ElasticSearch在Java中实现聚合操作

适用范围ElasticSearch6.x

如果是ElasticSearch7或以上版本情转到如下链接

REST高级客户端-DocumentApi:https://blog.csdn.net/u014646662/article/details/96473156

REST高级客户端SearchApi:https://blog.csdn.net/u014646662/article/details/96853830

更多新的查询方式(ElasticSearch相关文档):https://blog.csdn.net/u014646662/article/category/8747782

对人工智能感兴趣的同学,可以点击以下链接:

现在人工智能非常火爆,很多朋友都想学,但是一般的教程都是为博硕生准备的,太难看懂了。最近发现了一个非常适合小白入门的教程,不仅通俗易懂而且还很风趣幽默。所以忍不住分享一下给大家。点这里可以跳转到教程。

https://www.cbedai.net/u014646662


	/**
	 * 求最大值
	 * @throws Exception
	 */
	@Test
	public void max()throws Exception {
		Settings settings = Settings.builder().put("cluster.name","my-application").build();
		@SuppressWarnings("resource")
		TransportClient client = new PreBuiltTransportClient(settings)
				.addTransportAddress(new TransportAddress(InetAddress.getByName("master"),9300));
		AggregationBuilder max = AggregationBuilders.max("priceMax").field("price");
		SearchResponse sr = client.prepareSearch("lib2").addAggregation(max).get();
		Max maxValue = sr.getAggregations().get("priceMax");
		System.out.println(maxValue.getValue());
		
	}
	/**
	 * 求最小值
	 * @throws Exception
	 */
	@Test
	public void min()throws Exception {
		Settings settings = Settings.builder().put("cluster.name","my-application").build();
		@SuppressWarnings("resource")
		TransportClient client = new PreBuiltTransportClient(settings)
		.addTransportAddress(new TransportAddress(InetAddress.getByName("master"),9300));
		AggregationBuilder minAggregationBuilder = AggregationBuilders.min("priceMin").field("price");
		SearchResponse sr = client.prepareSearch("lib2").addAggregation(minAggregationBuilder).get();
		Min min = sr.getAggregations().get("priceMin");
		System.out.println(min.getValue());
		
	}
	/**
	 * 求平均值
	 * @throws Exception
	 */
	@Test
	public void avg()throws Exception {
		Settings settings = Settings.builder().put("cluster.name","my-application").build();
		@SuppressWarnings("resource")
		TransportClient client = new PreBuiltTransportClient(settings)
		.addTransportAddress(new TransportAddress(InetAddress.getByName("master"),9300));
		AggregationBuilder minAggregationBuilder = AggregationBuilders.avg("numAvg").field("num");
		SearchResponse sr = client.prepareSearch("lib2").addAggregation(minAggregationBuilder).get();
		Avg avg = sr.getAggregations().get("numAvg");
		System.out.println(avg.getValue());
	}
	/**
	 * 求和
	 * @throws Exception
	 */
	@Test
	public void sum()throws Exception {
		Settings settings = Settings.builder().put("cluster.name","my-application").build();
		@SuppressWarnings("resource")
		TransportClient client = new PreBuiltTransportClient(settings)
		.addTransportAddress(new TransportAddress(InetAddress.getByName("master"),9300));
		AggregationBuilder minAggregationBuilder = AggregationBuilders.sum("numSum").field("num");
		SearchResponse sr = client.prepareSearch("lib2").addAggregation(minAggregationBuilder).get();
		Sum sum = sr.getAggregations().get("numSum");
		System.out.println(sum.getValue());
	}

 

你可能感兴趣的:(大数据,ElasticSearch,ElasticSearch)