generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 620
Collapsing search results new page added to documentation #7919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vagimeli
merged 35 commits into
opensearch-project:main
from
leanneeliatra:collapse-search-results-#7507
Sep 24, 2024
Merged
Changes from 10 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
23e0e89
adding documentation for collapsing search results
leanneeliatra 3b21a0c
Merge branch 'main' into collapse-search-results-#7507
leanneeliatra 53f5f3f
Merge branch 'main' into collapse-search-results-#7507
leanneeliatra 3ab36b1
Clarifying collapsing of search results
leanneeliatra fa3ffbd
updating collapsing example as per request
leanneeliatra 84bec0d
updates as per reviewdog
leanneeliatra 18befbb
updates as per review dog
leanneeliatra d040c7d
Merge branch 'main' into collapse-search-results-#7507
leanneeliatra a5a1a5a
remove unneeded space
leanneeliatra b7a0f88
Merge branch 'main' into collapse-search-results-#7507
vagimeli d3b6fe6
Update _search-plugins/collapse-search.md
leanneeliatra bdedb11
Update _search-plugins/collapse-search.md
leanneeliatra 856b6c6
Update _search-plugins/collapse-search.md
leanneeliatra ad12cd8
Update _search-plugins/collapse-search.md
leanneeliatra 1d1e5ca
Apply suggestions from code review
leanneeliatra 9942b78
Update _search-plugins/collapse-search.md
vagimeli 587e3b6
Update _search-plugins/collapse-search.md
vagimeli 6d1cc8f
Update _search-plugins/collapse-search.md
vagimeli df62330
Update _search-plugins/collapse-search.md
vagimeli 9544dcd
Update _search-plugins/collapse-search.md
vagimeli c74e350
Update _search-plugins/collapse-search.md
vagimeli 80c8b03
Update _search-plugins/collapse-search.md
vagimeli 38a6cd8
Update _search-plugins/collapse-search.md
vagimeli c8cac89
Update _search-plugins/collapse-search.md
vagimeli 0c68494
Update _search-plugins/collapse-search.md
vagimeli 3df37b8
Update _search-plugins/collapse-search.md
vagimeli 535f77a
Update _search-plugins/collapse-search.md
vagimeli b606e21
Update _search-plugins/collapse-search.md
leanneeliatra 63a62e3
Merge branch 'main' into collapse-search-results-#7507
leanneeliatra 5dbb1c6
Review suggestions addressed
leanneeliatra 5535fcf
update to language
leanneeliatra 4e6c712
update to language from review
leanneeliatra cf38f29
Merge branch 'main' into collapse-search-results-#7507
leanneeliatra 224721d
Merge branch 'main' into collapse-search-results-#7507
vagimeli d2e728f
Merge branch 'main' into collapse-search-results-#7507
vagimeli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,228 @@ | ||
| --- | ||
| layout: default | ||
| title: Collapse search results | ||
| nav_order: 3 | ||
| --- | ||
|
|
||
| # Collapse search results | ||
|
|
||
| The collapse parameter in OpenSearch enables you to group search results by a particular field value, returning only the top document within each group. This feature is useful for reducing redundancy in search results by eliminating duplicates. | ||
|
|
||
| The collapse feature requires the field to be collapsed to be either a `keyword` or a `numeric` type. | ||
leanneeliatra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
leanneeliatra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ## Example of collapsing search results | ||
leanneeliatra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| To populate our index with the data needed, we will define our index mappings and define an `item` field indexed as a `keyword`. | ||
leanneeliatra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| #### Define the index mappings | ||
vagimeli marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```json | ||
| PUT /bakery-items | ||
| { | ||
| "mappings": { | ||
| "properties": { | ||
| "item": { | ||
| "type": "keyword" | ||
| }, | ||
| "category": { | ||
| "type": "keyword" | ||
| }, | ||
| "price": { | ||
| "type": "float" | ||
| }, | ||
| "baked_date": { | ||
| "type": "date" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| #### Populate the index | ||
vagimeli marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```json | ||
| POST /bakery-items/_bulk | ||
| { "index": {} } | ||
| { "item": "Chocolate Cake", "category": "cakes", "price": 15, "baked_date": "2023-07-01T00:00:00Z" } | ||
| { "index": {} } | ||
| { "item": "Chocolate Cake", "category": "cakes", "price": 18, "baked_date": "2023-07-04T00:00:00Z" } | ||
| { "index": {} } | ||
| { "item": "Vanilla Cake", "category": "cakes", "price": 12, "baked_date": "2023-07-02T00:00:00Z" } | ||
| ``` | ||
|
|
||
| #### Search the index, returning all results | ||
|
|
||
| ```json | ||
| GET /bakery-items/_search | ||
| { | ||
| "query": { | ||
| "match": { | ||
| "category": "cakes" | ||
| } | ||
| }, | ||
| "sort": ["price"] | ||
| } | ||
| ``` | ||
|
|
||
| The previous query will return the uncollapsed search results, showing all the documents, including both entries for "Chocolate Cake." | ||
leanneeliatra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| #### Search the index and collapse the results | ||
|
|
||
| To collapse search results by the `item` field and sort them by `price`, you can use the following query: | ||
leanneeliatra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| **Search query that collapses the results on the item field** | ||
leanneeliatra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```json | ||
| GET /bakery-items/_search | ||
| { | ||
| "query": { | ||
| "match": { | ||
| "category": "cakes" | ||
| } | ||
| }, | ||
| "collapse": { | ||
| "field": "item" | ||
| }, | ||
| "sort": ["price"] | ||
| } | ||
| ``` | ||
|
|
||
| **Results collapsed on item field** | ||
leanneeliatra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```json | ||
| { | ||
| "took": 3, | ||
| "timed_out": false, | ||
| "_shards": { | ||
| "total": 1, | ||
| "successful": 1, | ||
| "skipped": 0, | ||
| "failed": 0 | ||
| }, | ||
| "hits": { | ||
| "total": { | ||
| "value": 4, | ||
| "relation": "eq" | ||
| }, | ||
| "max_score": null, | ||
| "hits": [ | ||
| { | ||
| "_index": "bakery-items", | ||
| "_id": "mISga5EB2HLDXHkv9kAr", | ||
| "_score": null, | ||
| "_source": { | ||
| "item": "Vanilla Cake", | ||
| "category": "cakes", | ||
| "price": 12, | ||
| "baked_date": "2023-07-02T00:00:00Z", | ||
| "baker": "Baker A" | ||
| }, | ||
| "fields": { | ||
| "item": [ | ||
| "Vanilla Cake" | ||
| ] | ||
| }, | ||
| "sort": [ | ||
| 12 | ||
| ] | ||
| }, | ||
| { | ||
| "_index": "bakery-items", | ||
| "_id": "loSga5EB2HLDXHkv9kAr", | ||
| "_score": null, | ||
| "_source": { | ||
| "item": "Chocolate Cake", | ||
| "category": "cakes", | ||
| "price": 15, | ||
| "baked_date": "2023-07-01T00:00:00Z", | ||
| "baker": "Baker A" | ||
| }, | ||
| "fields": { | ||
| "item": [ | ||
| "Chocolate Cake" | ||
| ] | ||
| }, | ||
| "sort": [ | ||
| 15 | ||
| ] | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| The collapsed search results example will show only one "Chocolate Cake" entry, demonstrating how collapsing works in reducing redundancy. | ||
vagimeli marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
leanneeliatra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Collapsing 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 collapsing is applied, including duplicates. However, the response doesn't tell you the exact number of unique groups formed by collapsing. | ||
vagimeli marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
leanneeliatra marked this conversation as resolved.
Show resolved
Hide resolved
leanneeliatra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ### Expanding collapsed results | ||
leanneeliatra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| You can expand each collapsed top hit with the `inner_hits` property. | ||
|
|
||
| To demonstrate multiple inner hits, we'll retrieve the cheapest and most recent items for each type of cake. | ||
leanneeliatra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```json | ||
| GET /bakery-items/_search | ||
| { | ||
| "query": { | ||
| "match": { | ||
| "category": "cakes" | ||
| } | ||
| }, | ||
| "collapse": { | ||
| "field": "item", | ||
| "inner_hits": [ | ||
| { | ||
| "name": "cheapest_items", | ||
| "size": 1, | ||
| "sort": ["price"] | ||
| }, | ||
| { | ||
| "name": "newest_items", | ||
| "size": 1, | ||
| "sort": [{ "baked_date": "desc" }] | ||
| } | ||
| ] | ||
| }, | ||
| "sort": ["price"] | ||
| } | ||
|
|
||
| ``` | ||
|
|
||
| ### 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, you could request the three least expensive items and the three most recent items for every bakery item. | ||
leanneeliatra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
leanneeliatra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ```json | ||
| GET /bakery-items/_search | ||
| { | ||
| "query": { | ||
| "match": { | ||
| "category": "cakes" | ||
| } | ||
| }, | ||
| "collapse": { | ||
| "field": "item", | ||
| "inner_hits": [ | ||
| { | ||
| "name": "cheapest_items", | ||
| "size": 1, | ||
| "sort": ["price"] | ||
| }, | ||
| { | ||
| "name": "newest_items", | ||
| "size": 3, | ||
| "sort": [{ "baked_date": "desc" }] | ||
| } | ||
| ] | ||
| }, | ||
| "sort": ["price"] | ||
| } | ||
|
|
||
|
|
||
| ``` | ||
| 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 3 cheapest items and the top 3 most recent items, sorted by the `baked_date` in descending order. | ||
leanneeliatra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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 down the process if there are 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. | ||
leanneeliatra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
leanneeliatra marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| By using collapsing and inner hits effectively, you can manage large datasets in your bakery inventory, reduce redundancy, and focus on the most relevant information. This technique helps streamline search results, providing a clear and concise view of your data. | ||
leanneeliatra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.