2017-01-23 10:33:51 -05:00
[[search-request-collapse]]
2017-01-30 05:47:05 -05:00
=== Field Collapsing
2017-01-23 10:33:51 -05:00
Allows to collapse search results based on field values.
The collapsing is done by selecting only the top sorted document per collapse key.
For instance the query below retrieves the best tweet for each user and sorts them by number of likes.
[source,js]
--------------------------------------------------
2017-12-14 11:47:53 -05:00
GET /twitter/_search
2017-01-23 10:33:51 -05:00
{
"query": {
"match": {
"message": "elasticsearch"
}
},
"collapse" : {
"field" : "user" <1>
},
"sort": ["likes"], <2>
"from": 10 <3>
}
--------------------------------------------------
// CONSOLE
// TEST[setup:twitter]
<1> collapse the result set using the "user" field
<2> sort the top docs by number of likes
<3> define the offset of the first collapsed result
WARNING: The total number of hits in the response indicates the number of matching documents without collapsing.
The total number of distinct group is unknown.
2017-01-30 07:57:28 -05:00
The field used for collapsing must be a single valued <<keyword, `keyword`>> or <<number, `numeric`>> field with <<doc-values, `doc_values`>> activated
2017-01-23 10:33:51 -05:00
NOTE: The collapsing is applied to the top hits only and does not affect aggregations.
2017-01-30 05:47:05 -05:00
==== Expand collapse results
2017-01-23 10:33:51 -05:00
It is also possible to expand each collapsed top hits with the `inner_hits` option.
[source,js]
--------------------------------------------------
2017-12-14 11:47:53 -05:00
GET /twitter/_search
2017-01-23 10:33:51 -05:00
{
"query": {
"match": {
"message": "elasticsearch"
}
},
"collapse" : {
"field" : "user", <1>
"inner_hits": {
"name": "last_tweets", <2>
"size": 5, <3>
"sort": [{ "date": "asc" }] <4>
2017-02-09 12:06:10 -05:00
},
"max_concurrent_group_searches": 4 <5>
2017-01-23 10:33:51 -05:00
},
"sort": ["likes"]
}
--------------------------------------------------
// CONSOLE
// TEST[setup:twitter]
<1> collapse the result set using the "user" field
<2> the name used for the inner hit section in the response
<3> the number of inner_hits to retrieve per collapse key
<4> how to sort the document inside each group
2017-02-09 12:06:10 -05:00
<5> the number of concurrent requests allowed to retrieve the inner_hits` per group
2017-01-23 10:33:51 -05:00
See <<search-request-inner-hits, inner hits>> for the complete list of supported options and the format of the response.
2017-05-05 13:59:11 -04:00
It is also possible to request multiple `inner_hits` for each collapsed hit. This can be useful when you want to get
multiple representations of the collapsed hits.
[source,js]
--------------------------------------------------
2017-12-14 11:47:53 -05:00
GET /twitter/_search
2017-05-05 13:59:11 -04:00
{
"query": {
"match": {
"message": "elasticsearch"
}
},
"collapse" : {
"field" : "user", <1>
"inner_hits": [
{
"name": "most_liked", <2>
"size": 3,
"sort": ["likes"]
},
{
"name": "most_recent", <3>
"size": 3,
"sort": [{ "date": "asc" }]
}
]
},
"sort": ["likes"]
}
--------------------------------------------------
// CONSOLE
// TEST[setup:twitter]
<1> collapse the result set using the "user" field
<2> return the three most liked tweets for the user
<3> return the three most recent tweets for the user
2017-02-09 12:06:10 -05:00
The expansion of the group is done by sending an additional query for each
2017-05-05 13:59:11 -04:00
`inner_hit` request for each collapsed hit returned in the response. This can significantly slow things down
if you have too many groups and/or `inner_hit` requests.
2017-02-09 12:06:10 -05:00
The `max_concurrent_group_searches` request parameter can be used to control
the maximum number of concurrent searches allowed in this phase.
The default is based on the number of data nodes and the default search thread pool size.
2017-01-23 10:33:51 -05:00
WARNING: `collapse` cannot be used in conjunction with <<search-request-scroll, scroll>>,
<<search-request-rescore, rescore>> or <<search-request-search-after, search after>>.
2018-07-13 11:40:03 -04:00
==== Second level of collapsing
Second level of collapsing is also supported and is applied to `inner_hits`.
For example, the following request finds the top scored tweets for
each country, and within each country finds the top scored tweets
for each user.
[source,js]
--------------------------------------------------
GET /twitter/_search
{
"query": {
"match": {
"message": "elasticsearch"
}
},
"collapse" : {
"field" : "country",
"inner_hits" : {
"name": "by_location",
"collapse" : {"field" : "user"},
"size": 3
}
}
}
--------------------------------------------------
// NOTCONSOLE
Response:
[source,js]
--------------------------------------------------
{
...
"hits": [
{
"_index": "twitter",
"_type": "_doc",
"_id": "9",
"_score": ...,
"_source": {...},
"fields": {"country": ["UK"]},
"inner_hits":{
"by_location": {
"hits": {
...,
"hits": [
{
...
"fields": {"user" : ["user124"]}
},
{
...
"fields": {"user" : ["user589"]}
},
{
...
"fields": {"user" : ["user001"]}
}
]
}
}
}
},
{
"_index": "twitter",
"_type": "_doc",
"_id": "1",
"_score": ..,
"_source": {...},
"fields": {"country": ["Canada"]},
"inner_hits":{
"by_location": {
"hits": {
...,
"hits": [
{
...
"fields": {"user" : ["user444"]}
},
{
...
"fields": {"user" : ["user1111"]}
},
{
...
"fields": {"user" : ["user999"]}
}
]
}
}
}
},
....
]
}
--------------------------------------------------
// NOTCONSOLE
2018-08-28 07:16:43 -04:00
NOTE: Second level of collapsing doesn't allow `inner_hits`.