Collapse search results

You can use the collapse parameter to collapse search results based on field values. The collapsing is done by selecting only the top sorted document per collapse key.

For example, the following search collapses results by user.id and sorts them by http.response.bytes.

  1. GET /my-index-000001/_search
  2. {
  3. "query": {
  4. "match": {
  5. "message": "GET /search"
  6. }
  7. },
  8. "collapse": {
  9. "field": "user.id"
  10. },
  11. "sort": [ "http.response.bytes" ],
  12. "from": 10
  13. }

Collapse the result set using the “user.id” field

Sort the results by http.response.bytes

define the offset of the first collapsed result

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 or numeric field with doc_values activated

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.

  1. GET /my-index-000001/_search
  2. {
  3. "query": {
  4. "match": {
  5. "message": "GET /search"
  6. }
  7. },
  8. "collapse": {
  9. "field": "user.id",
  10. "inner_hits": {
  11. "name": "most_recent",
  12. "size": 5,
  13. "sort": [ { "@timestamp": "asc" } ]
  14. },
  15. "max_concurrent_group_searches": 4
  16. },
  17. "sort": [ "http.response.bytes" ]
  18. }

collapse the result set using the “user.id” field

the name used for the inner hit section in the response

the number of inner_hits to retrieve per collapse key

how to sort the document inside each group

the number of concurrent requests allowed to retrieve the inner_hits per group

See inner hits for the complete list of supported options and the format of the response.

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.

  1. GET /my-index-000001/_search
  2. {
  3. "query": {
  4. "match": {
  5. "message": "GET /search"
  6. }
  7. },
  8. "collapse": {
  9. "field": "user.id",
  10. "inner_hits": [
  11. {
  12. "name": "largest_responses",
  13. "size": 3,
  14. "sort": [ "http.response.bytes" ]
  15. },
  16. {
  17. "name": "most_recent",
  18. "size": 3,
  19. "sort": [ { "@timestamp": "asc" } ]
  20. }
  21. ]
  22. },
  23. "sort": [ "http.response.bytes" ]
  24. }

collapse the result set using the “user.id” field

return the three largest HTTP responses for the user

return the three most recent HTTP responses for the user

The expansion of the group is done by sending an additional query for each 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.

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.

collapse cannot be used in conjunction with scroll, rescore or search after.

Second level of collapsing

Second level of collapsing is also supported and is applied to inner_hits.

For example, the following search collapses results by geo.country_name. Within each geo.country_name, inner hits are collapsed by user.id.

  1. GET /my-index-000001/_search
  2. {
  3. "query": {
  4. "match": {
  5. "message": "GET /search"
  6. }
  7. },
  8. "collapse": {
  9. "field": "geo.country_name",
  10. "inner_hits": {
  11. "name": "by_location",
  12. "collapse": { "field": "user.id" },
  13. "size": 3
  14. }
  15. }
  16. }

Response:

  1. {
  2. ...
  3. "hits": [
  4. {
  5. "_index": "my-index-000001",
  6. "_type": "_doc",
  7. "_id": "9",
  8. "_score": ...,
  9. "_source": {...},
  10. "fields": { "geo": { "country_name": [ "UK" ] }},
  11. "inner_hits": {
  12. "by_location": {
  13. "hits": {
  14. ...,
  15. "hits": [
  16. {
  17. ...
  18. "fields": { "user": "id": { [ "user124" ] }}
  19. },
  20. {
  21. ...
  22. "fields": { "user": "id": { [ "user589" ] }}
  23. },
  24. {
  25. ...
  26. "fields": { "user": "id": { [ "user001" ] }}
  27. }
  28. ]
  29. }
  30. }
  31. }
  32. },
  33. {
  34. "_index": "my-index-000001",
  35. "_type": "_doc",
  36. "_id": "1",
  37. "_score": ..,
  38. "_source": {...
  39. },
  40. "fields": { "geo": { "country_name": [ "Canada" ] }},
  41. "inner_hits": {
  42. "by_location": {
  43. "hits": {
  44. ...,
  45. "hits": [
  46. {
  47. ...
  48. "fields": { "user": "id": { [ "user444" ] }}
  49. },
  50. {
  51. ...
  52. "fields": { "user": "id": { [ "user1111" ] }
  53. },
  54. {
  55. ...
  56. "fields": { "user": "id": { [ "user999" ] }}
  57. }
  58. ]
  59. }
  60. }
  61. }
  62. },
  63. ...
  64. ]
  65. }

Second level of collapsing doesn’t allow inner_hits.