Skip to content

Commit 2535af9

Browse files
Collapsing search results new page added to documentation (#7919) (#8361)
1 parent 39de58b commit 2535af9

File tree

1 file changed

+231
-0
lines changed

1 file changed

+231
-0
lines changed

_search-plugins/collapse-search.md

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
---
2+
layout: default
3+
title: Collapse search results
4+
nav_order: 3
5+
---
6+
7+
# Collapse search results
8+
9+
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.
10+
11+
The `collapse` parameter requires the field being collapsed to be of either a `keyword` or a `numeric` type.
12+
13+
---
14+
15+
## Collapsing search results
16+
17+
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.
18+
19+
#### Define index mappings
20+
21+
```json
22+
PUT /bakery-items
23+
{
24+
"mappings": {
25+
"properties": {
26+
"item": {
27+
"type": "keyword"
28+
},
29+
"category": {
30+
"type": "keyword"
31+
},
32+
"price": {
33+
"type": "float"
34+
},
35+
"baked_date": {
36+
"type": "date"
37+
}
38+
}
39+
}
40+
}
41+
```
42+
43+
#### Populate an index
44+
45+
```json
46+
POST /bakery-items/_bulk
47+
{ "index": {} }
48+
{ "item": "Chocolate Cake", "category": "cakes", "price": 15, "baked_date": "2023-07-01T00:00:00Z" }
49+
{ "index": {} }
50+
{ "item": "Chocolate Cake", "category": "cakes", "price": 18, "baked_date": "2023-07-04T00:00:00Z" }
51+
{ "index": {} }
52+
{ "item": "Vanilla Cake", "category": "cakes", "price": 12, "baked_date": "2023-07-02T00:00:00Z" }
53+
```
54+
55+
#### Search the index, returning all results
56+
57+
```json
58+
GET /bakery-items/_search
59+
{
60+
"query": {
61+
"match": {
62+
"category": "cakes"
63+
}
64+
},
65+
"sort": ["price"]
66+
}
67+
```
68+
69+
This query returns the uncollapsed search results, showing all documents, including both entries for "Chocolate Cake".
70+
71+
#### Search the index and collapse the results
72+
73+
To group search results by the `item` field and sort them by `price`, you can use the following query:
74+
75+
**Collapsed `item` field search results**
76+
77+
```json
78+
GET /bakery-items/_search
79+
{
80+
"query": {
81+
"match": {
82+
"category": "cakes"
83+
}
84+
},
85+
"collapse": {
86+
"field": "item"
87+
},
88+
"sort": ["price"]
89+
}
90+
```
91+
92+
**Response**
93+
94+
```json
95+
{
96+
"took": 3,
97+
"timed_out": false,
98+
"_shards": {
99+
"total": 1,
100+
"successful": 1,
101+
"skipped": 0,
102+
"failed": 0
103+
},
104+
"hits": {
105+
"total": {
106+
"value": 4,
107+
"relation": "eq"
108+
},
109+
"max_score": null,
110+
"hits": [
111+
{
112+
"_index": "bakery-items",
113+
"_id": "mISga5EB2HLDXHkv9kAr",
114+
"_score": null,
115+
"_source": {
116+
"item": "Vanilla Cake",
117+
"category": "cakes",
118+
"price": 12,
119+
"baked_date": "2023-07-02T00:00:00Z",
120+
"baker": "Baker A"
121+
},
122+
"fields": {
123+
"item": [
124+
"Vanilla Cake"
125+
]
126+
},
127+
"sort": [
128+
12
129+
]
130+
},
131+
{
132+
"_index": "bakery-items",
133+
"_id": "loSga5EB2HLDXHkv9kAr",
134+
"_score": null,
135+
"_source": {
136+
"item": "Chocolate Cake",
137+
"category": "cakes",
138+
"price": 15,
139+
"baked_date": "2023-07-01T00:00:00Z",
140+
"baker": "Baker A"
141+
},
142+
"fields": {
143+
"item": [
144+
"Chocolate Cake"
145+
]
146+
},
147+
"sort": [
148+
15
149+
]
150+
}
151+
]
152+
}
153+
}
154+
```
155+
156+
The collapsed search results will show only one "Chocolate Cake" entry, demonstrating how the `collapse` parameter reduces redundancy.
157+
158+
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.
159+
160+
---
161+
162+
## Expanding collapsed results
163+
164+
You can expand each collapsed top hit with the `inner_hits` property.
165+
166+
The following example request applies `inner_hits` to retrieve the lowest-priced and most recent item, for each type of cake:
167+
168+
```json
169+
GET /bakery-items/_search
170+
{
171+
"query": {
172+
"match": {
173+
"category": "cakes"
174+
}
175+
},
176+
"collapse": {
177+
"field": "item",
178+
"inner_hits": [
179+
{
180+
"name": "cheapest_items",
181+
"size": 1,
182+
"sort": ["price"]
183+
},
184+
{
185+
"name": "newest_items",
186+
"size": 1,
187+
"sort": [{ "baked_date": "desc" }]
188+
}
189+
]
190+
},
191+
"sort": ["price"]
192+
}
193+
194+
```
195+
196+
### Multiple inner hits for each collapsed hit
197+
198+
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:
199+
200+
```json
201+
GET /bakery-items/_search
202+
{
203+
"query": {
204+
"match": {
205+
"category": "cakes"
206+
}
207+
},
208+
"collapse": {
209+
"field": "item",
210+
"inner_hits": [
211+
{
212+
"name": "cheapest_items",
213+
"size": 1,
214+
"sort": ["price"]
215+
},
216+
{
217+
"name": "newest_items",
218+
"size": 3,
219+
"sort": [{ "baked_date": "desc" }]
220+
}
221+
]
222+
},
223+
"sort": ["price"]
224+
}
225+
226+
227+
```
228+
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.
229+
230+
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.
231+

0 commit comments

Comments
 (0)