ES 基本使用《二》--组合过滤
2017-10-19 17:02
225 查看
1.组合过滤器
在实际应用中,我们很有可能会过滤多个值或字段。比方说,怎样用
Elasticsearch 来表达下面的 SQL ? SELECT product
FROM products
WHERE (price = 20 OR productID = "XHDK-A-1293-#fJ3")
AND (price != 30)
这种情况下,我们需要
filter) ,它可以接受多个其他过滤器作为参数,并将这些过滤器结合成各式各样的布尔(逻辑)组合。
布尔过滤器组成:
{
"bool" : {
"must" : [],
"should" : [],
"must_not" : [],
}
}
就这么简单! 当我们需要多个过滤器时,只须将它们置入
注意:官方文档有点问题,在5.X后,filtered 被bool代替了,The
is replaced by the bool query。
https://www.elastic.co/guide/en/elasticsearch/reference/5.0/query-dsl-filtered-query.html
嵌套bool过滤器
对于以下SQL:
SELECT document
FROM products
WHERE productID = "KDKE-B-9947-#kL5"
OR ( productID = "JODL-X-1937-#pV7"
AND price = 30 )DSL 如下:
GET /my_store/products/_search
{
"query" : {
"bool": {
"should": [
{"term": { "productID": "KDKE-B-9947-#kL5"}},
{"bool": {"must": [
{"term": {"productID" : "JODL-X-1937-#pV7"}},
{"term":{"price":30}}
]}}
]
}
}
}
在实际应用中,我们很有可能会过滤多个值或字段。比方说,怎样用
Elasticsearch 来表达下面的 SQL ? SELECT product
FROM products
WHERE (price = 20 OR productID = "XHDK-A-1293-#fJ3")
AND (price != 30)
这种情况下,我们需要
bool(布尔)过滤器。 这是个 复合过滤器(compound
filter) ,它可以接受多个其他过滤器作为参数,并将这些过滤器结合成各式各样的布尔(逻辑)组合。
布尔过滤器组成:
{
"bool" : {
"must" : [],
"should" : [],
"must_not" : [],
}
}
must所有的语句都 必须(must) 匹配,与
AND等价。
must_not所有的语句都 不能(must not) 匹配,与
NOT等价。
should至少有一个语句要匹配,与
OR等价。
就这么简单! 当我们需要多个过滤器时,只须将它们置入
bool过滤器的不同部分即可。
注意:官方文档有点问题,在5.X后,filtered 被bool代替了,The
filteredquery
is replaced by the bool query。
https://www.elastic.co/guide/en/elasticsearch/reference/5.0/query-dsl-filtered-query.html
GET /my_store/products/_search { "query" : { "bool": { "should": [ {"term": {"price":20}}, {"term": { "productID": "XHDK-A-1293-#fJ3"}} ], "must_not": [ {"term": { "price": { "value": "30" } }} ] } } }
嵌套bool过滤器
对于以下SQL:
SELECT document
FROM products
WHERE productID = "KDKE-B-9947-#kL5"
OR ( productID = "JODL-X-1937-#pV7"
AND price = 30 )DSL 如下:
GET /my_store/products/_search
{
"query" : {
"bool": {
"should": [
{"term": { "productID": "KDKE-B-9947-#kL5"}},
{"bool": {"must": [
{"term": {"productID" : "JODL-X-1937-#pV7"}},
{"term":{"price":30}}
]}}
]
}
}
}
相关文章推荐
- Jquery基本选择器 层次选择器 过滤选择器 表单选择器使用示例 带注释
- php 3种基本设计模式的组合使用
- 使用query参数过滤组合AWS CLI输出信息
- jQuery基本过滤选择器使用介绍
- ES 基本使用《四》--索引mapping
- 使用jQuery基本过滤选择器
- struts2过滤器与拦截器组合使用,过滤拦截非法请求
- 使用Jquery基本过滤选择器
- ES 基本使用《一》--分析
- DataTables+BootStrap组合使用Ajax来获取数据并且动态加载dom的方法(排序,过滤,分页等)
- JQuery的基本选择器使用总结以及过滤,文本,可见度的选择代码
- es6的Object.keys()和map()组合使用的案例
- Jquery基本选择器 层次选择器 过滤选择器 表单选择器使用示例 带注释
- ViewPager与Fragment组合时的基本使用方法
- 使用jQuery基本过滤选择器
- es 基本语法 使用 案例
- 组合控件的基本使用
- HTML5利用Canvas绘制图形(Canvas基本知识、绘制矩形、使用路径和图形组合)
- ES 基本使用《三》--terms
- jQuery基本过滤选择器使用介绍