From 9235dac9738cc52a0601fde33879271ec5a37922 Mon Sep 17 00:00:00 2001 From: "leanne.laceybyrne@eliatra.com" Date: Fri, 2 Aug 2024 18:36:02 +0100 Subject: [PATCH 01/20] adding documentation for filter search in OpenSearch Signed-off-by: leanne.laceybyrne@eliatra.com --- _search-plugins/filter-search.md | 316 +++++++++++++++++++++++++++++++ 1 file changed, 316 insertions(+) create mode 100644 _search-plugins/filter-search.md diff --git a/_search-plugins/filter-search.md b/_search-plugins/filter-search.md new file mode 100644 index 0000000000..7bbc891052 --- /dev/null +++ b/_search-plugins/filter-search.md @@ -0,0 +1,316 @@ +--- +layout: default +title: Filter search results +nav_order: 36 +--- + +# Filter search results + +In OpenSearch, filtering search results can be achieved through two main approaches: using a [DSL boolean query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html) with a filter clause. The boolean query filtering approach applies filters to both search hits and aggregations. + +You can also filter search results with the `post_filter` parameter in the search API, which applies filters only to search hits, not aggregations. + +#### Table of contents +1. TOC + {:toc} + +--- + +## Using `post_filter` to filter search results + +Using the `post_filter` parameter to filter search results allows for calculating aggregations based on a broader result set before narrowing down the search hits. It can also improve relevance of results and reorder results by rescoring hits after applying the post filter. + +### Example of filtering search results + +1. Create an index of products + +``` +PUT /electronics +{ + "mappings": { + "properties": { + "brand": { "type": "keyword" }, + "category": { "type": "keyword" }, + "price": { "type": "float" }, + "features": { "type": "keyword" } + } + } +} +``` + +2. Index data: + +``` +PUT /electronics/_doc/1?refresh +{ + "brand": "BrandX", + "category": "Smartphone", + "price": 699.99, + "features": ["5G", "Dual Camera"] +} + +PUT /electronics/_doc/2?refresh +{ + "brand": "BrandX", + "category": "Laptop", + "price": 1199.99, + "features": ["Touchscreen", "16GB RAM"] +} + +PUT /electronics/_doc/3?refresh +{ + "brand": "BrandY", + "category": "Smartphone", + "price": 799.99, + "features": ["5G", "Triple Camera"] +} +``` + +3. Perform a `boolean filter` to show only smartphones from BrandX + +``` +GET /electronics/_search +{ + "query": { + "bool": { + "filter": [ + { "term": { "brand": "BrandX" }}, + { "term": { "category": "Smartphone" }} + ] + } + } +} +``` + +Alternatively, to refine search results further, for example, you may have a category field that allows users to limit their search results to BrandX smartphones or tablets, you can utilize a `terms aggregation`: + +``` +GET /electronics/_search +{ + "query": { + "bool": { + "filter": [ + { "term": { "brand": "BrandX" }}, + { "term": { "category": "Smartphone" }} + ] + } + }, + "aggs": { + "categories": { + "terms": { "field": "category" } + } + } +} +``` +This returns the most popular categories of products from BrandX that are smartphones. + +To display how many BrandX products are available in different price ranges, use a `post_filter`: + +``` +GET /electronics/_search +{ + "query": { + "bool": { + "filter": { + "term": { "brand": "BrandX" } + } + } + }, + "aggs": { + "price_ranges": { + "range": { + "field": "price", + "ranges": [ + { "to": 500 }, + { "from": 500, "to": 1000 }, + { "from": 1000 } + ] + } + }, + "category_smartphone": { + "filter": { + "term": { "category": "Smartphone" } + }, + "aggs": { + "price_ranges": { + "range": { + "field": "price", + "ranges": [ + { "to": 500 }, + { "from": 500, "to": 1000 }, + { "from": 1000 } + ] + } + } + } + } + }, + "post_filter": { + "term": { "category": "Smartphone" } + } +} + +``` +This query finds all products from BrandX. The `category_smartphone` aggregation limits the price range. The `price_ranges` aggregation returns price ranges for all BrandX products and the `post_filter` narrows the search hits to `smartphones`. + +### Rescoring filtered search results +Rescoring is a tool to improve the accuracy of the returned search results. Rescoring focuses on the top results rather than applying the complex algorithm to the entire dataset, optimizing efficiency. Each shard processes the rescore request before the final results are aggregated and sorted by the coordinating node. + +Example of using a rescore query: +``` +GET /electronics/_search +{ + "query": { + "bool": { + "filter": [ + { "term": { "brand": "BrandX" }}, + { "term": { "category": "Smartphone" }} + ] + } + }, + "post_filter": { + "term": { "category": "Smartphone" } + }, + "rescore": { + "window_size": 50, + "query": { + "rescore_query": { + "match": { + "features": "5G" + } + }, + "query_weight": 1.0, + "rescore_query_weight": 2.0 + } + } +} + +``` +In this example, the rescore section reorders the top 50 smartphones from BrandX based on whether their features include "5G". + +When using pagination, avoid changing window_size with each page step, as it may cause shifting results, which could confuse users. + +### Query rescorer + +In OpenSearch, the query rescorer refines search results by applying an additional query to the top results obtained from the initial search. Instead of evaluating every document, the rescorer focuses only on a subset defined by the window_size parameter, which defaults to 10. This approach enhances the efficiency of relevance adjustments. + +The rescore query’s influence is balanced with the original query through the `query_weight` and `rescore_query_weight` parameters, both set to 1 by default. + +#### Query rescorer example + +1. Create an index and add sample data: + +``` +PUT /articles +{ + "mappings": { + "properties": { + "title": { "type": "text" }, + "content": { "type": "text" }, + "views": { "type": "integer" } + } + } +} +``` + +2. Add sample documents: + +``` +POST /articles/_doc/1 +{ + "title": "OpenSearch Basics", + "content": "Learn the basics of OpenSearch with this guide.", + "views": 150 +} + +POST /articles/_doc/2 +{ + "title": "Advanced OpenSearch Techniques", + "content": "Explore advanced features and techniques in OpenSearch.", + "views": 300 +} + +POST /articles/_doc/3 +{ + "title": "OpenSearch Performance Tuning", + "content": "Optimize the performance of your OpenSearch cluster.", + "views": 450 +} + +``` + +3. Perform a search with query rescorer: + +This example query uses the query rescorer. It refines the results based on a phrase match for the content field. Documents that match "OpenSearch" in the content field are further rescored based on a phrase match, giving more weight to exact phrases. + +``` +POST /articles/_search +{ + "query": { + "match": { + "content": "OpenSearch" + } + }, + "rescore": { + "window_size": 10, + "query": { + "rescore_query": { + "match_phrase": { + "content": { + "query": "OpenSearch", + "slop": 2 + } + } + }, + "query_weight": 1, + "rescore_query_weight": 2 + } + } +} +``` +4. Perform a search with multiple rescorers: + +In this example, we first apply a phrase match rescorer and then a function score rescorer to adjust the final relevance based on the number of views. +``` +POST /articles/_search +{ + "query": { + "match": { + "content": "OpenSearch" + } + }, + "rescore": [ + { + "window_size": 10, + "query": { + "rescore_query": { + "match_phrase": { + "content": { + "query": "OpenSearch", + "slop": 2 + } + } + }, + "query_weight": 0.7, + "rescore_query_weight": 1.5 + } + }, + { + "window_size": 5, + "query": { + "score_mode": "multiply", + "rescore_query": { + "function_score": { + "field_value_factor": { + "field": "views", + "factor": 1.2, + "missing": 1 + } + } + } + } + } + ] +} + +``` From 3db58b64394c4699fd1434dff01b27e1aadecfe8 Mon Sep 17 00:00:00 2001 From: Heather Halter Date: Fri, 2 Aug 2024 15:40:21 -0700 Subject: [PATCH 02/20] Update _search-plugins/filter-search.md Signed-off-by: Heather Halter --- _search-plugins/filter-search.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_search-plugins/filter-search.md b/_search-plugins/filter-search.md index 7bbc891052..6fbdcf4a9d 100644 --- a/_search-plugins/filter-search.md +++ b/_search-plugins/filter-search.md @@ -6,7 +6,7 @@ nav_order: 36 # Filter search results -In OpenSearch, filtering search results can be achieved through two main approaches: using a [DSL boolean query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html) with a filter clause. The boolean query filtering approach applies filters to both search hits and aggregations. +In OpenSearch, filtering search results can be achieved using a DSL Boolean query with a filter clause. The boolean query filtering approach applies filters to both search hits and aggregations. You can also filter search results with the `post_filter` parameter in the search API, which applies filters only to search hits, not aggregations. From ab29e6a0ac216c1a29656d78fb1b4c9a071ee43b Mon Sep 17 00:00:00 2001 From: "leanne.laceybyrne@eliatra.com" Date: Tue, 6 Aug 2024 16:36:06 +0100 Subject: [PATCH 03/20] reviewdog updates Signed-off-by: leanne.laceybyrne@eliatra.com --- _search-plugins/filter-search.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_search-plugins/filter-search.md b/_search-plugins/filter-search.md index 6fbdcf4a9d..105ed6a690 100644 --- a/_search-plugins/filter-search.md +++ b/_search-plugins/filter-search.md @@ -6,7 +6,7 @@ nav_order: 36 # Filter search results -In OpenSearch, filtering search results can be achieved using a DSL Boolean query with a filter clause. The boolean query filtering approach applies filters to both search hits and aggregations. +In OpenSearch, filtering search results can be achieved using a DSL Boolean query with a filter clause. The Boolean query filtering approach applies filters to both search hits and aggregations. You can also filter search results with the `post_filter` parameter in the search API, which applies filters only to search hits, not aggregations. From c6251109414fa619f61974e2927bb79476267eca Mon Sep 17 00:00:00 2001 From: Melissa Vagi Date: Tue, 3 Sep 2024 12:23:50 -0600 Subject: [PATCH 04/20] Update filter-search.md Doc review completed Signed-off-by: Melissa Vagi --- _search-plugins/filter-search.md | 75 +++++++++++++++++++------------- 1 file changed, 44 insertions(+), 31 deletions(-) diff --git a/_search-plugins/filter-search.md b/_search-plugins/filter-search.md index 105ed6a690..52f43fd067 100644 --- a/_search-plugins/filter-search.md +++ b/_search-plugins/filter-search.md @@ -6,23 +6,17 @@ nav_order: 36 # Filter search results -In OpenSearch, filtering search results can be achieved using a DSL Boolean query with a filter clause. The Boolean query filtering approach applies filters to both search hits and aggregations. +Filter search results by using a DSL Boolean query with a filter clause. Boolean query filtering applies filters to both search hits and aggregations. Alternatively, filter search results using the `post_filter` parameter in the Search API. This applies filters only to search hits, not aggregations. -You can also filter search results with the `post_filter` parameter in the search API, which applies filters only to search hits, not aggregations. +## Using `post_filter` to filter search results -#### Table of contents -1. TOC - {:toc} +The `post_filter` parameter filters search results by calculating aggregations based on a broader result set before narrowing down the search hits. This also improves result relevance and reorders results by rescoring hits after applying the post filter. --- -## Using `post_filter` to filter search results - -Using the `post_filter` parameter to filter search results allows for calculating aggregations based on a broader result set before narrowing down the search hits. It can also improve relevance of results and reorder results by rescoring hits after applying the post filter. +#### Example: Filtering search results -### Example of filtering search results - -1. Create an index of products +1. Create an index: ``` PUT /electronics @@ -37,6 +31,7 @@ PUT /electronics } } ``` +{% include copy-curl.html %} 2. Index data: @@ -65,8 +60,9 @@ PUT /electronics/_doc/3?refresh "features": ["5G", "Triple Camera"] } ``` +{% include copy-curl.html %} -3. Perform a `boolean filter` to show only smartphones from BrandX +3. Use a Boolean filter query to display only smartphones from BrandX: ``` GET /electronics/_search @@ -81,8 +77,9 @@ GET /electronics/_search } } ``` +{% include copy-curl.html %} -Alternatively, to refine search results further, for example, you may have a category field that allows users to limit their search results to BrandX smartphones or tablets, you can utilize a `terms aggregation`: +Alternatively, refine search results using a terms aggregation. For example, use a category field to limit search results to BrandX smartphones or tablets: ``` GET /electronics/_search @@ -102,9 +99,11 @@ GET /electronics/_search } } ``` -This returns the most popular categories of products from BrandX that are smartphones. +{% include copy-curl.html %} + +This query returns the most popular categories of BrandX smartphones. -To display how many BrandX products are available in different price ranges, use a `post_filter`: +Then, use the `post_filter` parameter to show how many BrandX products are available in different price ranges: ``` GET /electronics/_search @@ -149,14 +148,23 @@ GET /electronics/_search "term": { "category": "Smartphone" } } } - ``` -This query finds all products from BrandX. The `category_smartphone` aggregation limits the price range. The `price_ranges` aggregation returns price ranges for all BrandX products and the `post_filter` narrows the search hits to `smartphones`. +{% include copy-curl.html %} + +This query finds all products from BrandX. The `category_smartphone` aggregation limits the price range. The `price_ranges` aggregation returns price ranges for all BrandX products. The `post_filter` narrows the search hits to `smartphones`. + +--- + +## Rescoring filtered search results -### Rescoring filtered search results Rescoring is a tool to improve the accuracy of the returned search results. Rescoring focuses on the top results rather than applying the complex algorithm to the entire dataset, optimizing efficiency. Each shard processes the rescore request before the final results are aggregated and sorted by the coordinating node. -Example of using a rescore query: +--- + +#### Example: Using a rescore query + +Use the following query with the `rescore` parameter to reorder the top 50 smartphones from BrandX that include 5G features: + ``` GET /electronics/_search { @@ -184,19 +192,20 @@ GET /electronics/_search } } } - ``` -In this example, the rescore section reorders the top 50 smartphones from BrandX based on whether their features include "5G". +{% include copy-curl.html %} -When using pagination, avoid changing window_size with each page step, as it may cause shifting results, which could confuse users. +Avoid changing `window_size` with each page step because it may cause shifting results and confuse users. ### Query rescorer -In OpenSearch, the query rescorer refines search results by applying an additional query to the top results obtained from the initial search. Instead of evaluating every document, the rescorer focuses only on a subset defined by the window_size parameter, which defaults to 10. This approach enhances the efficiency of relevance adjustments. +The query rescorer refines search results by applying an additional query to the top results obtained from the initial search. Instead of evaluating every document, the rescorer focuses on a subset defined by the `window_size` parameter, which defaults to `10`. This approach enhances the efficiency of relevance adjustments. + +The influence of the rescore query is balanced with the original query through the `query_weight` and `rescore_query_weight` parameters. Default for both is `1`. -The rescore query’s influence is balanced with the original query through the `query_weight` and `rescore_query_weight` parameters, both set to 1 by default. +--- -#### Query rescorer example +#### Example: Using the query rescorer 1. Create an index and add sample data: @@ -212,6 +221,7 @@ PUT /articles } } ``` +{% include copy-curl.html %} 2. Add sample documents: @@ -236,12 +246,12 @@ POST /articles/_doc/3 "content": "Optimize the performance of your OpenSearch cluster.", "views": 450 } - ``` +{% include copy-curl.html %} -3. Perform a search with query rescorer: +3. Perform a search using the query rescorer: -This example query uses the query rescorer. It refines the results based on a phrase match for the content field. Documents that match "OpenSearch" in the content field are further rescored based on a phrase match, giving more weight to exact phrases. +This example query uses the query rescorer to refines the results based on a phrase match for the content field. Documents that match "OpenSearch" in the content field are further rescored based on a phrase match, giving more weight to exact phrases. ``` POST /articles/_search @@ -268,9 +278,12 @@ POST /articles/_search } } ``` -4. Perform a search with multiple rescorers: +{% include copy-curl.html %} + +4. Perform a search using multiple rescorers: + +This example query first applies a phrase match rescorer and then a function score rescorer to adjust the final relevance based on the number of views. -In this example, we first apply a phrase match rescorer and then a function score rescorer to adjust the final relevance based on the number of views. ``` POST /articles/_search { @@ -312,5 +325,5 @@ POST /articles/_search } ] } - ``` +{% include copy-curl.html %} From 1a23b50f831adb2e6019d31bbd3fc562057c2ade Mon Sep 17 00:00:00 2001 From: "leanne.laceybyrne@eliatra.com" Date: Tue, 15 Oct 2024 17:42:14 +0100 Subject: [PATCH 05/20] Incorporating review comments. Signed-off-by: leanne.laceybyrne@eliatra.com --- _search-plugins/filter-search.md | 244 ++++--------------------------- 1 file changed, 30 insertions(+), 214 deletions(-) diff --git a/_search-plugins/filter-search.md b/_search-plugins/filter-search.md index 52f43fd067..b1d976862b 100644 --- a/_search-plugins/filter-search.md +++ b/_search-plugins/filter-search.md @@ -6,17 +6,12 @@ nav_order: 36 # Filter search results -Filter search results by using a DSL Boolean query with a filter clause. Boolean query filtering applies filters to both search hits and aggregations. Alternatively, filter search results using the `post_filter` parameter in the Search API. This applies filters only to search hits, not aggregations. +You can filter search using different methods, each suited to specific scenarios. We can apply filters at the query level, using `boolean` query clauses, as well as using `post_filter` and `aggregation` level filters. -## Using `post_filter` to filter search results +## 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 `post_filter` parameter filters search results by calculating aggregations based on a broader result set before narrowing down the search hits. This also improves result relevance and reorders results by rescoring hits after applying the post filter. - ---- - -#### Example: Filtering search results - -1. Create an index: +Create an index `electronics` and provide the mapping: ``` PUT /electronics @@ -31,38 +26,34 @@ PUT /electronics } } ``` -{% include copy-curl.html %} -2. Index data: +Add documents to the `electronics` index: ``` PUT /electronics/_doc/1?refresh { - "brand": "BrandX", + "brand": "BrandA", "category": "Smartphone", "price": 699.99, "features": ["5G", "Dual Camera"] } - PUT /electronics/_doc/2?refresh { - "brand": "BrandX", + "brand": "BrandA", "category": "Laptop", "price": 1199.99, "features": ["Touchscreen", "16GB RAM"] } - PUT /electronics/_doc/3?refresh { - "brand": "BrandY", + "brand": "BrandB", "category": "Smartphone", "price": 799.99, "features": ["5G", "Triple Camera"] } ``` -{% include copy-curl.html %} -3. Use a Boolean filter query to display only smartphones from BrandX: +Apply a `boolean` filter query to display only `smartphones` from `BrandA`: ``` GET /electronics/_search @@ -70,49 +61,49 @@ GET /electronics/_search "query": { "bool": { "filter": [ - { "term": { "brand": "BrandX" }}, + { "term": { "brand": "BrandA" }}, { "term": { "category": "Smartphone" }} ] } } } ``` -{% include copy-curl.html %} -Alternatively, refine search results using a terms aggregation. For example, use a category field to limit search results to BrandX smartphones or tablets: +## 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": { "brand": "BrandX" }}, - { "term": { "category": "Smartphone" }} - ] + "filter": { "term": { "category": "Smartphone" }} } }, "aggs": { - "categories": { - "terms": { "field": "category" } + "brands": { + "terms": { "field": "brand" } } + }, + "post_filter": { + "term": { "brand": "BrandA" } } } ``` -{% include copy-curl.html %} +This shows `BrandA` smartphones in the search hits while still displaying all brands in the aggregations. -This query returns the most popular categories of BrandX smartphones. +## Aggregation-level filtering to refine aggregations +We can use aggregation-level filtering to apply filters to specific aggregations, without affecting the overall aggregation. -Then, use the `post_filter` parameter to show how many BrandX products are available in different price ranges: +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": { "brand": "BrandX" } - } + "filter": { "term": { "category": "Smartphone" }} } }, "aggs": { @@ -126,9 +117,9 @@ GET /electronics/_search ] } }, - "category_smartphone": { + "filtered_brands": { "filter": { - "term": { "category": "Smartphone" } + "terms": { "brand": ["BrandA", "BrandB"] } }, "aggs": { "price_ranges": { @@ -143,187 +134,12 @@ GET /electronics/_search } } } - }, - "post_filter": { - "term": { "category": "Smartphone" } - } -} -``` -{% include copy-curl.html %} - -This query finds all products from BrandX. The `category_smartphone` aggregation limits the price range. The `price_ranges` aggregation returns price ranges for all BrandX products. The `post_filter` narrows the search hits to `smartphones`. - ---- - -## Rescoring filtered search results - -Rescoring is a tool to improve the accuracy of the returned search results. Rescoring focuses on the top results rather than applying the complex algorithm to the entire dataset, optimizing efficiency. Each shard processes the rescore request before the final results are aggregated and sorted by the coordinating node. - ---- - -#### Example: Using a rescore query - -Use the following query with the `rescore` parameter to reorder the top 50 smartphones from BrandX that include 5G features: - -``` -GET /electronics/_search -{ - "query": { - "bool": { - "filter": [ - { "term": { "brand": "BrandX" }}, - { "term": { "category": "Smartphone" }} - ] - } - }, - "post_filter": { - "term": { "category": "Smartphone" } - }, - "rescore": { - "window_size": 50, - "query": { - "rescore_query": { - "match": { - "features": "5G" - } - }, - "query_weight": 1.0, - "rescore_query_weight": 2.0 - } } } ``` -{% include copy-curl.html %} - -Avoid changing `window_size` with each page step because it may cause shifting results and confuse users. - -### Query rescorer -The query rescorer refines search results by applying an additional query to the top results obtained from the initial search. Instead of evaluating every document, the rescorer focuses on a subset defined by the `window_size` parameter, which defaults to `10`. This approach enhances the efficiency of relevance adjustments. +## 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. -The influence of the rescore query is balanced with the original query through the `query_weight` and `rescore_query_weight` parameters. Default for both is `1`. - ---- - -#### Example: Using the query rescorer - -1. Create an index and add sample data: - -``` -PUT /articles -{ - "mappings": { - "properties": { - "title": { "type": "text" }, - "content": { "type": "text" }, - "views": { "type": "integer" } - } - } -} -``` -{% include copy-curl.html %} - -2. Add sample documents: - -``` -POST /articles/_doc/1 -{ - "title": "OpenSearch Basics", - "content": "Learn the basics of OpenSearch with this guide.", - "views": 150 -} - -POST /articles/_doc/2 -{ - "title": "Advanced OpenSearch Techniques", - "content": "Explore advanced features and techniques in OpenSearch.", - "views": 300 -} - -POST /articles/_doc/3 -{ - "title": "OpenSearch Performance Tuning", - "content": "Optimize the performance of your OpenSearch cluster.", - "views": 450 -} -``` -{% include copy-curl.html %} - -3. Perform a search using the query rescorer: - -This example query uses the query rescorer to refines the results based on a phrase match for the content field. Documents that match "OpenSearch" in the content field are further rescored based on a phrase match, giving more weight to exact phrases. - -``` -POST /articles/_search -{ - "query": { - "match": { - "content": "OpenSearch" - } - }, - "rescore": { - "window_size": 10, - "query": { - "rescore_query": { - "match_phrase": { - "content": { - "query": "OpenSearch", - "slop": 2 - } - } - }, - "query_weight": 1, - "rescore_query_weight": 2 - } - } -} -``` -{% include copy-curl.html %} - -4. Perform a search using multiple rescorers: - -This example query first applies a phrase match rescorer and then a function score rescorer to adjust the final relevance based on the number of views. - -``` -POST /articles/_search -{ - "query": { - "match": { - "content": "OpenSearch" - } - }, - "rescore": [ - { - "window_size": 10, - "query": { - "rescore_query": { - "match_phrase": { - "content": { - "query": "OpenSearch", - "slop": 2 - } - } - }, - "query_weight": 0.7, - "rescore_query_weight": 1.5 - } - }, - { - "window_size": 5, - "query": { - "score_mode": "multiply", - "rescore_query": { - "function_score": { - "field_value_factor": { - "field": "views", - "factor": 1.2, - "missing": 1 - } - } - } - } - } - ] -} -``` -{% include copy-curl.html %} From 8aa29b6e92dfd07af0ef5564005519003be6a4eb Mon Sep 17 00:00:00 2001 From: Melissa Vagi Date: Tue, 15 Oct 2024 12:58:15 -0600 Subject: [PATCH 06/20] Update _search-plugins/filter-search.md Signed-off-by: Melissa Vagi --- _search-plugins/filter-search.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_search-plugins/filter-search.md b/_search-plugins/filter-search.md index b1d976862b..98a4dbb25b 100644 --- a/_search-plugins/filter-search.md +++ b/_search-plugins/filter-search.md @@ -6,7 +6,7 @@ nav_order: 36 # Filter search results -You can filter search using different methods, each suited to specific scenarios. We can apply filters at the query level, using `boolean` query clauses, as well as using `post_filter` and `aggregation` level filters. +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 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. From 28aa7cdd81092e970df53ef5631949cd1323eff3 Mon Sep 17 00:00:00 2001 From: Melissa Vagi Date: Tue, 15 Oct 2024 12:58:33 -0600 Subject: [PATCH 07/20] Update _search-plugins/filter-search.md Signed-off-by: Melissa Vagi --- _search-plugins/filter-search.md | 1 + 1 file changed, 1 insertion(+) diff --git a/_search-plugins/filter-search.md b/_search-plugins/filter-search.md index 98a4dbb25b..79180a3c18 100644 --- a/_search-plugins/filter-search.md +++ b/_search-plugins/filter-search.md @@ -9,6 +9,7 @@ nav_order: 36 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 + 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: From 10f018139560e42620114f1c75c7923ecb619886 Mon Sep 17 00:00:00 2001 From: Melissa Vagi Date: Tue, 15 Oct 2024 13:49:44 -0600 Subject: [PATCH 08/20] Update filter-search.md Signed-off-by: Melissa Vagi --- _search-plugins/filter-search.md | 39 +++++++++++++++++--------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/_search-plugins/filter-search.md b/_search-plugins/filter-search.md index 79180a3c18..936d59632e 100644 --- a/_search-plugins/filter-search.md +++ b/_search-plugins/filter-search.md @@ -6,13 +6,17 @@ 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. +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 with boolean queries +- **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. -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. +## Query-level filtering with Boolean queries -Create an index `electronics` and provide the mapping: +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: ``` PUT /electronics @@ -27,8 +31,9 @@ PUT /electronics } } ``` +{% include copy-curl.html %} -Add documents to the `electronics` index: +2. Add documents to the `electronics` index using the following request: ``` PUT /electronics/_doc/1?refresh @@ -53,8 +58,9 @@ PUT /electronics/_doc/3?refresh "features": ["5G", "Triple Camera"] } ``` +{% include copy-curl.html %} -Apply a `boolean` filter query to display only `smartphones` from `BrandA`: +3. Apply a `boolean` filter query to display only `smartphones` from `BrandA` using the following request: ``` GET /electronics/_search @@ -69,10 +75,11 @@ GET /electronics/_search } } ``` +{% include copy-curl.html %} -## Using `post-filter` to narrow results without affecting aggregations +## Narrowing results using `post-filter` while preserving aggregation visibility -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. +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 visibility of all brand options in the aggregations, as shown in the following example request: ``` GET /electronics/_search @@ -92,12 +99,13 @@ GET /electronics/_search } } ``` -This shows `BrandA` smartphones in the search hits while still displaying all brands in the aggregations. +{% include copy-curl.html %} + +Your result should show `BrandA` smartphones in the search hits and 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. +## Refining aggregrations with aggregation-level filtering -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. +You can use aggregation-level filtering to apply filters to specific aggregations, without affecting the overall aggregation. For example, 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. ``` GET /electronics/_search @@ -138,9 +146,4 @@ GET /electronics/_search } } ``` - -## 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. - +{% include copy-curl.html %} From bdf604244a6240b5cf242ddde49bfa007c037b8d Mon Sep 17 00:00:00 2001 From: Melissa Vagi Date: Tue, 15 Oct 2024 13:50:58 -0600 Subject: [PATCH 09/20] Update _search-plugins/filter-search.md Signed-off-by: Melissa Vagi --- _search-plugins/filter-search.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/_search-plugins/filter-search.md b/_search-plugins/filter-search.md index 936d59632e..19e4f0bbfd 100644 --- a/_search-plugins/filter-search.md +++ b/_search-plugins/filter-search.md @@ -105,7 +105,9 @@ Your result should show `BrandA` smartphones in the search hits and all brands i ## Refining aggregrations with aggregation-level filtering -You can use aggregation-level filtering to apply filters to specific aggregations, without affecting the overall aggregation. For example, 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. +You can use aggregation-level filtering to apply filters to specific aggregations, without affecting the overall aggregation. + +For example, 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. ``` GET /electronics/_search From 3017860999ee15c859912ac140b884cbfff024af Mon Sep 17 00:00:00 2001 From: Melissa Vagi Date: Wed, 16 Oct 2024 08:16:22 -0600 Subject: [PATCH 10/20] Update _search-plugins/filter-search.md Co-authored-by: Nathan Bower Signed-off-by: Melissa Vagi --- _search-plugins/filter-search.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_search-plugins/filter-search.md b/_search-plugins/filter-search.md index 19e4f0bbfd..76b210deb0 100644 --- a/_search-plugins/filter-search.md +++ b/_search-plugins/filter-search.md @@ -18,7 +18,7 @@ Use a `boolean` query with a filter clause to apply filters to both search hits 1. Create an index `electronics` and provide the mapping using the following request: -``` +```json PUT /electronics { "mappings": { From 7552312d144283dbe9f9cde8a41f0e344f3df76f Mon Sep 17 00:00:00 2001 From: Melissa Vagi Date: Wed, 16 Oct 2024 08:16:28 -0600 Subject: [PATCH 11/20] Update _search-plugins/filter-search.md Co-authored-by: Nathan Bower Signed-off-by: Melissa Vagi --- _search-plugins/filter-search.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_search-plugins/filter-search.md b/_search-plugins/filter-search.md index 76b210deb0..8589c28e2c 100644 --- a/_search-plugins/filter-search.md +++ b/_search-plugins/filter-search.md @@ -35,7 +35,7 @@ PUT /electronics 2. Add documents to the `electronics` index using the following request: -``` +```json PUT /electronics/_doc/1?refresh { "brand": "BrandA", From 24fddb0dbfdc1f08c9ec884fb63e392597431fee Mon Sep 17 00:00:00 2001 From: Melissa Vagi Date: Wed, 16 Oct 2024 08:16:38 -0600 Subject: [PATCH 12/20] Update _search-plugins/filter-search.md Co-authored-by: Nathan Bower Signed-off-by: Melissa Vagi --- _search-plugins/filter-search.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_search-plugins/filter-search.md b/_search-plugins/filter-search.md index 8589c28e2c..8cb53e3209 100644 --- a/_search-plugins/filter-search.md +++ b/_search-plugins/filter-search.md @@ -79,7 +79,7 @@ GET /electronics/_search ## 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 visibility of all brand options in the aggregations, as shown in the following example request: +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: ``` GET /electronics/_search From 1efb63b990307b67274df0e64dd283373003b117 Mon Sep 17 00:00:00 2001 From: Melissa Vagi Date: Wed, 16 Oct 2024 08:16:51 -0600 Subject: [PATCH 13/20] Update _search-plugins/filter-search.md Co-authored-by: Nathan Bower Signed-off-by: Melissa Vagi --- _search-plugins/filter-search.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_search-plugins/filter-search.md b/_search-plugins/filter-search.md index 8cb53e3209..4aafe02ce1 100644 --- a/_search-plugins/filter-search.md +++ b/_search-plugins/filter-search.md @@ -103,7 +103,7 @@ GET /electronics/_search Your result should show `BrandA` smartphones in the search hits and all brands in the aggregations. -## Refining aggregrations with aggregation-level filtering +## Refining aggregations with aggregation-level filtering You can use aggregation-level filtering to apply filters to specific aggregations, without affecting the overall aggregation. From c04ef8ff98b91bf77bb5ca4d7269f35033f00718 Mon Sep 17 00:00:00 2001 From: Melissa Vagi Date: Wed, 16 Oct 2024 08:17:14 -0600 Subject: [PATCH 14/20] Update _search-plugins/filter-search.md Co-authored-by: Nathan Bower Signed-off-by: Melissa Vagi --- _search-plugins/filter-search.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_search-plugins/filter-search.md b/_search-plugins/filter-search.md index 4aafe02ce1..9ffec3630b 100644 --- a/_search-plugins/filter-search.md +++ b/_search-plugins/filter-search.md @@ -105,7 +105,7 @@ Your result should show `BrandA` smartphones in the search hits and all brands i ## Refining aggregations with aggregation-level filtering -You can use aggregation-level filtering to apply filters to specific aggregations, without affecting the overall aggregation. +You can use aggregation-level filtering to apply filters to specific aggregations without affecting the overall aggregation. For example, 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. From 7f1f9b78379119ebbbb4a5d3a65ea57115970812 Mon Sep 17 00:00:00 2001 From: Melissa Vagi Date: Wed, 16 Oct 2024 08:17:22 -0600 Subject: [PATCH 15/20] Update _search-plugins/filter-search.md Co-authored-by: Nathan Bower Signed-off-by: Melissa Vagi --- _search-plugins/filter-search.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_search-plugins/filter-search.md b/_search-plugins/filter-search.md index 9ffec3630b..da7da28d6a 100644 --- a/_search-plugins/filter-search.md +++ b/_search-plugins/filter-search.md @@ -107,7 +107,7 @@ Your result should show `BrandA` smartphones in the search hits and all brands i You can use aggregation-level filtering to apply filters to specific aggregations without affecting the overall aggregation. -For example, 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. +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. ``` GET /electronics/_search From d9f13473f36658aced15fa01cff6dd977f11cd3e Mon Sep 17 00:00:00 2001 From: Melissa Vagi Date: Wed, 16 Oct 2024 08:17:30 -0600 Subject: [PATCH 16/20] Update _search-plugins/filter-search.md Co-authored-by: Nathan Bower Signed-off-by: Melissa Vagi --- _search-plugins/filter-search.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_search-plugins/filter-search.md b/_search-plugins/filter-search.md index da7da28d6a..2c66a38314 100644 --- a/_search-plugins/filter-search.md +++ b/_search-plugins/filter-search.md @@ -109,7 +109,7 @@ You can use aggregation-level filtering to apply filters to specific aggregation 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": { From 865da34cf2894fbe87b51c56c6ec4df70387f3e2 Mon Sep 17 00:00:00 2001 From: Melissa Vagi Date: Wed, 16 Oct 2024 08:17:49 -0600 Subject: [PATCH 17/20] Update _search-plugins/filter-search.md Co-authored-by: Nathan Bower Signed-off-by: Melissa Vagi --- _search-plugins/filter-search.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_search-plugins/filter-search.md b/_search-plugins/filter-search.md index 2c66a38314..734fc8d1df 100644 --- a/_search-plugins/filter-search.md +++ b/_search-plugins/filter-search.md @@ -101,7 +101,7 @@ GET /electronics/_search ``` {% include copy-curl.html %} -Your result should show `BrandA` smartphones in the search hits and all brands in the aggregations. +The result should show `BrandA` smartphones in the search hits and all brands in the aggregations. ## Refining aggregations with aggregation-level filtering From e37867adfab9e45bcf9935da45a5f453631c0436 Mon Sep 17 00:00:00 2001 From: Melissa Vagi Date: Wed, 16 Oct 2024 08:17:58 -0600 Subject: [PATCH 18/20] Update _search-plugins/filter-search.md Co-authored-by: Nathan Bower Signed-off-by: Melissa Vagi --- _search-plugins/filter-search.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_search-plugins/filter-search.md b/_search-plugins/filter-search.md index 734fc8d1df..d13da881dd 100644 --- a/_search-plugins/filter-search.md +++ b/_search-plugins/filter-search.md @@ -81,7 +81,7 @@ GET /electronics/_search 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": { From c444b4ae73713316e3a8c71dcfb75d7d5dbcabe5 Mon Sep 17 00:00:00 2001 From: Melissa Vagi Date: Wed, 16 Oct 2024 08:57:30 -0600 Subject: [PATCH 19/20] Update _search-plugins/filter-search.md Co-authored-by: Nathan Bower Signed-off-by: Melissa Vagi --- _search-plugins/filter-search.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_search-plugins/filter-search.md b/_search-plugins/filter-search.md index d13da881dd..e16832ac19 100644 --- a/_search-plugins/filter-search.md +++ b/_search-plugins/filter-search.md @@ -62,7 +62,7 @@ PUT /electronics/_doc/3?refresh 3. Apply a `boolean` filter query to display only `smartphones` from `BrandA` using the following request: -``` +```json GET /electronics/_search { "query": { From 77100a293d06fd3f9d3f6caa37a1fdfd0ed1d7de Mon Sep 17 00:00:00 2001 From: Melissa Vagi Date: Wed, 16 Oct 2024 09:07:50 -0600 Subject: [PATCH 20/20] Update _search-plugins/filter-search.md Signed-off-by: Melissa Vagi --- _search-plugins/filter-search.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_search-plugins/filter-search.md b/_search-plugins/filter-search.md index e16832ac19..f8625e0ac0 100644 --- a/_search-plugins/filter-search.md +++ b/_search-plugins/filter-search.md @@ -105,7 +105,7 @@ The result should show `BrandA` smartphones in the search hits and all brands in ## Refining aggregations with aggregation-level filtering -You can use aggregation-level filtering to apply filters to specific aggregations without affecting the overall aggregation. +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.