章节
第一章链接: SpringBoot集成Elasticsearch7.x(1)|(增删改查功能实现)
第二章链接: SpringBoot集成Elasticsearch7.x(2)|(复杂查询)
第三章链接: SpringBoot集成Elasticsearch7.x(3)|(aggregations之指标聚合查询)
第四章链接: SpringBoot集成Elasticsearch7.x(4)|(aggregations之分桶聚合查询)
第五章链接: SpringBoot集成Elasticsearch7.x(5)|(term、match、match_phrase区别)
第六章链接: SpringBoot集成Elasticsearch8.x(6)|(新版本Java API Client使用)
第七章链接: SpringBoot集成Elasticsearch8.x(7)|(新版本Java API Client使用完整示例)
本章节主要介绍SpringBoot项目集成ElasticSearch的一些相关知识,主要介绍的是基于Java High Level REST Client方式实现ElasticSearch分组聚合(aggregations)查询操作以及对应kibana控制台JSON格式访问对照。
es的aggs可以分为 指标聚合(metrice)、分桶聚合(bucket)、 管道聚合(pipeline),分通聚合相当于sql的group by 能按照某一个或多个条件,对数据进行分桶(分组),默认返回数据的count(计数)条数,但实际上,可以理解为数据被分桶了,方便后面的聚合或者统计操作。指标聚合就是对分桶,或者未分桶的数据进行计算,例如avg求平均值,MAX最大值,min最小值,value count 计数 cardinality 基数 去重 starts 统计聚合等。管道聚合基于聚合结果的查询,分桶有可能是多层的,也有可能和指标是仙桃的,管道聚合可以根据路径(分桶和指标聚合时候的命名路径)对数据进行有针对性的操作,例如排序。
查询所有城市景点票价总和
GET /architecture_index/_search
{
"size": 0,
"aggs": {
"sum_price": {
"sum": {
"field": "price"
}
}
}
}
实现代码如下
/**
* 聚合查询所有景点门票和
* @throws IOException
*/
@Test
public void searchSum() throws IOException {
//定义请求对象
SearchRequest request = new SearchRequest("architecture_index");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//制定检索条件
sourceBuilder.query(QueryBuilders.matchAllQuery())
.aggregation(AggregationBuilders.sum("sum_price").field("price"));
//组装
request.source(sourceBuilder);
//执行
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
//打印数据
Aggregations aggregations = response.getAggregations();
System.out.println(JSON.toJSONString(aggregations.getAsMap().get("sum_price")));
}
查询所有城市景点票价总和
GET /architecture_index/_search
{
"size": 0,
"aggs": {
"count_name": {
"value_count": {
"field": "name.keyword"
}
}
}
}
实现代码如下
/**
* 聚合查询所有景点总数
* @throws IOException
*/
@Test
public void searchCount() throws IOException {
//定义请求对象
SearchRequest request = new SearchRequest("architecture_index");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//制定检索条件
sourceBuilder.query(QueryBuilders.matchAllQuery())
.aggregation(AggregationBuilders.count("count_name").field("name.keyword"));
//组装
request.source(sourceBuilder);
//执行
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
//打印数据
Aggregations aggregations = response.getAggregations();
System.out.println(JSON.toJSONString(aggregations.getAsMap().get("count_name")));
}
针对数字类型的查询,一次性查询出最低、最高、总和、总数量、平均值等
GET /architecture_index/_search
{
"size": 0,
"aggs": {
"status_price": {
"stats": {
"field": "price"
}
}
}
}
实现代码如下
/**
* 查询门票价格 最大值,最小值,平均值等
* @throws IOException
*/
@Test
public void searchStats() throws IOException {
//定义请求对象
SearchRequest request = new SearchRequest("architecture_index");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//制定检索条件
sourceBuilder.query(QueryBuilders.matchAllQuery())
.aggregation(AggregationBuilders.stats("status_price").field("price"));
//组装
request.source(sourceBuilder);
//执行
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
//打印数据
Aggregations aggregations = response.getAggregations();
System.out.println(JSON.toJSONString(aggregations.getAsMap().get("status_price")));
}
只统计总数、平均值
GET /architecture_index/_search
{
"size": 0,
"aggs": {
"count_name": {
"value_count": {
"field": "name.keyword"
}
},
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
实现代码如下
/**
* 求和 以及求平均值
* @throws IOException
*/
@Test
public void search2() throws IOException {
//定义请求对象
SearchRequest request = new SearchRequest("architecture_index");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//制定检索条件
sourceBuilder.query(QueryBuilders.matchAllQuery())
.aggregation(AggregationBuilders.count("count_name").field("name.keyword"))
.aggregation(AggregationBuilders.avg("avg_price").field("price"));
//组装
request.source(sourceBuilder);
//执行
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
//打印数据
Aggregations aggregations = response.getAggregations();
System.out.println(JSON.toJSONString(aggregations.getAsMap().get("count_name")));
System.out.println(JSON.toJSONString(aggregations.getAsMap().get("avg_price")));
}
以上就是SpringBoot集成Elasticsearch数据库内容,在验证过程中遇到很多问题,如果大家在使用过程中遇到问题欢迎留言。