此篇文章簡單的記錄一下 elasticsearch
的metric
聚合操作。比如求 平均值、最大值、最小值、求和、總計、去重總計等。
PUT /index_person
{
"settings": {
"number_of_shards": 1
},
"mappings": {
"properties": {
"id":{
"type": "long"
},
"name": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"class":{
"type": "text",
"fielddata": true
},
"province":{
"type": "keyword"
}
}
}
}
PUT /index_person/_bulk
{"index":{"_id":1}}
{"id":1, "name":"張三","age":18,"class":"大一班","province":"湖北"}
{"index":{"_id":2}}
{"id":2, "name":"李四","age":19,"class":"大一班","province":"湖北"}
{"index":{"_id":3}}
{"id":3, "name":"王武","age":20,"class":"大二班","province":"北京"}
{"index":{"_id":4}}
{"id":4, "name":"趙六","age":21,"class":"大三班技術班","province":"北京"}
{"index":{"_id":5}}
{"id":5, "name":"錢七","age":22,"class":"大三班","province":"湖北"}
POST /index_person/_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"agg_01": {
"max": {
"field": "age",
"missing": 10
}
}
}
}
POST /index_person/_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"agg_01": {
"max": {
"script": {
"lang": "painless",
"source": """
doc.age
"""
}
}
}
}
}
POST /index_person/_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"agg_01": {
"max": {
"field": "age",
"script": {
"lang": "painless",
"source": """
_value * params.a
""",
"params": {
"a": 2
}
}
}
}
}
}
@Test
@DisplayName("最大值聚合")
public void test01() throws IOException {
SearchRequest request = SearchRequest.of(searchRequest ->
searchRequest.index("index_person")
.size(0)
.aggregations("agg_01", agg ->
agg.max(max ->
// 聚合的欄位
max.field("age")
// 如果聚合的檔案缺失這個欄位,則給10
.missing(10)
)
)
);
System.out.println("request: " + request);
SearchResponse<String> response = client.search(request, String.class);
System.out.println("response: " + response);
}
@Test
@DisplayName("指令碼聚合")
public void test02() throws IOException {
SearchRequest request = SearchRequest.of(searchRequest ->
searchRequest.index("index_person")
.size(0)
.aggregations("agg_01", agg ->
agg.max(max ->
max.script(script ->
script.inline(inline ->
inline.lang(ScriptLanguage.Painless)
// 指令碼錶示式
.source("doc.age")
)
)
)
)
);
System.out.println("request: " + request);
SearchResponse<String> response = client.search(request, String.class);
System.out.println("response: " + response);
}
@Test
@DisplayName("值指令碼聚合")
public void test03() throws IOException {
SearchRequest request = SearchRequest.of(searchRequest ->
searchRequest.index("index_person")
.size(0)
.aggregations("agg_01", agg ->
agg.max(max ->
// 指定參與聚合的欄位
max.field("age")
.script(script ->
script.inline(inline ->
inline.lang(ScriptLanguage.Painless)
// 指令碼錶示式
.source("_value * params.plus")
// 引數
.params("plus", JsonData.of(2))
)
)
)
)
);
System.out.println("request: " + request);
SearchResponse<String> response = client.search(request, String.class);
System.out.println("response: " + response);
}
POST /index_person/_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"agg_01": {
"min": {
"field": "age",
"missing": 10
}
}
}
}
POST /index_person/_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"agg_01": {
"min": {
"field": "age",
"missing": 10
}
}
}
}
POST /index_person/_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"agg_01": {
"avg": {
"field": "age",
"missing": 10
}
}
}
}
@Test
@DisplayName("平均值聚合")
public void test01() throws IOException {
SearchRequest request = SearchRequest.of(searchRequest ->
searchRequest.index("index_person")
.size(0)
.aggregations("agg_01", agg ->
agg.avg(avg ->
// 聚合的欄位
avg.field("age")
// 如果聚合的檔案缺失這個欄位,則給10
.missing(10)
)
)
);
System.out.println("request: " + request);
SearchResponse<String> response = client.search(request, String.class);
System.out.println("response: " + response);
}
POST /index_person/_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"agg_01": {
"sum": {
"field": "age",
"missing": 10
}
}
}
}
@Test
@DisplayName("求和聚合")
public void test01() throws IOException {
SearchRequest request = SearchRequest.of(searchRequest ->
searchRequest.index("index_person")
.size(0)
.aggregations("agg_01", agg ->
agg.sum(sum ->
// 聚合的欄位
sum.field("age")
// 如果聚合的檔案缺失這個欄位,則給10
.missing(10)
)
)
);
System.out.println("request: " + request);
SearchResponse<String> response = client.search(request, String.class);
System.out.println("response: " + response);
}
POST /index_person/_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"agg_01": {
"value_count": {
"field": "province",
"missing": 10
}
}
}
}
@Test
@DisplayName("count(*)聚合")
public void test01() throws IOException {
SearchRequest request = SearchRequest.of(searchRequest ->
searchRequest.index("index_person")
.size(0)
.aggregations("agg_01", agg ->
agg.valueCount(valueCount ->
// 聚合的欄位
valueCount.field("age")
// 如果聚合的檔案缺失這個欄位,則給10
.missing(10)
)
)
);
System.out.println("request: " + request);
SearchResponse<String> response = client.search(request, String.class);
System.out.println("response: " + response);
}
POST /index_person/_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"agg_01": {
"cardinality": {
"field": "province",
"missing": 10
}
}
}
}
@Test
@DisplayName("count(distinct)聚合")
public void test01() throws IOException {
SearchRequest request = SearchRequest.of(searchRequest ->
searchRequest.index("index_person")
.size(0)
.aggregations("agg_01", agg ->
agg.cardinality(cardinality ->
// 聚合的欄位
cardinality.field("province")
// 如果聚合的檔案缺失這個欄位,則給10
.missing(10)
)
)
);
System.out.println("request: " + request);
SearchResponse<String> response = client.search(request, String.class);
System.out.println("response: " + response);
}
POST /index_person/_search
{
"size": 0,
"query": {
"match_all": {}
},
"aggs": {
"agg_01": {
"stats": {
"field": "avg",
"missing": 10
}
}
}
}
@Test
@DisplayName("stat聚合")
public void test01() throws IOException {
SearchRequest request = SearchRequest.of(searchRequest ->
searchRequest.index("index_person")
.size(0)
.aggregations("agg_01", agg ->
agg.stats(stats ->
// 聚合的欄位
stats.field("age")
// 如果聚合的檔案缺失這個欄位,則給10
.missing(10)
)
)
);
System.out.println("request: " + request);
SearchResponse<String> response = client.search(request, String.class);
System.out.println("response: " + response);
}
根據 province
進行terms
聚合,然後獲取每個terms
聚合 age
最大的那個檔案。
POST /index_person/_search
{
"size": 0,
"query": {
"range": {
"age": {
"gte": 10
}
}
},
"aggs": {
"agg_01": {
"terms": {
"field": "province"
},
"aggs": {
"agg_02": {
"top_hits": {
"from": 0,
"size": 1,
"sort": [
{
"age": {"order": "desc"}
}
],
"_source": {
"includes": ["id","age","name"]
}
}
}
}
}
}
}
@Test
@DisplayName("top hits 聚合")
public void test01() throws IOException {
SearchRequest request = SearchRequest.of(searchRequest ->
searchRequest.index("index_person")
.size(0)
.query(query -> query.range(range -> range.field("age").gt(JsonData.of(10))))
.aggregations("agg_01", agg ->
agg.terms(terms ->
terms.field("province")
)
.aggregations("agg_02", subAgg ->
subAgg.topHits(topHits ->
topHits.from(0)
.size(1)
.sort(sort -> sort.field(field -> field.field("age").order(SortOrder.Desc)))
.source(source -> source.filter(filter -> filter.includes(Arrays.asList("id", "age", "name"))))
)
)
)
);
System.out.println("request: " + request);
SearchResponse<String> response = client.search(request, String.class);
System.out.println("response: " + response);
}
本文來自部落格園,作者:huan1993,轉載請註明原文連結:https://www.cnblogs.com/huan1993/p/17007807.html