# 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" ![](img/eea34d40d69568fde6b7f906a6f54fe4.jpg)
```

```
    },
```

```
    "sort": ["likes"], ![](img/c59efe79ee8017c8cc06a8d1f8762bba.jpg)
```

```
     "from": 10 ![](img/bcbebca4d761395d051445589f0f088c.jpg)
```

```
}
```

[![](img/eea34d40d69568fde6b7f906a6f54fe4.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO27-1)    使用user字段来折叠结果集

* * *

[![](img/c59efe79ee8017c8cc06a8d1f8762bba.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO27-2)    按照likes数量进行排序获取顶部文档

* * *

[![](img/bcbebca4d761395d051445589f0f088c.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",![](img/eea34d40d69568fde6b7f906a6f54fe4.jpg)

        "inner_hits": {

            "name": "last_tweets",![](img/c59efe79ee8017c8cc06a8d1f8762bba.jpg)

            "size": 5,![](img/bcbebca4d761395d051445589f0f088c.jpg)

            "sort": [{ "date": "asc" }]![](img/d7449d755ec194a56f64c0fa7c2ab9b1.jpg)

        },

        "max_concurrent_group_searches": 4![](img/29fbadd7b713d771c1a9624ae239bc95.jpg)

    },

    "sort": ["likes"]

}

[![](img/eea34d40d69568fde6b7f906a6f54fe4.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO28-1)    使用"user"字段折叠结果集

* * *

[![](img/c59efe79ee8017c8cc06a8d1f8762bba.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO28-2)    name参数用来分割结果响应中的inner_hits

* * *

[![](img/bcbebca4d761395d051445589f0f088c.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO28-3)    每个折叠内inner_hits检索到的数目

* * *

[![](img/d7449d755ec194a56f64c0fa7c2ab9b1.jpg)](https://www.elastic.co/guide/en/elasticsearch/reference/5.4/search-request-collapse.html#CO28-4)    每个分组的排序方式

* * *

[![](img/29fbadd7b713d771c1a9624ae239bc95.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) 一同使用。`