创建索引结构,向 ES 服务发送 PUT 请求:
http://127.0.0.1:9200/jh_test
{
"settings": {},
"mappings": {
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"sex": {
"type": "keyword"
},
"buyCount": {
"type": "long"
},
"createMonth":{
"type":"keyword"
}
}
}
}
其中字段的含义为:
name
:姓名、buyCount
:购买数量,sex
:性别,createMonth
:创建月
添加测试数据:
{"name":"张三","buyCount":5,"sex":"男","createMonth":"2021-01"}
{"name":"李四","buyCount":5,"sex":"男","createMonth":"2021-01"}
{"name":"小卷","buyCount":18,"sex":"女","createMonth":"2021-01"}
{"name":"小明","buyCount":6,"sex":"女","createMonth":"2021-01"}
{"name":"张三","buyCount":3,"sex":"男","createMonth":"2021-02"}
{"name":"王五","buyCount":8,"sex":"男","createMonth":"2021-02"}
{"name":"赵四","buyCount":4,"sex":"男","createMonth":"2021-02"}
{"name":"诸葛亮","buyCount":6,"sex":"男","createMonth":"2021-02"}
{"name":"黄忠","buyCount":9,"sex":"男","createMonth":"2021-03"}
{"name":"李白","buyCount":1,"sex":"男","createMonth":"2021-03"}
{"name":"赵四","buyCount":3,"sex":"男","createMonth":"2021-03"}
{"name":"张三","buyCount":2,"sex":"男","createMonth":"2021-03"}
{"name":"李四","buyCount":6,"sex":"男","createMonth":"2021-04"}
{"name":"王五","buyCount":9,"sex":"男","createMonth":"2021-04"}
{"name":"李四","buyCount":4,"sex":"男","createMonth":"2021-04"}
{"name":"王五","buyCount":2,"sex":"男","createMonth":"2021-04"}
聚合查询的用法,向 ES 服务发送 GET 请求:
http://127.0.0.1:9200/jh_test/_search
"aggs" : {
"" : { <!--聚合名称 -->
"" : { <!--聚合类型 -->
<aggregation_body> <!--聚合具体字段 -->
}
[,"meta" : { [<meta_data_body>] } ]? <!--元信息 -->
[,"aggs" : { [<sub_aggregation>]+ } ]? <!--子聚合 -->
}
}
{
"size":0,
"aggs":{
"buyCountSum":{
"sum": {
"field": "buyCount"
}
}
}
}
{
"size":0,
"query": {
"term": {
"createMonth": "2021-02"
}
},
"aggs":{
"buyCountSum":{
"sum": {
"field": "buyCount"
}
}
}
}
{
"size":0,
"query": {
"term": {
"createMonth": "2021-03"
}
},
"aggs":{
"buyCountMax":{
"max": {
"field": "buyCount"
}
}
}
}
{
"size":0,
"query": {
"term": {
"createMonth": "2021-03"
}
},
"aggs":{
"buyCountMin":{
"min": {
"field": "buyCount"
}
}
}
}
{
"size":0,
"query": {
"term": {
"createMonth": "2021-03"
}
},
"aggs":{
"buyCountMax":{
"max": {
"field": "buyCount"
}
},
"buyCountMin":{
"min": {
"field": "buyCount"
}
}
}
}
{
"size":0,
"aggs":{
"distinctName":{
"cardinality": {
"field": "name.keyword"
}
}
}
}
{
"size":0,
"query": {
"term": {
"createMonth": "2021-04"
}
},
"aggs":{
"distinctName":{
"cardinality": {
"field": "name.keyword"
}
}
}
}
{
"size":"0",
"aggs":{
"buyCountAvg":{
"avg":{
"field":"buyCount"
}
}
}
}
{
"size":0,
"aggs":{
"statsAll":{
"stats":{
"field":"buyCount"
}
}
}
}
{
"size":0,
"aggs": {
"createMonthGroup": {
"terms": {
"field": "createMonth"
},
"aggs": {
"buyCountMax": {
"max": {
"field": "buyCount"
}
}
}
}
}
}
{
"size":0,
"aggs": {
"createMonthGroup": {
"terms": {
"field": "createMonth"
},
"aggs": {
"sexGroup": {
"terms": {
"field": "sex"
},
"aggs": {
"buyCountAvg": {
"avg": {
"field": "buyCount"
}
}
}
}
}
}
}
}
创建实体类:
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = "jh_test", shards = 3, replicas = 1, refreshInterval = "30s")
public class JhTestEntity {
@Id
private String id;
@Field(type = FieldType.Text)
private String name;
@Field(type = FieldType.Keyword)
private String sex;
@Field(type = FieldType.Long)
private Long buyCount;
@Field(type = FieldType.Keyword)
private String createMonth;
}
@Test
void aggs() {
SumAggregationBuilder buyCountSum = AggregationBuilders.sum("buyCountSum").field("buyCount");
Query query = new NativeSearchQueryBuilder()
.addAggregation(buyCountSum)
.build();
SearchHits<JhTestEntity> search = elasticsearchRestTemplate.search(query, JhTestEntity.class);
if (search.hasAggregations()) {
Aggregations aggregations = search.getAggregations();
if (Objects.nonNull(aggregations)) {
Sum sum = aggregations.get("buyCountSum");
log.info("计算 buyCount 总数:{} ", sum.getValue());
}
}
}
@Test
void aggs() {
QueryBuilder queryBuilder = QueryBuilders.termQuery("createMonth", "2021-02");
SumAggregationBuilder buyCountSum = AggregationBuilders.sum("buyCountSum").field("buyCount");
Query query = new NativeSearchQueryBuilder()
.withQuery(queryBuilder)
.addAggregation(buyCountSum)
.build();
SearchHits<JhTestEntity> search = elasticsearchRestTemplate.search(query, JhTestEntity.class);
if (search.hasAggregations()) {
Aggregations aggregations = search.getAggregations();
if (Objects.nonNull(aggregations)) {
Sum sum = aggregations.get("buyCountSum");
log.info("计算 buyCount 总数:{} ", sum.getValue());
}
}
}
@Test
void aggs() {
QueryBuilder queryBuilder = QueryBuilders.termQuery("createMonth", "2021-03");
MaxAggregationBuilder buyCountMax = AggregationBuilders.max("buyCountMax").field("buyCount");
Query query = new NativeSearchQueryBuilder()
.withQuery(queryBuilder)
.addAggregation(buyCountMax)
.build();
SearchHits<JhTestEntity> search = elasticsearchRestTemplate.search(query, JhTestEntity.class);
if (search.hasAggregations()) {
Aggregations aggregations = search.getAggregations();
if (Objects.nonNull(aggregations)) {
Max max = aggregations.get("buyCountMax");
log.info("计算 buyCount 最大值:{} ", max.getValue());
}
}
}
@Test
void aggs() {
QueryBuilder queryBuilder = QueryBuilders.termQuery("createMonth", "2021-03");
MinAggregationBuilder buyCountMin = AggregationBuilders.min("buyCountMin").field("buyCount");
Query query = new NativeSearchQueryBuilder()
.withQuery(queryBuilder)
.addAggregation(buyCountMin)
.build();
SearchHits<JhTestEntity> search = elasticsearchRestTemplate.search(query, JhTestEntity.class);
if (search.hasAggregations()) {
Aggregations aggregations = search.getAggregations();
if (Objects.nonNull(aggregations)) {
Min min = aggregations.get("buyCountMin");
log.info("计算 buyCount 最小值:{} ", min.getValue());
}
}
}
@Test
void aggs() {
QueryBuilder queryBuilder = QueryBuilders.termQuery("createMonth", "2021-03");
MaxAggregationBuilder buyCountMax = AggregationBuilders.max("buyCountMax").field("buyCount");
MinAggregationBuilder buyCountMin = AggregationBuilders.min("buyCountMin").field("buyCount");
Query query = new NativeSearchQueryBuilder()
.withQuery(queryBuilder)
.addAggregation(buyCountMax)
.addAggregation(buyCountMin)
.build();
SearchHits<JhTestEntity> search = elasticsearchRestTemplate.search(query, JhTestEntity.class);
if (search.hasAggregations()) {
Aggregations aggregations = search.getAggregations();
if (Objects.nonNull(aggregations)) {
Max max = aggregations.get("buyCountMax");
log.info("计算 buyCount 最大值:{} ", max.getValue());
Min min = aggregations.get("buyCountMin");
log.info("计算 buyCount 最小值:{} ", min.getValue());
}
}
}
@Test
void aggs() {
CardinalityAggregationBuilder distinctName = AggregationBuilders.cardinality("distinctName").field("name.keyword");
Query query = new NativeSearchQueryBuilder()
.addAggregation(distinctName)
.build();
SearchHits<JhTestEntity> search = elasticsearchRestTemplate.search(query, JhTestEntity.class);
if (search.hasAggregations()) {
Aggregations aggregations = search.getAggregations();
if (Objects.nonNull(aggregations)) {
Cardinality cardinality = aggregations.get("distinctName");
log.info("计算 name 的去重后的数量:{} ", cardinality.getValue());
}
}
}
@Test
void aggs() {
QueryBuilder queryBuilder = QueryBuilders.termQuery("createMonth", "2021-04");
CardinalityAggregationBuilder distinctName = AggregationBuilders.cardinality("distinctName").field("name.keyword");
Query query = new NativeSearchQueryBuilder()
.withQuery(queryBuilder)
.addAggregation(distinctName)
.build();
SearchHits<JhTestEntity> search = elasticsearchRestTemplate.search(query, JhTestEntity.class);
if (search.hasAggregations()) {
Aggregations aggregations = search.getAggregations();
if (Objects.nonNull(aggregations)) {
Cardinality cardinality = aggregations.get("distinctName");
log.info("计算 name 的去重后的数量:{} ", cardinality.getValue());
}
}
}
@Test
void aggs() {
AvgAggregationBuilder buyCountAvg = AggregationBuilders.avg("buyCountAvg").field("buyCount");
Query query = new NativeSearchQueryBuilder()
.addAggregation(buyCountAvg)
.build();
SearchHits<JhTestEntity> search = elasticsearchRestTemplate.search(query, JhTestEntity.class);
if (search.hasAggregations()) {
Aggregations aggregations = search.getAggregations();
if (Objects.nonNull(aggregations)) {
Avg avg = aggregations.get("buyCountAvg");
log.info("计算 buyCount 的平均值:{} ", avg.getValue());
}
}
}
@Test
void aggs() {
StatsAggregationBuilder stats = AggregationBuilders.stats("stats").field("buyCount");
Query query = new NativeSearchQueryBuilder()
.addAggregation(stats)
.build();
SearchHits<JhTestEntity> search = elasticsearchRestTemplate.search(query, JhTestEntity.class);
if (search.hasAggregations()) {
Aggregations aggregations = search.getAggregations();
if (Objects.nonNull(aggregations)) {
Stats s = aggregations.get("stats");
log.info("计算 buyCount 的 count:{} ", s.getCount());
log.info("计算 buyCount 的 min:{} ", s.getMin());
log.info("计算 buyCount 的 max:{} ", s.getMax());
log.info("计算 buyCount 的 avg:{} ", s.getAvg());
log.info("计算 buyCount 的 sum:{} ", s.getSum());
}
}
}
@Test
void aggs() {
TermsAggregationBuilder createMonthGroup = AggregationBuilders.terms("createMonthGroup").field("createMonth")
.subAggregation(AggregationBuilders.max("buyCountMax").field("buyCount"));
Query query = new NativeSearchQueryBuilder()
.addAggregation(createMonthGroup)
.build();
SearchHits<JhTestEntity> search = elasticsearchRestTemplate.search(query, JhTestEntity.class);
if (search.hasAggregations()) {
Aggregations aggregations = search.getAggregations();
if (Objects.nonNull(aggregations)) {
Terms terms = aggregations.get("createMonthGroup");
terms.getBuckets().forEach(bucket -> {
String createMonth = bucket.getKeyAsString();
Aggregations subAggs = bucket.getAggregations();
if (Objects.nonNull(subAggs)) {
Max max = subAggs.get("buyCountMax");
log.info("计算 {} 月的最大值为:{} ", createMonth, max.getValue());
}
});
}
}
}
@Test
void aggs() {
TermsAggregationBuilder createMonthGroup = AggregationBuilders.terms("createMonthGroup").field("createMonth")
.subAggregation(AggregationBuilders.terms("sexGroup").field("sex")
.subAggregation(AggregationBuilders.avg("buyCountAvg").field("buyCount")));
Query query = new NativeSearchQueryBuilder()
.addAggregation(createMonthGroup)
.build();
SearchHits<JhTestEntity> search = elasticsearchRestTemplate.search(query, JhTestEntity.class);
if (search.hasAggregations()) {
Aggregations aggregations = search.getAggregations();
if (Objects.nonNull(aggregations)) {
Terms terms = aggregations.get("createMonthGroup");
terms.getBuckets().forEach(bucket -> {
String createMonth = bucket.getKeyAsString();
Aggregations sexAggs = bucket.getAggregations();
if (Objects.nonNull(sexAggs)) {
Terms sexTerms = sexAggs.get("sexGroup");
sexTerms.getBuckets().forEach(sexBucket -> {
String sex = sexBucket.getKeyAsString();
Aggregations avgAggs = sexBucket.getAggregations();
if (Objects.nonNull(avgAggs)) {
Avg avg = avgAggs.get("buyCountAvg");
log.info("计算 {} 月,{} 性 的平均值为:{} ", createMonth, sex, avg.getValue());
}
});
}
});
}
}
}