aggregations 包含 bucketing metric matrix pipeline
Bucketing
A family of aggregations that build buckets, where each bucket is associated with a key and a document criterion. When the aggregation is executed, all the buckets criteria are evaluated on every document in the context and when a criterion matches, the document is considered to "fall in" the relevant bucket. By the end of the aggregation process, we’ll end up with a list of buckets - each one with a set of documents that "belong" to it.
实体类
@Data
@Document(indexName = "es-customer",
type = "customer",
shards = 2,
replicas = 1, refreshInterval = "-1")
public class Customer {
@Id
private String id;
@Field(type = FieldType.Keyword
)
private String firstName;
private String lastName;
private Boolean valid;
private Integer age;
private Date createTime;
}
es里 数据
Customer(id=4_lguWkB9U5ULlhWJ1QK, firstName=tom, lastName=001, valid=null, age=44, createTime=Tue Mar 26 17:41:20 CST 2019)
Customer(id=5PlguWkB9U5ULlhWJ1Q5, firstName=lily, lastName=002, valid=null, age=56, createTime=Tue Mar 26 17:41:20 CST 2019)
Customer(id=5flguWkB9U5ULlhWJ1Rk, firstName=lily, lastName=004, valid=null, age=53, createTime=Tue Mar 26 17:41:20 CST 2019)
Customer(id=3_lguWkB9U5ULlhWJlQy, firstName=river 1, lastName=007, valid=null, age=12, createTime=Tue Mar 26 17:41:20 CST 2019)
Customer(id=4PlguWkB9U5ULlhWJlR6, firstName=Lucy 1, lastName=001, valid=null, age=13, createTime=Tue Mar 26 17:41:20 CST 2019)
Customer(id=4flguWkB9U5ULlhWJlSp, firstName=Lucy, lastName=002, valid=null, age=22, createTime=Tue Mar 26 17:41:20 CST 2019)
Customer(id=4vlguWkB9U5ULlhWJlTc, firstName=frank, lastName=001, valid=null, age=33, createTime=Tue Mar 26 17:41:20 CST 2019)
Customer(id=5vlguWkB9U5ULlhWJ1SN, firstName=tom, lastName=002, valid=null, age=66, createTime=Tue Mar 26 17:41:20 CST 2019)
Customer(id=5_lguWkB9U5ULlhWJ1S2, firstName=tom, lastName=005, valid=null, age=33, createTime=Tue Mar 26 17:41:20 CST 2019)
进行聚合操作
@Test
public void aggregationTest(){
//获取注解,通过注解可以得到 indexName 和 type
Document document = Customer.class.getAnnotation(Document.class);
// 构建查询 , Aggregation 中 nameCount 为聚合的结果,firstName 为字段名称
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchAllQuery())
.withSearchType(SearchType.QUERY_THEN_FETCH)
.withIndices(document.indexName()).withTypes(document.type())
.addAggregation(AggregationBuilders.terms("nameCount").field("firstName"))
.build();
// 聚合的结果
Aggregations aggregations = elasticsearchTemplate.query(searchQuery, response -> response.getAggregations());
Map<String, Aggregation> results = aggregations.asMap();
StringTerms stringTerms= (StringTerms) results.get("nameCount");
// 将bucket list 转换成 map , key -> 名字 value-> 出现次数
Map<String,Long> nameCountMap = stringTerms.getBuckets()
.stream()
.collect(Collectors.toMap(
StringTerms.Bucket::getKeyAsString,
InternalTerms.Bucket::getDocCount,
(x,y)->x)
);
System.out.println(nameCountMap);
}
返回结果
{frank=1, tom=3, lily=2, Lucy=1, river 1=1, Lucy 1=1}
参考地址
https://www.elastic.co/guide/en/elasticsearch/reference/6.3/search-aggregations.html#search-aggregations
https://blog.csdn.net/Topdandan/article/details/81436141
https://my.oschina.net/u/3664884/blog/1814423
网友评论