Elasticsearch version: 7.8html
需求是分頁去重獲取索引中的數據, 相似 MySQL 的 distinct. Elasticsearch 中的 collapse 能夠實現該需求.app
collapse 官網文檔elasticsearch
You can use the collapse parameter to collapse search results based on field values. The collapsing is done by selecting only the top sorted document per collapse key.
你可使用 collapse 參數根據字段值摺疊搜索結果, 摺疊是經過每一個摺疊鍵僅選擇排序最靠前的文檔來完成的.ide
注意:ui
The total number of hits in the response indicates the number of matching documents without collapsing. The total number of distinct group is unknown.
響應中的總數表示沒有摺疊的匹配文檔數, 去重後的總數是不知道的.spa
那麼怎麼獲取去重後的總數呢? 可使用 Aggregation 中的 cardinality 來實現.code
DSL example:blog
{ "from": 0, "size": 5, "sort": [ { "createTime": { "order": "desc" } } ], "collapse": { "field": "app_id" }, "aggs": { "total_size": { "cardinality": { "field": "app_id" } } } }
Java API example:排序
SortBuilder sortBuilder = SortBuilders.fieldSort(CREATE_TIME).order(SortOrder.DESC); CollapseBuilder collapseBuilder = new CollapseBuilder(APP_ID); AggregationBuilder aggregation = AggregationBuilders.cardinality(TOTAL_COUNT_KEY).field(APP_ID);