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 constant_score query to wrap everything with its filterclause. This is what enables non-scoring mode

 

These two term queries are children of the bool query, and since they are placed inside the should clause, at least one of them needs to match.

If a product has a price of 30, it is automatically excluded because it matches a must_notclause.

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:

"hits" : [
{
"_id" : "1",
"_score" : 1.0,
"_source" : {
"price" : 10,
"productID" : "XHDK-A-1293-#fJ3"
}
},
{
"_id" : "2",
"_score" : 1.0,
"_source" : {
"price" : 20,
"productID" : "KDKE-B-9947-#kL5"
}
}
]

Matches the term query for productID = "XHDK-A-1293-#fJ3"

Matches the term query for price = 20

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 term and the bool are sibling clauses inside the Boolean should, at least one of these queries must match for a document to be a hit.

 

These two term clauses are siblings in a must clause, so they both have to match for a document to be returned as a hit.

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 productID matches the term in the first bool.

 

These two fields match the term filters in the nested bool.

This was a simple example, but it demonstrates how Boolean queries can be used as building blocks to construct complex logical conditions.

combining-filters的更多相关文章

  1. Shodan全世界在线设备搜索引擎

    reproduction from https://danielmiessler.com/study/shodan/ What is Shodan? Shodan is a search engine ...

  2. ABP(现代ASP.NET样板开发框架)系列之13、ABP领域层——数据过滤器(Data filters)

    点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之13.ABP领域层——数据过滤器(Data filters) ABP是“ASP.NET Boilerplate P ...

  3. ASP.NET MVC Filters 4种默认过滤器的使用【附示例】

    过滤器(Filters)的出现使得我们可以在ASP.NET MVC程序里更好的控制浏览器请求过来的URL,不是每个请求都会响应内容,只响应特定内容给那些有特定权限的用户,过滤器理论上有以下功能: 判断 ...

  4. 关于Cewu Lu等的《Combining Sketch and Tone for Pencil Drawing Production》一文铅笔画算法的理解和笔录。

     相关论文的链接:Combining Sketch and Tone for Pencil Drawing Production 第一次看<Combining Sketch and Tone f ...

  5. correlation filters in object tracking

    http://www.cnblogs.com/hanhuili/p/4266990.html Correlation Filter in Visual Tracking系列一:Visual Objec ...

  6. MongoDB JAVA API Filters

    Filters 该过滤器类为所有的MongoDB的查询操作静态工厂方法.每个方法返回BSON类型,又可以传递给期望一个查询过滤器的任何方法的一个实例. eq:匹配等于指定值的值.gt:匹配大于指定值的 ...

  7. Android开发之旅: Intents和Intent Filters(理论部分)

    引言 大部分移动设备平台上的应用程序都运行在他们自己的沙盒中.他们彼此之间互相隔离,并且严格限制应用程序与硬件和原始组件之间的交互. 我们知道交流是多么的重要,作为一个孤岛没有交流的东西,一定毫无意义 ...

  8. PRML读书会第十四章 Combining Models(committees,Boosting,AdaBoost,决策树,条件混合模型)

    主讲人 网神 (新浪微博: @豆角茄子麻酱凉面) 网神(66707180) 18:57:18 大家好,今天我们讲一下第14章combining models,这一章是联合模型,通过将多个模型以某种形式 ...

  9. >>> FilterDispatcher <<< is deprecated! Please use the new filters!

    在struts2.3.20下,web.xml中使用 会出现*********************************************************************** ...

  10. Correlation Filter in Visual Tracking系列一:Visual Object Tracking using Adaptive Correlation Filters 论文笔记

    Visual Object Tracking using Adaptive Correlation Filters 一文发表于2010的CVPR上,是笔者所知的第一篇将correlation filt ...

随机推荐

  1. 03-SSH综合案例:商城表关系分析

    1.2   设计页面: 静态页面: 1.3    数据库分析与设计: 一般是一个Java类就对应一个表,有哪些类?那看你有哪些实体啊?一般一个模块对应一个实体 有用户模块就会有用户的一个实体,就会有用 ...

  2. linux sudo 系统环境变量 用户环境变量

    1. sudo就是普通用户临时拥有root的权限.好处在于,大多数时候使用用户自定义的配置,少数情况可以通过sudo实现root权限做事. 故而,需要注意的一点是,在你使用了sudo后,你临时不再是原 ...

  3. android应用程序monkey压力测试(模拟器或真机)

    首先需要安装一个模拟器: 前置条件: 1.jdk环境配置 2.eclipse下载安装(直接解压即可) 3.网站上下载ADT: 由于国内禁止google的浏览,所以需要自己上网找资源,下面这个网站有比较 ...

  4. ORA-01795: maximum number of expressions in a list is 1000

     今天发现查询Oracle用In查询In的元素不可以超过1000个还需要分成多个1000查询记录博客备忘!! Load Test的时候发现这么如下这个错误.... ORA-01795: maximum ...

  5. React Native 常用插件案例

    (二).基础入门: 1.React Native For Android环境配置以及第一个实例 2.React Native开发IDE安装及配置 3.React Native应用设备运行(Runnin ...

  6. [Jmeter]让报告在邮件中以链接进行显示,通过IIS

  7. 如何规范移动应用交互设计?UI/UX设计师须知的11个小技巧

    以下内容由Mockplus团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具. 十年前,手机的使用只是为了沟通. 而近几年,情况发生了很大变化,我们很难找到不使用手机的人.手机在极 ...

  8. 关于删除MySQL Logs的一点记录

    五一前,一个DBA同事反馈,在日常环境中删除一个大的slow log文件(假设文件大小10G以上吧),然后在MySQL中执行flush slow logs,会发现mysqld hang住. 今天尝试着 ...

  9. 阿里云服务器ECS按ctrl+alt+delete无法登录

    今天在使用阿里云服务器远程桌面的时候发现怎么也进入不了,远程桌面无法连接,于是想到了在阿里云服务器管理控制台可以使用连接管理终端进行远程桌面连接,下面详细介绍阿里云服务器操作经验. 操作步骤如下 登录 ...

  10. (转)【经验之谈】Git使用之Windows环境下配置

    原文地址:http://www.cnblogs.com/xishuai/p/3590434.html 前言 安装 配置 关于git使用的几个问题 后记 关于代码托管,以前用过vss和svn,看博客或论 ...