ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# Field Collapsing 允许基于字段对结果进行折叠。折叠操作是通过每个选择每个折叠键的顶部文档来实现的。例如下面的query获取每个user的最佳tweet并且根据他们的likes数量排序。 GET /twitter/tweet/_search {     "query": {         "match": {             "message": "elasticsearch"         }     },     "collapse" : {         "field" : "user" [![](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/images/icons/callouts/1.png)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO27-1)     },    "sort": ["likes"], [![](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/images/icons/callouts/2.png)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO27-2)    "from": 10 [![](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/images/icons/callouts/3.png)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO27-3) } [![](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/images/icons/callouts/1.png)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO27-1)    使用user字段来折叠结果集 * * * [![](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/images/icons/callouts/2.png)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO27-2)    按照likes数量进行排序获取顶部文档 * * * [![](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/images/icons/callouts/3.png)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO27-3)    定义第一个折叠结果的偏移量 ****警告:**    **响应结果中的hits总数表示匹配的文档总数而不是折叠的,去重后的聚合总数是未知的。 用于折叠的字段必须是单值的[keyword](http://cwiki.apachecn.org/display/Elasticsearch/KeyWord)或[numeric](http://cwiki.apachecn.org/display/Elasticsearch/Numeric)字段并开启[doc_values](http://cwiki.apachecn.org/pages/viewpage.action?pageId=10027604)(文档值)。 **注意:    **折叠只应用于顶部文档,而且不会影响聚合。 # 扩大折叠的显示结果 它也可以通过*inner_hits*参数来扩大每个折叠的顶部文档。 GET /twitter/tweet/_search {     "query": {         "match": {             "message": "elasticsearch"         }     },     "collapse" : {         "field" : "user",![](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/images/icons/callouts/1.png)         "inner_hits": {             "name": "last_tweets",![](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/images/icons/callouts/2.png)             "size": 5,![](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/images/icons/callouts/3.png)             "sort": [{ "date": "asc" }]![](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/images/icons/callouts/4.png)         },         "max_concurrent_group_searches": 4![](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/images/icons/callouts/5.png)     },     "sort": ["likes"] } [![](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/images/icons/callouts/1.png)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO28-1)    使用"user"字段折叠结果集 * * * [![](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/images/icons/callouts/2.png)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO28-2)    name参数用来分割结果响应中的inner_hits * * * [![](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/images/icons/callouts/3.png)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO28-3)    每个折叠内inner_hits检索到的数目 * * * [![](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/images/icons/callouts/4.png)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO28-4)    每个分组的排序方式 * * * [![](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/images/icons/callouts/5.png)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO28-5)    每个分组内允许检索inner_hits的并发请求数量 查看[inner hits ](http://cwiki.apachecn.org/display/Elasticsearch/Inner+hits)对于所支持的选项的完整列表和响应的格式。 该分组的扩展是通过向响应中返回的每个折叠的命中发送一个额外的查询来完成的。请求参数 `max_concurrent_group_searches` 可用于控制这个阶段并发搜索的最大数量。 默认值是基于数据节点的数量和默认的搜索线程池大小。 ****警告:**     **`collapse(折叠)不能用于与[scroll](http://cwiki.apachecn.org/display/Elasticsearch/Scroll), [rescore](http://cwiki.apachecn.org/display/Elasticsearch/Rescoring), [search after](http://cwiki.apachecn.org/display/Elasticsearch/Search+After) 一同使用。`