OpenSearch/docs/reference/search/request/collapse.asciidoc
Jim Ferenczi e48bc2eed7 Add field collapsing for search request (#22337)
* Add top hits collapsing to search request

The field collapsing is done with a custom top docs collector that "collapse" search hits with same field value.
The distributed aspect is resolve using the two passes that the regular search uses. The first pass "collapse" the top hits, then the coordinating node merge/collapse the top hits from each shard.

```
GET _search
{
   "collapse": {
      "field": "category",
   }
}
```

This change also adds an ExpandCollapseSearchResponseListener that intercepts the search response and expands collapsed hits using the CollapseBuilder#innerHit} options.
The retrieval of each inner_hits is done by sending a query to all shards filtered by the collapse key.

```
GET _search
{
   "collapse": {
      "field": "category",
      "inner_hits": {
	"size": 2
      }
   }
}
```
2017-01-23 16:33:51 +01:00

73 lines
2.3 KiB
Plaintext

[[search-request-collapse]]
== Collapse
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]
--------------------------------------------------
GET /twitter/tweet/_search
{
"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.
The field used for collapsing must be a single valued <<keyword, `keyword`> or <<number, `number`>> field with <<doc-values, `doc_values`>> activated
NOTE: The collapsing is applied to the top hits only and does not affect aggregations.
=== Expand collapse results
It is also possible to expand each collapsed top hits with the `inner_hits` option.
[source,js]
--------------------------------------------------
GET /twitter/tweet/_search
{
"query": {
"match": {
"message": "elasticsearch"
}
},
"collapse" : {
"field" : "user", <1>
"inner_hits": {
"name": "last_tweets", <2>
"size": 5, <3>
"sort": [{ "date": "asc" }] <4>
}
},
"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
See <<search-request-inner-hits, inner hits>> for the complete list of supported options and the format of the response.
WARNING: `collapse` cannot be used in conjunction with <<search-request-scroll, scroll>>,
<<search-request-rescore, rescore>> or <<search-request-search-after, search after>>.