elasticsearch的查询器(query)和过滤器(filter)的比较

<p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(61, 68, 80); font-family: "Microsoft Yahei", Helvetica, Arial, sans-serif;"><span style="font-size: 16px;">类似lucene, elasticsearch的查询有两部分组成:query and filter 。 </span></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(61, 68, 80); font-family: "Microsoft Yahei", Helvetica, Arial, sans-serif;"><span style="box-sizing: border-box; font-size: 14px;">参考:https://www.elastic.co/guide/en/elasticsearch/guide/current/_queries_and_filters.html</span></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(61, 68, 80); font-family: "Microsoft Yahei", Helvetica, Arial, sans-serif;">
<span style="box-sizing: border-box; color: rgb(0, 153, 0); text-decoration: none; font-size: 18px;"></span></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(61, 68, 80); font-family: "Microsoft Yahei", Helvetica, Arial, sans-serif;"><span style="box-sizing: border-box; font-size: 18px;">Query查询器 与 Filter 过滤器</span></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(61, 68, 80); font-family: "Microsoft Yahei", Helvetica, Arial, sans-serif;"><span style="box-sizing: border-box; font-size: 14px;">尽管我们之前已经涉及了查询DSL,然而实际上存在两种DSL:查询DSL(query DSL)和过滤DSL(filter DSL)。</span><br style="box-sizing: border-box;" /><span style="box-sizing: border-box; font-size: 14px;">过滤器(filter)通常用于过滤文档的范围,比如某个字段是否属于某个类型,或者是属于哪个时间区间</span><br style="box-sizing: border-box;" /><span style="box-sizing: border-box; font-size: 14px;">* 创建日期是否在2014-2015年间?</span><br style="box-sizing: border-box;" /><span style="box-sizing: border-box; font-size: 14px;">* status字段是否为success?</span></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(61, 68, 80); font-family: "Microsoft Yahei", Helvetica, Arial, sans-serif;"><span style="box-sizing: border-box; font-size: 14px;">* lat_lon字段是否在某个坐标的10公里范围内?</span></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(61, 68, 80); font-family: "Microsoft Yahei", Helvetica, Arial, sans-serif;"><span style="box-sizing: border-box; font-size: 16px;"><br style="box-sizing: border-box;" /></span></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(61, 68, 80); font-family: "Microsoft Yahei", Helvetica, Arial, sans-serif;"><span style="box-sizing: border-box; font-size: 16px;">查询器(query)的使用方法像极了filter,但query更倾向于更准确的查找。</span></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(61, 68, 80); font-family: "Microsoft Yahei", Helvetica, Arial, sans-serif;"></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(61, 68, 80); font-family: "Microsoft Yahei", Helvetica, Arial, sans-serif;"><span style="box-sizing: border-box; font-size: 16px;">* 与full text search的匹配度最高</span></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(61, 68, 80); font-family: "Microsoft Yahei", Helvetica, Arial, sans-serif;"><span style="box-sizing: border-box; font-size: 16px;">* 正则匹配</span></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(61, 68, 80); font-family: "Microsoft Yahei", Helvetica, Arial, sans-serif;"><span style="box-sizing: border-box; font-size: 16px;">* 包含run单词,如果包含这些单词:runs、running、jog、sprint,也被视为包含run单词</span><br style="box-sizing: border-box;" /><span style="box-sizing: border-box; font-size: 16px;">* 包含quick、brown、fox。这些词越接近,这份文档的相关性就越高</span><br style="box-sizing: border-box;" /><span style="box-sizing: border-box; font-size: 16px;">查询器会计算出每份文档对于某次查询有多相关(relevant),然后分配文档一个相关性分数:_score。而这个分数会被用来对匹配了的文档进行相关性排序。相关性概念十分适合全文搜索(full-text search),这个很难能给出完整、“正确”答案的领域。</span></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(61, 68, 80); font-family: "Microsoft Yahei", Helvetica, Arial, sans-serif;"></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(61, 68, 80); font-family: "Microsoft Yahei", Helvetica, Arial, sans-serif;"><span style="box-sizing: border-box; font-size: 16px;">query filter在性能上对比:filter是不计算相关性的,同时可以cache。因此,filter速度要快于query。</span></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(61, 68, 80); font-family: "Microsoft Yahei", Helvetica, Arial, sans-serif;"></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(61, 68, 80); font-family: "Microsoft Yahei", Helvetica, Arial, sans-serif;"><span style="box-sizing: border-box; font-size: 16px;">下面是使用query语句查询的结果,第一次查询用了300ms,第二次用了280ms.</span></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(61, 68, 80); font-family: "Microsoft Yahei", Helvetica, Arial, sans-serif;"></p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px; color: rgb(61, 68, 80); font-family: "Microsoft Yahei", Helvetica, Arial, sans-serif;"></p><div id="crayon-57b29b90eda00375056990" class="crayon-syntax crayon-theme-solarized-light crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover expand" style="box-sizing: border-box; text-size-adjust: none; width: 712px; padding: 0px; margin: 12px 0px; font-family: Monaco, MonacoRegular, "Courier New", monospace; color: rgb(61, 68, 80); height: auto; top: auto; left: auto; overflow: hidden !important; position: relative !important; direction: ltr !important; text-shadow: none !important; background: rgb(253, 246, 227) !important; border-style: solid !important; border-width: 1px !important; border-color: rgb(153, 153, 153) !important; line-height: 15px !important;"><div class="crayon-toolbar" data-settings=" show" style="box-sizing: border-box; border-top: 0px; border-right: 0px; border-left: 0px; border-image: initial; padding: 0px; margin: 0px; width: 710px; position: relative; overflow: hidden; z-index: 4; background: rgb(221, 221, 221) !important; border-bottom: 1px solid rgb(187, 187, 187) !important; height: 18px !important; line-height: 18px !important;"><span class="crayon-title" style="box-sizing: border-box; font-family: inherit; float: left; height: inherit; font-size: inherit !important; line-height: inherit !important; font-weight: inherit !important; color: rgb(51, 51, 51) !important; padding: 0px 4px !important;"></span><div class="crayon-tools" style="box-sizing: border-box; font-family: inherit; background: 0px center; border: 0px; padding: 0px; margin: 0px; position: absolute; right: 0px; height: 18px !important; line-height: 18px !important; font-weight: inherit !important; float: right !important;"><div class="crayon-button crayon-nums-button crayon-pressed" title="切换是否显示行编号" style="box-sizing: border-box; font-family: inherit; background-image: initial; background-position: -24px 0px; background-size: initial; background-repeat: no-repeat; background-attachment: initial; background-origin: initial; background-clip: initial; border: 0px; padding: 0px; margin: 0px; height: inherit; line-height: 15px; display: inline; position: relative; width: 24px; color: rgb(255, 255, 255); background-color: rgb(188, 188, 188) !important; font-size: inherit !important; font-weight: inherit !important; float: left !important;"><div class="crayon-button-icon" style="box-sizing: border-box; font-family: inherit; background: url("../images/toolbar/buttons.png") -24px -32px; border: 0px; padding: 0px; margin: -8px 0px 0px; width: 24px; position: absolute; left: 0px; top: 9px; height: 16px !important; font-size: inherit !important; line-height: inherit !important; font-weight: inherit !important;"></div></div><div class="crayon-button crayon-plain-button crayon-pressed" title="纯文本显示代码" style="box-sizing: border-box; font-family: inherit; background-image: initial; background-position: -24px 0px; background-size: initial; background-repeat: no-repeat; background-attachment: initial; background-origin: initial; background-clip: initial; border: 0px; padding: 0px; margin: 0px; height: inherit; line-height: 15px; display: inline; position: relative; width: 24px; color: rgb(255, 255, 255); background-color: rgb(188, 188, 188) !important; font-size: inherit !important; font-weight: inherit !important; float: left !important;"><div class="crayon-button-icon" style="box-sizing: border-box; font-family: inherit; background: url("../images/toolbar/buttons.png") -24px -48px; border: 0px; padding: 0px; margin: -8px 0px 0px; width: 24px; position: absolute; left: 0px; top: 9px; height: 16px !important; font-size: inherit !important; line-height: inherit !important; font-weight: inherit !important;"></div></div><div class="crayon-button crayon-wrap-button" title="切换自动换行" style="box-sizing: border-box; font-family: inherit; background-image: initial; background-position: 0px center; background-size: initial; background-repeat: no-repeat; background-attachment: initial; background-origin: initial; background-clip: initial; border: 0px; padding: 0px; margin: 0px; height: inherit; line-height: 15px; display: inline; position: relative; width: 24px; background-color: transparent !important; font-size: inherit !important; font-weight: inherit !important; float: left !important;"><div class="crayon-button-icon" style="box-sizing: border-box; font-family: inherit; background: url("../images/toolbar/buttons.png") 0px -112px; border: 0px; padding: 0px; margin: -8px 0px 0px; width: 24px; position: absolute; left: 0px; top: 9px; height: 16px !important; font-size: inherit !important; line-height: inherit !important; font-weight: inherit !important;"></div></div><div class="crayon-button crayon-copy-button" title="复制代码" style="box-sizing: border-box; font-family: inherit; background-image: initial; background-position: 0px center; background-size: initial; background-repeat: no-repeat; background-attachment: initial; background-origin: initial; background-clip: initial; border: 0px; padding: 0px; margin: 0px; height: inherit; line-height: 15px; display: inline; position: relative; width: 24px; background-color: transparent !important; font-size: inherit !important; font-weight: inherit !important; float: left !important;"><div class="crayon-button-icon" style="box-sizing: border-box; font-family: inherit; background: url("../images/toolbar/buttons.png") 0px -16px; border: 0px; padding: 0px; margin: -8px 0px 0px; width: 24px; position: absolute; left: 0px; top: 9px; height: 16px !important; font-size: inherit !important; line-height: inherit !important; font-weight: inherit !important;"></div></div><div class="crayon-button crayon-popup-button" title="在新窗口中显示代码" style="box-sizing: border-box; font-family: inherit; background-image: initial; background-position: 0px center; background-size: initial; background-repeat: no-repeat; background-attachment: initial; background-origin: initial; background-clip: initial; border: 0px; padding: 0px; margin: 0px; height: inherit; line-height: 15px; display: inline; position: relative; width: 24px; background-color: transparent !important; font-size: inherit !important; font-weight: inherit !important; float: left !important;"><div class="crayon-button-icon" style="box-sizing: border-box; font-family: inherit; background: url("../images/toolbar/buttons.png") 0px 0px; border: 0px; padding: 0px; margin: -8px 0px 0px; width: 24px; position: absolute; left: 0px; top: 9px; height: 16px !important; font-size: inherit !important; line-height: inherit !important; font-weight: inherit !important;"></div></div><span class="crayon-language" style="box-sizing: border-box; font-family: inherit; float: left; height: inherit; font-size: inherit !important; line-height: inherit !important; font-weight: inherit !important; color: rgb(153, 153, 153) !important; padding: 0px 8px 0px 4px !important;">Python</span></div></div><div class="crayon-plain-wrap" style="box-sizing: border-box; background: 0px center; border: 0px; padding: 0px !important; margin: 0px !important; height: auto !important;">
  
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 
#blog:  http://xiaorui.cc
{
     "size" : 1 ,
     "query" : {
         "bool" : {
             "must" : [
                 {
                     "terms" : {
                         "keyword" : [
                             "手机" ,
                             "iphone"
                         ]
                     }
                 } ,
                 {
                     "range" : {
                         "cdate" : {
                             "gt" : "2015-11-09T11:00:00"
                         }
                     }
                 }
             ]
         }
     }
}
 
{
     "took" : 51 ,
     "timed_out" : false ,
     "_shards" : {
         "total" : 30 ,
         "successful" : 30 ,
         "failed" : 0
     } ,
     "hits" : {
         "total" : 6818 ,
         "max_score" : 0 ,
         "hits" : [ ]
     }
}

下面是使用filter查询出来的结果,第一次查询时间是280ms,第二次130ms…. 速度确实快了不少,也证明filter走了cache缓存。 但是如果我们对比下命中的数目,query要比filter要多一点,换句话说,更加的精准。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值