**同步结果摘要**: | 目录 | 同步前 | 同步后 | 官方 | |------|--------|--------|------| | aggregations | 40 | 65 | 64 | | analyzers | 8 | 141 | 140 | | api-reference | 97 | 186 | 174 | | benchmark | 34 | 67 | 53 | | clients | 17 | 18 | 18 | | dashboards | 38 | 149 | 131 | | data-prepper | 77 | 134 | 123 | | im-plugin | 22 | 37 | 37 | | ingest-pipelines | 27 | 52 | 51 | | install-and-configure | 37 | 57 | 53 | | ml-commons-plugin | 81 | 157 | 155 | | observing-your-data | 39 | 73 | 66 | | query-dsl | 40 | 77 | 75 | | search-plugins | 80 | 139 | 95 | | security-analytics | 22 | 47 | 46 | **说明**: - 本地文件数 > 官方:本地有一些官方已删除或重命名的文件 - 新增了约 **740 个英文文档文件** - 已有文件保持不变(未覆盖)
4.7 KiB
layout, title, parent, nav_order
| layout | title | parent | nav_order |
|---|---|---|---|
| default | Filter results | Customizing search results | 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
booleanquery filter clauses to filter search hits and aggregations, such as to narrow results to specific categories or brands. - Post-filter filtering: Use
post_filterto 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.
- Create an index
electronicsand provide the mapping using the following request:
PUT /electronics
{
"mappings": {
"properties": {
"brand": { "type": "keyword" },
"category": { "type": "keyword" },
"price": { "type": "float" },
"features": { "type": "keyword" }
}
}
}
{% include copy-curl.html %}
- Add documents to the
electronicsindex using the following request:
POST /_bulk?refresh
{ "index": { "_index": "electronics", "_id": "1" } }
{ "brand": "BrandA", "category": "Smartphone", "price": 699.99, "features": ["5G", "Dual Camera"] }
{ "index": { "_index": "electronics", "_id": "2" } }
{ "brand": "BrandA", "category": "Laptop", "price": 1199.99, "features": ["Touchscreen", "16GB RAM"] }
{ "index": { "_index": "electronics", "_id": "3" } }
{ "brand": "BrandB", "category": "Smartphone", "price": 799.99, "features": ["5G", "Triple Camera"] }
{% include copy-curl.html %}
- Apply a
booleanfilter query to display onlysmartphonesfromBrandAusing the following request:
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:
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.
To keep facet options visible while users apply filters, combine post_filter with filter aggregations. For more information, see Maintaining facet options during filtering.
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.
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 %}