🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Field Collapsing 字段折叠 原文链接 : [https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html) 译文链接 : [http://cwiki.apachecn.org/pages/editpage.action?pageId=10813543](http://cwiki.apachecn.org/pages/editpage.action?pageId=10813543) 贡献者 : [李辰](/display/~lichen),[ApacheCN](/display/~apachecn),[Apache中文网](/display/~apachechina) 允许基于字段对结果进行折叠。折叠操作是通过每个选择每个折叠键的顶部文档来实现的。例如下面的query获取每个user的最佳tweet并且根据他们的likes数量排序。 ``` GET /twitter/tweet/_search ``` ``` { ``` ```     "query": { ``` ```         "match": { ``` ```             "message": "elasticsearch" ``` ```         } ``` ```     }, ``` ```     "collapse" : { ``` ```         "field" : "user" ![](https://img.kancloud.cn/89/2e/892e2d6105d4361c3e81583c8e019d23_14x15.jpg) ``` ```     }, ``` ```    "sort": ["likes"], ![](https://img.kancloud.cn/50/46/50466ab92768bfee2306a5da100c7496_14x15.jpg) ``` ```    "from": 10 ![](https://img.kancloud.cn/59/09/59090687855ebb833c063fe09e622609_14x15.jpg) ``` ``` } ``` [![](https://img.kancloud.cn/89/2e/892e2d6105d4361c3e81583c8e019d23_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO27-1)    使用user字段来折叠结果集 * * * [![](https://img.kancloud.cn/50/46/50466ab92768bfee2306a5da100c7496_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO27-2)    按照likes数量进行排序获取顶部文档 * * * [![](https://img.kancloud.cn/59/09/59090687855ebb833c063fe09e622609_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO27-3)    定义第一个折叠结果的偏移量 ****警告:**** 响应结果中的hits总数表示匹配的文档总数而不是折叠的,去重后的聚合总数是未知的。 用于折叠的字段必须是单值的[keyword](/display/Elasticsearch/KeyWord)或[numeric](/display/Elasticsearch/Numeric)字段并开启[doc_values](/pages/viewpage.action?pageId=10027604)(文档值)。 **注意:** 折叠只应用于顶部文档,而且不会影响聚合。 ## 扩大折叠的显示结果 它也可以通过_inner_hits_参数来扩大每个折叠的顶部文档。 GET /twitter/tweet/_search {     "query": {         "match": {             "message": "elasticsearch"         }     },     "collapse" : {         "field" : "user",![](https://img.kancloud.cn/89/2e/892e2d6105d4361c3e81583c8e019d23_14x15.jpg)         "inner_hits": {             "name": "last_tweets",![](https://img.kancloud.cn/50/46/50466ab92768bfee2306a5da100c7496_14x15.jpg)             "size": 5,![](https://img.kancloud.cn/59/09/59090687855ebb833c063fe09e622609_14x15.jpg)             "sort": [{ "date": "asc" }]![](https://img.kancloud.cn/72/66/726692319400896ddea1bf795251948a_14x15.jpg)         },         "max_concurrent_group_searches": 4![](https://img.kancloud.cn/34/27/3427b2f40fd77851efa479c41f613dfd_14x15.jpg)     },     "sort": ["likes"] } [![](https://img.kancloud.cn/89/2e/892e2d6105d4361c3e81583c8e019d23_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO28-1)    使用"user"字段折叠结果集 * * * [![](https://img.kancloud.cn/50/46/50466ab92768bfee2306a5da100c7496_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO28-2)    name参数用来分割结果响应中的inner_hits * * * [![](https://img.kancloud.cn/59/09/59090687855ebb833c063fe09e622609_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO28-3)    每个折叠内inner_hits检索到的数目 * * * [![](https://img.kancloud.cn/72/66/726692319400896ddea1bf795251948a_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO28-4)    每个分组的排序方式 * * * [![](https://img.kancloud.cn/34/27/3427b2f40fd77851efa479c41f613dfd_14x15.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO28-5)    每个分组内允许检索inner_hits的并发请求数量 查看[inner hits ](/display/Elasticsearch/Inner+hits)对于所支持的选项的完整列表和响应的格式。 该分组的扩展是通过向响应中返回的每个折叠的命中发送一个额外的查询来完成的。请求参数 `max_concurrent_group_searches` 可用于控制这个阶段并发搜索的最大数量。 默认值是基于数据节点的数量和默认的搜索线程池大小。 ****警告:**** `collapse(折叠)不能用于与[scroll](/display/Elasticsearch/Scroll), [rescore](/display/Elasticsearch/Rescoring), [search after](/display/Elasticsearch/Search+After) 一同使用。`