elasticsearch bucket 之rare terms聚合使用詳解
1、背景
我們知道當我們使用 terms
聚合時,當修改默認順序為_count asc
時,統計的結果是不準備的,而且官方也不推薦我們這樣做,而是推薦使用rare terms
聚合。rare terms
是一個稀少
的term聚合,可以一定程度的解決升序問題。
2、需求
統計province
字段中包含上和湖
的term數據,並且最多隻能出現2次。獲取到聚合後的結果。
3、前置準備
3.1 準備mapping
PUT /index_person { "settings": { "number_of_shards": 1 }, "mappings": { "properties": { "id": { "type": "long" }, "name": { "type": "keyword" }, "province": { "type": "keyword" }, "sex": { "type": "keyword" }, "age": { "type": "integer" }, "pipeline_province_sex":{ "type": "keyword" }, "address": { "type": "text", "analyzer": "ik_max_word", "fields": { "keyword": { "type": "keyword", "ignore_above": 256 } } } } } }
3.2 準備數據
PUT /_bulk {"create":{"_index":"index_person","_id":1}} {"id":1,"name":"張三","sex":"男","age":20,"province":"湖北","address":"湖北省黃岡市羅田縣匡河鎮"} {"create":{"_index":"index_person","_id":2}} {"id":2,"name":"李四","sex":"男","age":19,"province":"江蘇","address":"江蘇省南京市"} {"create":{"_index":"index_person","_id":3}} {"id":3,"name":"王武","sex":"女","age":25,"province":"湖北","address":"湖北省武漢市江漢區"} {"create":{"_index":"index_person","_id":4}} {"id":4,"name":"趙六","sex":"女","age":30,"province":"北京","address":"北京市東城區"} {"create":{"_index":"index_person","_id":5}} {"id":5,"name":"錢七","sex":"女","age":16,"province":"北京","address":"北京市西城區"} {"create":{"_index":"index_person","_id":6}} {"id":6,"name":"王八","sex":"女","age":45,"province":"北京","address":"北京市朝陽區"} {"create":{"_index":"index_person","_id":7}} {"id":7,"name":"九哥","sex":"男","age":25,"province":"上海市","address":"上海市嘉定區"}
4、實現需求
4.1 dsl
GET /index_person/_search { "size": 0, "aggs": { "agg_province": { "rare_terms": { "field": "province", "max_doc_count": 2, "precision": 0.01, "include": "(.*上.*|.*湖.*|.*江.*)", "exclude": ["江蘇"], "missing": "default省" } } } }
4.2 java代碼
@Test @DisplayName("稀少的term聚合,類似按照 _count asc 排序的terms聚合,但是terms聚合中按照_count asc的結果是不準的,需要使用 rare terms 聚合") public void agg01() throws IOException { SearchRequest searchRequest = new SearchRequest.Builder() .size(0) .index("index_person") .aggregations("agg_province", agg -> agg.rareTerms(rare -> // 稀有詞 的字段 rare.field("province") // 該稀有詞最多可以出現在幾個文檔中,最大值為100,如果要調整,需要修改search.max_buckets參數的值(嘗試修改這個值,不生效) // 在該例子中,隻要是出現的次數<=2的聚合都會返回 .maxDocCount(2L) // 內部佈谷鳥過濾器的精度,精度越小越準,但是相應的消耗內存也越多,最小值為 0.00001,默認值為 0.01 .precision(0.01) // 應該包含在聚合的term, 當是單個字段是,可以寫正則表達式 .include(include -> include.regexp("(.*上.*|.*湖.*|.*江.*)")) // 排出在聚合中的term,當是集合時,需要寫準確的值 .exclude(exclude -> exclude.terms(Collections.singletonList("江蘇"))) // 當文檔中缺失province字段時,給默認值 .missing("default省") ) ) .build(); System.out.println(searchRequest); SearchResponse<Object> response = client.search(searchRequest, Object.class); System.out.println(response); }
一些註意事項都在註釋中。
4.3 運行結果
5、max_doc_count 和 search.max_buckets
6、註意事項
rare terms
統計返回的數據沒有大小
限制,而且受max_doc_count
參數的限制,比如:如果復合 max_doc_count 的分組有60個,那麼這60個分組會直接返回。max_doc_count
的值最大為100
,貌似不能修改。- 如果一臺節點聚合收集的結果過多,那麼很容易超過
search.max_buckets
的值,此時就需要修改這個值。
# 臨時修改 PUT /_cluster/settings {"transient": {"search.max_buckets": 65536}} # 永久修改 PUT /_cluster/settings {"persistent": {"search.max_buckets": 65536}}
完整代碼
gitee.com/huan1993/sp…
參考文檔
www.elastic.co/guide/en/el…
以上就是elasticsearch bucket 之rare terms聚合使用詳解的詳細內容,更多關於elasticsearch bucket rare terms的資料請關註WalkonNet其它相關文章!
推薦閱讀:
- Elasticsearch查詢之Term Query示例解析
- 詳解windows 環境下搭建electricSearch+kibana
- Elasticsearch 映射參數詳解 fields
- Elasticsearch屬性單詞常用解析說明
- Java面試重點中的重點之Elasticsearch核心原理