詳解elasticsearch之metric聚合實(shí)現(xiàn)示例
1、背景
此篇文章簡(jiǎn)單的記錄一下 elasticsearch
的metric
聚合操作。比如求 平均值、最大值、最小值、求和、總計(jì)、去重總計(jì)等。
2、準(zhǔn)備數(shù)據(jù)
2.1 準(zhǔn)備mapping
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" } } } }
2.2 準(zhǔn)備數(shù)據(jù)
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":"大三班技術(shù)班","province":"北京"} {"index":{"_id":5}} {"id":5, "name":"錢(qián)七","age":22,"class":"大三班","province":"湖北"}
3、metric聚合
3.1 max 平均值
3.1.1 dsl
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 } } } } } }
3.1.2 java代碼
@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") // 如果聚合的文檔缺失這個(gè)字段,則給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) // 腳本表達(dá)式 .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) // 腳本表達(dá)式 .source("_value * params.plus") // 參數(shù) .params("plus", JsonData.of(2)) ) ) ) ) ); System.out.println("request: " + request); SearchResponse<String> response = client.search(request, String.class); System.out.println("response: " + response); }
3.2 min最小值
3.2.1 dsl
POST /index_person/_search { "size": 0, "query": { "match_all": {} }, "aggs": { "agg_01": { "min": { "field": "age", "missing": 10 } } } }
3.2.2 java
POST /index_person/_search { "size": 0, "query": { "match_all": {} }, "aggs": { "agg_01": { "min": { "field": "age", "missing": 10 } } } }
3.3 min最小值
3.3.1 dsl
POST /index_person/_search { "size": 0, "query": { "match_all": {} }, "aggs": { "agg_01": { "avg": { "field": "age", "missing": 10 } } } }
3.3.2 java
@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") // 如果聚合的文檔缺失這個(gè)字段,則給10 .missing(10) ) ) ); System.out.println("request: " + request); SearchResponse<String> response = client.search(request, String.class); System.out.println("response: " + response); }
3.4 min最小值
3.4.1 dsl
POST /index_person/_search { "size": 0, "query": { "match_all": {} }, "aggs": { "agg_01": { "sum": { "field": "age", "missing": 10 } } } }
3.4.2 java
@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") // 如果聚合的文檔缺失這個(gè)字段,則給10 .missing(10) ) ) ); System.out.println("request: " + request); SearchResponse<String> response = client.search(request, String.class); System.out.println("response: " + response); }
3.5 count(*)
3.5.1 dsl
POST /index_person/_search { "size": 0, "query": { "match_all": {} }, "aggs": { "agg_01": { "value_count": { "field": "province", "missing": 10 } } } }
3.5.2 java
@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") // 如果聚合的文檔缺失這個(gè)字段,則給10 .missing(10) ) ) ); System.out.println("request: " + request); SearchResponse<String> response = client.search(request, String.class); System.out.println("response: " + response); }
3.6 count(distinct)
3.6.1 dsl
POST /index_person/_search { "size": 0, "query": { "match_all": {} }, "aggs": { "agg_01": { "cardinality": { "field": "province", "missing": 10 } } } }
3.6.2 java
@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") // 如果聚合的文檔缺失這個(gè)字段,則給10 .missing(10) ) ) ); System.out.println("request: " + request); SearchResponse<String> response = client.search(request, String.class); System.out.println("response: " + response); }
3.7 stat (max,min,avg,count,sum)
3.7.1 dsl
POST /index_person/_search { "size": 0, "query": { "match_all": {} }, "aggs": { "agg_01": { "stats": { "field": "avg", "missing": 10 } } } }
3.7.2 java
@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") // 如果聚合的文檔缺失這個(gè)字段,則給10 .missing(10) ) ) ); System.out.println("request: " + request); SearchResponse<String> response = client.search(request, String.class); System.out.println("response: " + response); }
3.8 聚合后返回每個(gè)聚合涉及的文檔
3.8.1 需求
根據(jù) province
進(jìn)行terms
聚合,然后獲取每個(gè)terms
聚合 age
最大的那個(gè)文檔。
3.8.2 dsl
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"] } } } } } } }
3.8.3 java
@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); }
3.8.4 運(yùn)行結(jié)果
完整代碼
參考文檔
以上就是詳解elasticsearch之metric聚合實(shí)現(xiàn)示例的詳細(xì)內(nèi)容,更多關(guān)于elasticsearch metric聚合的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- ElasticSearch學(xué)習(xí)之文檔API相關(guān)操作
- Elasticsearch?計(jì)數(shù)分詞中的token使用實(shí)例
- Elasticsearch?percolate?查詢(xún)示例詳解
- 詳解如何在Elasticsearch中搜索空值
- 詳解elasticsearch實(shí)現(xiàn)基于拼音搜索
- Elasticsearch查詢(xún)及聚合類(lèi)DSL語(yǔ)句寶典示例詳解
- logstash將mysql數(shù)據(jù)同步到elasticsearch方法詳解
- ElasticSearch 動(dòng)態(tài)映射實(shí)戰(zhàn)詳解
相關(guān)文章
Java跨平臺(tái)原理與虛擬機(jī)相關(guān)簡(jiǎn)介
這篇文章主要介紹了Java跨平臺(tái)原理與虛擬機(jī)的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下2021-03-03Java設(shè)計(jì)模式之觀察者模式(Observer模式)
這篇文章主要介紹了Java設(shè)計(jì)模式之觀察者模式(Observer模式),文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04springboot cloud使用eureka整合分布式事務(wù)組件Seata 的方法
這篇文章主要介紹了springboot cloud使用eureka整合分布式事務(wù)組件Seata 的方法 ,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05Spring定時(shí)任務(wù)實(shí)現(xiàn)與配置(一)
這篇文章主要為大家詳細(xì)介紹了Spring定時(shí)任務(wù)的實(shí)現(xiàn)與配置第一篇,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06Java實(shí)現(xiàn)經(jīng)典游戲復(fù)雜迷宮
這篇文章主要介紹了如何利用java語(yǔ)言實(shí)現(xiàn)經(jīng)典《復(fù)雜迷宮》游戲,文中采用了swing技術(shù)進(jìn)行了界面化處理,感興趣的小伙伴可以動(dòng)手試一試2022-02-02使用ThreadPoolExecutor之高效處理并發(fā)任務(wù)
這篇文章主要介紹了使用ThreadPoolExecutor之高效處理并發(fā)任務(wù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-07-07一文搞懂spring boot本地事務(wù)@Transactional參數(shù)
這篇文章主要介紹了spring boot本地事務(wù)@Transactional參數(shù)詳解,本文通過(guò)示例代碼圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-10-10