-
Notifications
You must be signed in to change notification settings - Fork 509
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
Adding documentation for filter search in OpenSearch #7900
Merged
vagimeli
merged 34 commits into
opensearch-project:main
from
leanneeliatra:adding-filter-search-results
Oct 16, 2024
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
9235dac
adding documentation for filter search in OpenSearch
leanneeliatra 3db58b6
Update _search-plugins/filter-search.md
hdhalter 8f35e20
Merge branch 'main' into adding-filter-search-results
leanneeliatra ab29e6a
reviewdog updates
leanneeliatra 03e8674
Merge branch 'main' into adding-filter-search-results
leanneeliatra 434aafb
Merge branch 'main' into adding-filter-search-results
leanneeliatra f7d4cd0
Merge branch 'main' into adding-filter-search-results
leanneeliatra 10f9f8f
Merge branch 'main' into adding-filter-search-results
vagimeli c88e601
Merge branch 'main' into adding-filter-search-results
leanneeliatra c625110
Update filter-search.md
vagimeli 59d3f09
Merge branch 'main' into adding-filter-search-results
vagimeli f952109
Merge branch 'main' into adding-filter-search-results
leanneeliatra 5674a02
Merge branch 'main' into adding-filter-search-results
leanneeliatra f923669
Merge branch 'main' into adding-filter-search-results
vagimeli 15b97e6
Merge branch 'main' into adding-filter-search-results
vagimeli 71b640d
Merge branch 'main' into adding-filter-search-results
vagimeli 1a23b50
Incorporating review comments.
leanneeliatra 8aa29b6
Update _search-plugins/filter-search.md
vagimeli 28aa7cd
Update _search-plugins/filter-search.md
vagimeli 10f0181
Update filter-search.md
vagimeli bdf6042
Update _search-plugins/filter-search.md
vagimeli 85e90ce
Merge branch 'main' into adding-filter-search-results
vagimeli 3017860
Update _search-plugins/filter-search.md
vagimeli 7552312
Update _search-plugins/filter-search.md
vagimeli 24fddb0
Update _search-plugins/filter-search.md
vagimeli 1efb63b
Update _search-plugins/filter-search.md
vagimeli c04ef8f
Update _search-plugins/filter-search.md
vagimeli 7f1f9b7
Update _search-plugins/filter-search.md
vagimeli d9f1347
Update _search-plugins/filter-search.md
vagimeli 865da34
Update _search-plugins/filter-search.md
vagimeli e37867a
Update _search-plugins/filter-search.md
vagimeli c444b4a
Update _search-plugins/filter-search.md
vagimeli 77100a2
Update _search-plugins/filter-search.md
vagimeli 05211a3
Merge branch 'main' into adding-filter-search-results
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 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,151 @@ | ||
--- | ||
layout: default | ||
title: Filter search results | ||
nav_order: 36 | ||
--- | ||
|
||
# Filter search results | ||
|
||
You can filter searches using different methods, each suited to specific scenarios. You can apply filters at the query level, using `boolean` query clauses and `post_filter` and `aggregation` level filters, as follows: | ||
|
||
- **Query-level filtering:** Apply `boolean` query filter clauses to filter search hits and aggregations, such as to narrow results to specific categories or brands. | ||
- **Post-filter filtering:** Use `post_filter` to refine search hits based on user selections while preserving all aggregation options. | ||
- **Aggregation-level filtering:** Adjust specific aggregations based on selected filters without impacting other aggregations. | ||
|
||
## Query-level filtering with Boolean queries | ||
|
||
Use a `boolean` query with a filter clause to apply filters to both search hits and aggregations. For example, if a shopper searches for `smartphones` from `BrandA`, a Boolean query can restrict results to only those smartphones from `BrandA`. The following steps guide you through query-level filtering. | ||
|
||
1. Create an index `electronics` and provide the mapping using the following request: | ||
|
||
```json | ||
PUT /electronics | ||
{ | ||
"mappings": { | ||
"properties": { | ||
"brand": { "type": "keyword" }, | ||
"category": { "type": "keyword" }, | ||
"price": { "type": "float" }, | ||
"features": { "type": "keyword" } | ||
} | ||
} | ||
} | ||
``` | ||
{% include copy-curl.html %} | ||
|
||
2. Add documents to the `electronics` index using the following request: | ||
|
||
```json | ||
PUT /electronics/_doc/1?refresh | ||
{ | ||
"brand": "BrandA", | ||
"category": "Smartphone", | ||
"price": 699.99, | ||
"features": ["5G", "Dual Camera"] | ||
} | ||
PUT /electronics/_doc/2?refresh | ||
{ | ||
"brand": "BrandA", | ||
"category": "Laptop", | ||
"price": 1199.99, | ||
"features": ["Touchscreen", "16GB RAM"] | ||
} | ||
PUT /electronics/_doc/3?refresh | ||
{ | ||
"brand": "BrandB", | ||
"category": "Smartphone", | ||
"price": 799.99, | ||
"features": ["5G", "Triple Camera"] | ||
} | ||
``` | ||
{% include copy-curl.html %} | ||
|
||
3. Apply a `boolean` filter query to display only `smartphones` from `BrandA` using the following request: | ||
|
||
```json | ||
GET /electronics/_search | ||
{ | ||
"query": { | ||
"bool": { | ||
"filter": [ | ||
{ "term": { "brand": "BrandA" }}, | ||
{ "term": { "category": "Smartphone" }} | ||
] | ||
} | ||
} | ||
} | ||
``` | ||
{% include copy-curl.html %} | ||
|
||
## Narrowing results using `post-filter` while preserving aggregation visibility | ||
|
||
Use `post_filter` to limit search hits while preserving all aggregation options. For example, if a shopper selects `BrandA`, results are filtered to show only `BrandA` products while maintaining the visibility of all brand options in the aggregations, as shown in the following example request: | ||
|
||
```json | ||
GET /electronics/_search | ||
{ | ||
"query": { | ||
"bool": { | ||
"filter": { "term": { "category": "Smartphone" }} | ||
} | ||
}, | ||
"aggs": { | ||
"brands": { | ||
"terms": { "field": "brand" } | ||
} | ||
}, | ||
"post_filter": { | ||
"term": { "brand": "BrandA" } | ||
} | ||
} | ||
``` | ||
{% include copy-curl.html %} | ||
|
||
The result should show `BrandA` smartphones in the search hits and all brands in the aggregations. | ||
|
||
## Refining aggregations with aggregation-level filtering | ||
|
||
You can use aggregation-level filtering to apply filters to specific aggregations without affecting the main aggregation to which they belong. | ||
|
||
For example, you can use aggregation-level filtering to filter the `price_ranges` aggregation based on selected brands, `BrandA` and `BrandB`, without affecting the main `price_ranges` aggregation, as shown in the following example request. This displays price ranges relevant to the selected brands while also displaying overall price ranges for all products. | ||
|
||
```json | ||
GET /electronics/_search | ||
{ | ||
"query": { | ||
"bool": { | ||
"filter": { "term": { "category": "Smartphone" }} | ||
} | ||
}, | ||
"aggs": { | ||
"price_ranges": { | ||
"range": { | ||
"field": "price", | ||
"ranges": [ | ||
{ "to": 500 }, | ||
{ "from": 500, "to": 1000 }, | ||
{ "from": 1000 } | ||
] | ||
} | ||
}, | ||
"filtered_brands": { | ||
"filter": { | ||
"terms": { "brand": ["BrandA", "BrandB"] } | ||
}, | ||
"aggs": { | ||
"price_ranges": { | ||
"range": { | ||
"field": "price", | ||
"ranges": [ | ||
{ "to": 500 }, | ||
{ "from": 500, "to": 1000 }, | ||
{ "from": 1000 } | ||
] | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
{% include copy-curl.html %} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Above: "without affecting the main aggregation to which they belong"?