combining-filters
The previous two examples showed a single filter in use. In practice, you will probably need to filter on multiple values or fields. For example, how would you express this SQL in Elasticsearch?
SELECT product
FROM products
WHERE (price = 20 OR productID = "XHDK-A-1293-#fJ3")
AND (price != 30)
In these situations, you will need to use a bool
query inside the constant_score
query. This allows us to build filters that can have multiple components in boolean combinations.
Bool Filteredit
Recall that the bool
query is composed of four sections:
{
"bool" : {
"must" : [],
"should" : [],
"must_not" : [],
"filter": []
}
}
must
- All of these clauses must match. The equivalent of
AND
. must_not
- All of these clauses must not match. The equivalent of
NOT
. should
- At least one of these clauses must match. The equivalent of
OR
. filter
- Clauses that must match, but are run in non-scoring, filtering mode.
In this secondary boolean query, we can ignore the filter
clause: the queries are already running in non-scoring mode, so the extra filter
clause is useless.
Each section of the bool
filter is optional (for example, you can have a must
clause and nothing else), and each section can contain a single query or an array of queries.
To replicate the preceding SQL example, we will take the two term
queries that we used previously and place them inside the should
clause of a bool
query, and add another clause to deal with the NOT
condition:
GET /my_store/products/_search
{
"query" : {
"constant_score" : {
"filter" : {
"bool" : {
"should" : [
{ "term" : {"price" : 20}},
{ "term" : {"productID" : "XHDK-A-1293-#fJ3"}}
],
"must_not" : {
"term" : {"price" : 30}
}
}
}
}
}
}
Note that we still need to use a |
|
These two |
|
If a product has a price of |
Notice how boolean is placed inside the constant_score
, but the individual term queries are just placed in the should
and must_not
. Because everything is wrapped with the constant_score
, the rest of the queries are executing in filtering mode.
Our search results return two hits, each document satisfying a different clause in the bool
query:
Nesting Boolean Queriesedit
You can already see how nesting boolean queries together can give rise to more sophisticated boolean logic. If you need to perform more complex operations, you can continue nesting boolean queries in any combination, giving rise to arbitrarily complex boolean logic.
For example, if we have this SQL statement:
SELECT document
FROM products
WHERE productID = "KDKE-B-9947-#kL5"
OR ( productID = "JODL-X-1937-#pV7"
AND price = 30 )
We can translate it into a pair of nested bool
filters:
GET /my_store/products/_search
{
"query" : {
"constant_score" : {
"filter" : {
"bool" : {
"should" : [
{ "term" : {"productID" : "KDKE-B-9947-#kL5"}},
{ "bool" : {
"must" : [
{ "term" : {"productID" : "JODL-X-1937-#pV7"}},
{ "term" : {"price" : 30}}
]
}}
]
}
}
}
}
}
Because the |
|
These two |
The results show us two documents, one matching each of the should
clauses:
"hits" : [
{
"_id" : "2",
"_score" : 1.0,
"_source" : {
"price" : 20,
"productID" : "KDKE-B-9947-#kL5"
}
},
{
"_id" : "3",
"_score" : 1.0,
"_source" : {
"price" : 30,
"productID" : "JODL-X-1937-#pV7"
}
}
]
This |
|
These two fields match the |
This was a simple example, but it demonstrates how Boolean queries can be used as building blocks to construct complex logical conditions.
相关文章
- Route Filters「建议收藏」
- maven的资源过滤filters[通俗易懂]
- Fiddler工具之Filters[通俗易懂]
- PS2021神经滤镜Neural Filters离线安装包 亲测可用
- vue filters 是什么?怎么使用?
- Antd的table筛选,表头columns的filters过滤清空
- 卷积核(kernels)与滤波器(filters)的关系「建议收藏」
- Vue3不支持Filters过滤器
- vue filters过滤器的统一封装「建议收藏」
- django_filters实现数据过滤
- Spam Filters「建议收藏」
- TMOS系统之Packet Filters
- Garbled Bloom Filters算法简述
- Bloom Filters简介
- Simulation of SAW & BAW Resonators for RF Filters
- PS2023 Neural Filters(PS2023神经滤镜插件) V24.0 最新离线安装包直装版
- 【笔记】vue filters过滤器传多个参数
- django_filters TypeError: __init__() got an unexpected keyword argument 'name'
- django.template.exceptions.TemplateDoesNotExist: django_filters/rest_framework/crispy_form.html
- ORA-39192: table mode jobs with transportable=always and partition filters only allow objects from one table ORACLE 报错 故障修复 远程处理
- ORA-39221: Specifying data filters is invalid when dumpfiles do not contain table data. ORACLE 报错 故障修复 远程处理
- MySQL Error number: 3898; Symbol: ER_XA_REPLICATION_FILTERS; SQLSTATE: HY000 报错 故障修复 远程处理
- MySQL Error number: 4003; Symbol: ER_DA_ERROR_LOG_MULTIPLE_FILTERS; SQLSTATE: HY000 报错 故障修复 远程处理
- MySQL Error number: MY-010110; Symbol: ER_RPL_BINLOG_FILTERS_OOM; SQLSTATE: HY000 报错 故障修复 远程处理
- MySQL Error number: MY-010963; Symbol: ER_RPL_SLAVE_GLOBAL_FILTERS_COPY_FAILED; SQLSTATE: HY000 报错 故障修复 远程处理
- MySQL Error number: MY-011032; Symbol: ER_RPL_FILTERS_NOT_ATTACHED_TO_CHANNEL; SQLSTATE: HY000 报错 故障修复 远程处理
- MySQL Error number: MY-013164; Symbol: ER_COULD_NOT_REINITIALIZE_AUDIT_LOG_FILTERS; SQLSTATE: HY000 报错 故障修复 远程处理
- MongoDB JAVA API Filters详解编程语言
- Oracle 视图 DBA_WORKLOAD_FILTERS 官方解释,作用,如何使用详细说明
- filters.revealTrans.Transition使用方法小结