Skip to content
Merged
Changes from 18 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 Aug 2, 2024
3db58b6
Update _search-plugins/filter-search.md
hdhalter Aug 2, 2024
8f35e20
Merge branch 'main' into adding-filter-search-results
leanneeliatra Aug 6, 2024
ab29e6a
reviewdog updates
leanneeliatra Aug 6, 2024
03e8674
Merge branch 'main' into adding-filter-search-results
leanneeliatra Aug 7, 2024
434aafb
Merge branch 'main' into adding-filter-search-results
leanneeliatra Aug 15, 2024
f7d4cd0
Merge branch 'main' into adding-filter-search-results
leanneeliatra Aug 20, 2024
10f9f8f
Merge branch 'main' into adding-filter-search-results
vagimeli Aug 28, 2024
c88e601
Merge branch 'main' into adding-filter-search-results
leanneeliatra Aug 29, 2024
c625110
Update filter-search.md
vagimeli Sep 3, 2024
59d3f09
Merge branch 'main' into adding-filter-search-results
vagimeli Sep 3, 2024
f952109
Merge branch 'main' into adding-filter-search-results
leanneeliatra Sep 10, 2024
5674a02
Merge branch 'main' into adding-filter-search-results
leanneeliatra Sep 23, 2024
f923669
Merge branch 'main' into adding-filter-search-results
vagimeli Sep 24, 2024
15b97e6
Merge branch 'main' into adding-filter-search-results
vagimeli Sep 24, 2024
71b640d
Merge branch 'main' into adding-filter-search-results
vagimeli Oct 8, 2024
1a23b50
Incorporating review comments.
leanneeliatra Oct 15, 2024
8aa29b6
Update _search-plugins/filter-search.md
vagimeli Oct 15, 2024
28aa7cd
Update _search-plugins/filter-search.md
vagimeli Oct 15, 2024
10f0181
Update filter-search.md
vagimeli Oct 15, 2024
bdf6042
Update _search-plugins/filter-search.md
vagimeli Oct 15, 2024
85e90ce
Merge branch 'main' into adding-filter-search-results
vagimeli Oct 15, 2024
3017860
Update _search-plugins/filter-search.md
vagimeli Oct 16, 2024
7552312
Update _search-plugins/filter-search.md
vagimeli Oct 16, 2024
24fddb0
Update _search-plugins/filter-search.md
vagimeli Oct 16, 2024
1efb63b
Update _search-plugins/filter-search.md
vagimeli Oct 16, 2024
c04ef8f
Update _search-plugins/filter-search.md
vagimeli Oct 16, 2024
7f1f9b7
Update _search-plugins/filter-search.md
vagimeli Oct 16, 2024
d9f1347
Update _search-plugins/filter-search.md
vagimeli Oct 16, 2024
865da34
Update _search-plugins/filter-search.md
vagimeli Oct 16, 2024
e37867a
Update _search-plugins/filter-search.md
vagimeli Oct 16, 2024
c444b4a
Update _search-plugins/filter-search.md
vagimeli Oct 16, 2024
77100a2
Update _search-plugins/filter-search.md
vagimeli Oct 16, 2024
05211a3
Merge branch 'main' into adding-filter-search-results
vagimeli Oct 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions _search-plugins/filter-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
---
layout: default
title: Filter search results
nav_order: 36
---

# Filter search results

You can filter search using different methods, each suited to specific scenarios. You can apply filters at the query level, using `boolean` query clauses, as well as by using `post_filter` and `aggregation` level filters.

## Query-level filtering with boolean queries

Check failure on line 11 in _search-plugins/filter-search.md

View workflow job for this annotation

GitHub Actions / style-job

[vale] reported by reviewdog 🐶 [Vale.Terms] Use 'Boolean' instead of 'boolean'. Raw Output: {"message": "[Vale.Terms] Use 'Boolean' instead of 'boolean'.", "location": {"path": "_search-plugins/filter-search.md", "range": {"start": {"line": 11, "column": 31}}}, "severity": "ERROR"}

Check failure on line 11 in _search-plugins/filter-search.md

View workflow job for this annotation

GitHub Actions / style-job

[vale] reported by reviewdog 🐶 [OpenSearch.Spelling] Error: boolean. If you are referencing a setting, variable, format, function, or repository, surround it with tic marks. Raw Output: {"message": "[OpenSearch.Spelling] Error: boolean. If you are referencing a setting, variable, format, function, or repository, surround it with tic marks.", "location": {"path": "_search-plugins/filter-search.md", "range": {"start": {"line": 11, "column": 31}}}, "severity": "ERROR"}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Query-level filtering with boolean queries
## 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.

Create an index `electronics` and provide the mapping:

```
PUT /electronics
{
"mappings": {
"properties": {
"brand": { "type": "keyword" },
"category": { "type": "keyword" },
"price": { "type": "float" },
"features": { "type": "keyword" }
}
}
}
```

Add documents to the `electronics` index:

```
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"]
}
```

Apply a `boolean` filter query to display only `smartphones` from `BrandA`:

```
GET /electronics/_search
{
"query": {
"bool": {
"filter": [
{ "term": { "brand": "BrandA" }},
{ "term": { "category": "Smartphone" }}
]
}
}
}
```

## Using `post-filter` to narrow results without affecting aggregations

Use `post_filter` to limit search hits, while maintaining all aggregation options. For example, if a shopper selects `BrandA`, you can filter results to show only `BrandA` products while keeping all brands visible in the aggregation.

```
GET /electronics/_search
{
"query": {
"bool": {
"filter": { "term": { "category": "Smartphone" }}
}
},
"aggs": {
"brands": {
"terms": { "field": "brand" }
}
},
"post_filter": {
"term": { "brand": "BrandA" }
}
}
```
This shows `BrandA` smartphones in the search hits while still displaying all brands in the aggregations.

## Aggregation-level filtering to refine aggregations
We can use aggregation-level filtering to apply filters to specific aggregations, without affecting the overall aggregation.

Use aggregation-level filtering to filter the `price_ranges` aggregation based on selected brands, `BrandA` and `BrandB`, without affecting the main `price_ranges` aggregation. This allows you to display price ranges relevant to the selected brands while still showing overall price ranges for all products.

```
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 }
]
}
}
}
}
}
}
```

## Summary of filters used for filtering the search results:
1. Query-level filtering: Apply `boolean` query filter clauses to filter both search hits and aggregations, such as narrowing results to specific categories or brands.
2. Post-filtering: Use `post_filter` to refine search hits based on user selections, while keeping all aggregation options, like brands, visible and unaffected.
3. Aggregation-level filtering: Adjust specific aggregations, like price ranges, based on selected filters without impacting other aggregations, such as the brand list.

Loading