概念
bool (布爾)過濾器。 這是個 複合過濾器(compound filter) ,它可以接受多個其他過濾器作爲參數,並將這些過濾器結合成各式各樣的布爾(邏輯)組合。
格式
一個 bool 過濾器由三部分組成:
{
"bool" : {
"must" : [],
"should" : [],
"must_not" : [],
}
}
must
所有的語句都 必須(must) 匹配,與 AND 等價。
must_not
所有的語句都 不能(must not) 匹配,與 NOT 等價。
should
至少有一個語句要匹配,與 OR 等價。
**
嵌套布爾過濾器
**
對於以下這個 SQL 語句
SELECT document
FROM products
WHERE productID = "KDKE-B-9947-#kL5"
OR ( productID = "JODL-X-1937-#pV7"
AND price = 30 )
我們將其轉換成一組嵌套的 bool 過濾器:
GET /my_store/products/_search
{
"query" : {
"filtered" : {
"filter" : {
"bool" : {
"should" : [
{ "term" : {"productID" : "KDKE-B-9947-#kL5"}},
{ "bool" : {
"must" : [
{ "term" : {"productID" : "JODL-X-1937-#pV7"}},
{ "term" : {"price" : 30}}
]
}}
]
}
}
}
}
}
參考
Elasticsearch: 權威指南