8.11 SpringBoot集成ElasticSearch之聚合
作者:互联网
1.condition开发
在项目目录“/src/main/java/com/example/es/condition”下新建AggEmployeeCondition聚合条件类,AggEmployeeCondition类需要实现AggProvider接口,具体代码如下。
@Data
public class AggEmployeeCondition extends SampleEmployeeCondition implements AggProvider {
@Override
public Aggregation[] getAggregations() {
TermsAggregation terms = Aggregation.terms("groupByAge").fieldName("age").order(Order.COUNT_DESC, Order.KEY_ASC);
return new Aggregation[]{terms};
}
}
2.mapper开发
在项目目录“/src/main/java/com/example/es”的EmployeeMapper类中新增聚合接口,具体代码如下。
@EasyMapper(indices = "employee", clusterRouter = "sampleCluster")
public interface EmployeeMapper {
/**
* 聚合查询
*
* @param pageable 分页信息
* @param aggEmployeeCondition 聚合条件
* @return 响应
*/
@Agg
SearchResponse aggEmployee(Pageable pageable, AggEmployeeCondition aggEmployeeCondition);
/**
* 聚合查询
*
* @param aggEmployeeCondition 聚合条件
* @return 响应
*/
@Agg(aggOnly = true)
Aggregations aggsEmployee(AggEmployeeCondition aggEmployeeCondition);
/**
* 聚合查询
*
* @param aggEmployeeCondition 聚合条件
* @return 响应
*/
@Agg
List<Aggregation> aggEmployee(AggEmployeeCondition aggEmployeeCondition);
/**
* 聚合查询
*
* @param aggEmployeeCondition 聚合条件
* @return 响应
*/
@Agg
Map<String, Aggregation> aggEmployeeMap(AggEmployeeCondition aggEmployeeCondition);
}
3.controller开发
在项目目录“/src/main/java/com/example/es”下的EmployeeController类中新增聚合接口,以aggEmployee接口为例演示开发步骤,具体代码如下。
@RestController
@RequestMapping("/employee")
public class EmployeeController {
@Resource
private EmployeeMapper employeeMapper;
@ResponseBody
@RequestMapping(value = "/aggEmployee", method = RequestMethod.GET)
public List<Aggregation> aggEmployee() {
return employeeMapper.aggEmployee(new AggEmployeeCondition());
}
}
4.测试
启动项目,然后在postman中请求“http://localhost:8080/employee/aggEmployee”,成功后返回对应的信息。
[
{
"name": "groupByAge",
"buckets": [
{
"aggregations": {
"asMap": {},
"fragment": true
},
"keyAsString": "24",
"docCount": 1,
"docCountError": 0,
"key": 24,
"keyAsNumber": 24,
"fragment": true
},
{
"aggregations": {
"asMap": {},
"fragment": true
},
"keyAsString": "25",
"docCount": 1,
"docCountError": 0,
"key": 25,
"keyAsNumber": 25,
"fragment": true
},
{
"aggregations": {
"asMap": {},
"fragment": true
},
"keyAsString": "27",
"docCount": 1,
"docCountError": 0,
"key": 27,
"keyAsNumber": 27,
"fragment": true
},
{
"aggregations": {
"asMap": {},
"fragment": true
},
"keyAsString": "30",
"docCount": 1,
"docCountError": 0,
"key": 30,
"keyAsNumber": 30,
"fragment": true
},
{
"aggregations": {
"asMap": {},
"fragment": true
},
"keyAsString": "31",
"docCount": 1,
"docCountError": 0,
"key": 31,
"keyAsNumber": 31,
"fragment": true
},
{
"aggregations": {
"asMap": {},
"fragment": true
},
"keyAsString": "33",
"docCount": 1,
"docCountError": 0,
"key": 33,
"keyAsNumber": 33,
"fragment": true
},
{
"aggregations": {
"asMap": {},
"fragment": true
},
"keyAsString": "35",
"docCount": 1,
"docCountError": 0,
"key": 35,
"keyAsNumber": 35,
"fragment": true
}
],
"type": "lterms",
"docCountError": 0,
"sumOfOtherDocCounts": 0,
"metaData": null,
"fragment": true
}
]
标签:聚合,SpringBoot,AggEmployeeCondition,fragment,8.11,docCountError,ElasticSearch,agg 来源: https://blog.csdn.net/Jgx1214/article/details/122347476