es5.3【6】条件聚合

条件聚合
head插件条件如下
在这里插入图片描述

package com.yb.es;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;

public class GroupBy {
	public static void main(String[] args) {
		new GroupBy().dowork();
	}

	private void dowork() {
		SearchRequest searchRequest = new SearchRequest("land_chufa");
		searchRequest.types("land_chufa");

		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		//可以拼接多个条件
		QueryBuilder query = QueryBuilders.boolQuery().must(QueryBuilders.prefixQuery("party", "岱山县"))
//				.mustNot(QueryBuilders.matchQuery("", ""))
//				.must(QueryBuilders.rangeQuery("createTime").gt(""))//gt大于 lt小于  gte大于等于  lte小于等于
				;
		
		TermsAggregationBuilder aggregation = AggregationBuilders.terms("field_count").field("lanmuId");

		searchSourceBuilder.query(query).size(0);
		searchSourceBuilder.aggregation(aggregation).size(0);
		searchRequest.source(searchSourceBuilder);
		Client client = ESUtils.getClient();
		SearchResponse actionGet = client.search(searchRequest).actionGet();
		Terms agg = actionGet.getAggregations().get("field_count");
		System.out.println("聚合结果长度:"+agg.getBuckets().size());
		for (Terms.Bucket entry : agg.getBuckets()) {
			String key = (String) entry.getKey();
			long count = entry.getDocCount();
			System.out.println("key:" + key + ", count:" + count);
		}

	}
}

运行结果:

聚合结果长度:1
key:lt_5a1682c4acd75d6cf07bb509, count:9
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值