Collapse search results

The collapse parameter groups search results by a particular field value. This returns only the top document within each group, which helps reduce redundancy by eliminating duplicates.

The collapse parameter requires the field being collapsed to be of either a keyword or a numeric type.


Collapsing search results

To populate an index with data, define the index mappings and an item field indexed as a keyword. The following example request shows you how to define index mappings, populate an index, and then search it.

Define index mappings

  1. PUT /bakery-items
  2. {
  3. "mappings": {
  4. "properties": {
  5. "item": {
  6. "type": "keyword"
  7. },
  8. "category": {
  9. "type": "keyword"
  10. },
  11. "price": {
  12. "type": "float"
  13. },
  14. "baked_date": {
  15. "type": "date"
  16. }
  17. }
  18. }
  19. }

Populate an index

  1. POST /bakery-items/_bulk
  2. { "index": {} }
  3. { "item": "Chocolate Cake", "category": "cakes", "price": 15, "baked_date": "2023-07-01T00:00:00Z" }
  4. { "index": {} }
  5. { "item": "Chocolate Cake", "category": "cakes", "price": 18, "baked_date": "2023-07-04T00:00:00Z" }
  6. { "index": {} }
  7. { "item": "Vanilla Cake", "category": "cakes", "price": 12, "baked_date": "2023-07-02T00:00:00Z" }

Search the index, returning all results

  1. GET /bakery-items/_search
  2. {
  3. "query": {
  4. "match": {
  5. "category": "cakes"
  6. }
  7. },
  8. "sort": ["price"]
  9. }

This query returns the uncollapsed search results, showing all documents, including both entries for “Chocolate Cake”.

Search the index and collapse the results

To group search results by the item field and sort them by price, you can use the following query:

Collapsed item field search results

  1. GET /bakery-items/_search
  2. {
  3. "query": {
  4. "match": {
  5. "category": "cakes"
  6. }
  7. },
  8. "collapse": {
  9. "field": "item"
  10. },
  11. "sort": ["price"]
  12. }

Response

  1. {
  2. "took": 3,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 4,
  13. "relation": "eq"
  14. },
  15. "max_score": null,
  16. "hits": [
  17. {
  18. "_index": "bakery-items",
  19. "_id": "mISga5EB2HLDXHkv9kAr",
  20. "_score": null,
  21. "_source": {
  22. "item": "Vanilla Cake",
  23. "category": "cakes",
  24. "price": 12,
  25. "baked_date": "2023-07-02T00:00:00Z",
  26. "baker": "Baker A"
  27. },
  28. "fields": {
  29. "item": [
  30. "Vanilla Cake"
  31. ]
  32. },
  33. "sort": [
  34. 12
  35. ]
  36. },
  37. {
  38. "_index": "bakery-items",
  39. "_id": "loSga5EB2HLDXHkv9kAr",
  40. "_score": null,
  41. "_source": {
  42. "item": "Chocolate Cake",
  43. "category": "cakes",
  44. "price": 15,
  45. "baked_date": "2023-07-01T00:00:00Z",
  46. "baker": "Baker A"
  47. },
  48. "fields": {
  49. "item": [
  50. "Chocolate Cake"
  51. ]
  52. },
  53. "sort": [
  54. 15
  55. ]
  56. }
  57. ]
  58. }
  59. }

The collapsed search results will show only one “Chocolate Cake” entry, demonstrating how the collapse parameter reduces redundancy.

The collapse parameter affects only the top search results and does not change any aggregation results. The total number of hits shown in the response reflects all matching documents before the parameter is applied, including duplicates. However, the response doesn’t indicate the exact number of unique groups formed by the operation.


Expanding collapsed results

You can expand each collapsed top hit with the inner_hits property.

The following example request applies inner_hits to retrieve the lowest-priced and most recent item, for each type of cake:

  1. GET /bakery-items/_search
  2. {
  3. "query": {
  4. "match": {
  5. "category": "cakes"
  6. }
  7. },
  8. "collapse": {
  9. "field": "item",
  10. "inner_hits": [
  11. {
  12. "name": "cheapest_items",
  13. "size": 1,
  14. "sort": ["price"]
  15. },
  16. {
  17. "name": "newest_items",
  18. "size": 1,
  19. "sort": [{ "baked_date": "desc" }]
  20. }
  21. ]
  22. },
  23. "sort": ["price"]
  24. }

Multiple inner hits for each collapsed hit

To obtain several groups of inner hits for each collapsed result, you can set different criteria for each group. For example, lets request the three most recent items for every bakery item:

  1. GET /bakery-items/_search
  2. {
  3. "query": {
  4. "match": {
  5. "category": "cakes"
  6. }
  7. },
  8. "collapse": {
  9. "field": "item",
  10. "inner_hits": [
  11. {
  12. "name": "cheapest_items",
  13. "size": 1,
  14. "sort": ["price"]
  15. },
  16. {
  17. "name": "newest_items",
  18. "size": 3,
  19. "sort": [{ "baked_date": "desc" }]
  20. }
  21. ]
  22. },
  23. "sort": ["price"]
  24. }

This query searches for documents in the cakes category and groups the search results by the item_name field. For each item_name, it retrieves the top three lowest-priced items and the top three most recent items, sorted by baked_date in descending order.

You can expand the groups by sending an additional query for each inner hit request corresponding to each collapsed hit in the response. This can significantly slow down the process if there are too many groups 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.