文档-聚合查询-max
# AggregationBuilders.max
创建Test20_Doc_Agg_Max
package com.stt.demo;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.ParsedMax;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
public class Test20_Doc_Agg_Max {
public static void main(String[] args) throws IOException {
// 创建客户端
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
// 创建查询请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
// 构建查询请求体
SearchSourceBuilder builder = new SearchSourceBuilder()
.aggregation(AggregationBuilders.max("max_age").field("age"));
builder.size(0);
request.source(builder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 查询后匹配
System.out.println("took:" + response.getTook());
System.out.println("time out:" + response.isTimedOut());
for (Aggregation agg : response.getAggregations()) {
System.out.println(agg.getName());
System.out.println(((ParsedMax) agg).getValue());
}
// 关闭客户端
client.close();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
返回
took:2ms
time out:false
max_age
50.0
1
2
3
4
2
3
4
Last Updated: 2022/02/05, 15:58:51