Elasticsearch filter过滤查询
发布日期:2021-07-28 08:29:42
浏览次数:4
分类:技术文章
本文共 4784 字,大约阅读时间需要 15 分钟。
注:本实例代码是在ES marvel插件中执行
# ---------------------------------------
# filter 查询 # 建立测试数据_1 POST /store/products/_bulk { "index": {"_id": 1}} { "price": 10, "productID": "SD1002136"} { "index": {"_id": 2}} { "price": 20, "productID": "SD2678421"} { "index": {"_id": 3}} { "price": 30, "productID": "SD8897573"} { "index": {"_id": 4}} { "price": 40, "productID": "SD4535233"} # 查看测试数据 GET /store/products/_mget { "ids" : ["1", "2", "3", "4"] } # 查看library的mapping信息 GET /store/_mapping # --------------------------------------- # 简单过滤查询 # 最简单filter查询 # SELECT document FROM products where price = 20 # filtered 查询价格是20的商品 GET /store/products/_search { "query": { "filtered": { "query": { "match_all": {} }, "filter": { "term": { "price": 200 } } } } } # 也可以指定多个值 GET /store/products/_search { "query": { "filtered": { "filter": { "terms": { "price": [10, 20] } } } } } # SELECT product FROM products WHERE productID = "SD4535233" # 由于默认分析器进行分析,会将大写转小写 GET /store/products/_search { "query": { "filtered": { "filter": { "term": { "productID": "sd4535233" } } } } } # 查看分析器解析的结果 GET /_analyze?text=SD4535233 GET /store/_mapping DELETE /store # 重新建立一个映射,让productID处于not_analyzed模式 PUT /store { "mappings": { "products": { "properties": { "productID" : { "type" : "string", "index": "not_analyzed" } } } } } # 重新创建mapping插入数据后,进行查看 GET /store/products/_search { "query": { "filtered": { "filter": { "term": { "productID": "SD4535233" } } } } } # ------------------------- # bool过滤查询,可以做组合过滤查询 # SELECT product FROM products WHERE (price = 20 OR productID = "SD1002136") AND (price != 30) # 查询价格等于20的或者productID为SD4535233的商品,排除价格30元的。 # 类似的,Elasticsearch也有 and,or,not这样的组合条件的查询 方式 # 格式如下: # { # "bool" : { # "must" : [], # "should" : [], # "must_not" : [] # } # } # # must: 条件必须满足,相当于 and # should: 条件可以满足也可以不满足,相当于 or # must_not: 条件不需要满足,相当于 not GET /store/products/_search { "query": { "filtered": { "filter": { "bool": { "should": [ { "term": { "price": "20" }}, { "term": { "productID": "SD1002136" }} ], "must_not": [ { "term": { "price": "30" }} ] } } } } } # 嵌套处查询 # SELECT document FROM products WHERE productID ="SD1002136" OR (productID = "SD4535233" AND price = 30) GET /store/products/_search { "query": { "filtered": { "filter": { "bool": { "should": [ { "term": { "productID": "SD1002136" }}, { "bool": { "must": [ { "term": { "productID": "SD4535233" }}, { "term": { "price": "30" } } ] } } ] } } } } } # 另外一种 and,or,not查询 # 没有bool,直接使用and,or,not # # 查询价格既是10元,productID又为SD1002136的结果 GET /store/products/_search { "query": { "filtered": { "query": { "match_all": {} }, "filter": { "and": [ { "term" : { "price": 10 } }, { "term" : { "productID" : "SD1002136" } } ] } } } } # or # 查询价格是10元或productID 是SD4535233的一些商品 GET /store/products/_search { "query": { "filtered": { "query": { "match_all": {}}, "filter": { "or": [ { "term": { "price": 10 } }, { "term": { "productID": "SD4535233" } } ] } } } } # not # 查询productID不是SD1002136的商品 GET /store/products/_search { "query": { "filtered": { "query": { "match_all": {}}, "filter": { "not": { "term": { "productID": "SD1002136" } } } } } } # range范围过滤 # SELECT document FROM products WHERE price BETWEEN 20 AND 40 # gt : > 大于 # lt : < 小于 # gte : >= 大于等于 # lte : <= 小于等于 GET /store/products/_search { "query": { "filtered": { "filter": { "range": { "price": { "gt": 20, "lt": 40 } } } } } } # ------------------------- # 过滤空和非空 # 建立测试数据_2 POST /test_index/test/_bulk { "index": { "_id": "1"}} { "tags" : ["search"]} { "index": { "_id": "2"}} { "tags" : ["search", "open_source"]} { "index": { "_id": "3"}} { "other_field" : ["some data"]} { "index": { "_id": "4"}} { "tags" : null} { "index": { "_id": "5"}} { "tags" : ["search", null]} # 处理null空值的方法 # SELECT tags FROM test WHERE tags IS NOT NULL # SELECT tags FROM test WHERE tags IS NULL GET /test_index/test/_search { "query": { "filtered": { "filter": { "exists": { "field": "tags" } } } } } GET /test_index/test/_search { "query": { "filtered": { "filter": { "missing": { "field": "tags" } } } } }cache缓存
Elasticsearch在执行带有filter查询时,会打开索引的每个segment文件(Lucene式底层文件),然后去判断里面的文档是否符合filter要求。 注意:旧的segment文件不会变,新来的数据会产生新的segment。匹配的结果会用一个大型的BigSet数组来存储,这个数组的值只有0和1
匹配:1 不匹配:0 BigSet值是存在内存里的,而不是硬盘里,所以速度快!开启方式:在filter查询语句后面加"_cache": true
注意:
Scriptfilters,Geo-filters,Data ranges这样的过滤方式开启cache无意义 exists,missing,range,term和terms查询是默认开启cache的
如果想要开启cache缓存,只需要在filter中添加"_cache": true属性就可以,例如:
# cache缓存
GET /store/products/_search { "query": { "filtered": { "filter": { "bool": { "should": [ { "term": { "price": "20" }}, { "term": { "productID": "SD1002136" }} ], "_cache": true, "must_not": [ { "term": { "price": "30" }} ] } } } } }————————————————
版权声明:本文为CSDN博主「lebron3v」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/lebron3v/article/details/84030836转载地址:https://blog.csdn.net/sinat_17775997/article/details/115873720 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
能坚持,总会有不一样的收获!
[***.219.124.196]2024年03月29日 18时37分02秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
java httpclient 工具_spring整合httpClient工具类
2019-04-21
java监控其他服务器运行状态_windows服务器监控多个tomcat运行状态
2019-04-21
java构造函数有什么用_java构造函数有什么用,怎么用
2019-04-21
mysql 匹配 隔开的_按空格分隔关键字并搜索MySQL数据库
2019-04-21
java factory用法_怎样使用Java实现Factory设计模式
2019-04-21
盾神与砝码称重java_[蓝桥杯][算法提高VIP]盾神与砝码称重
2019-04-21
java输出狗的各类信息_第九章Java输入输出操作
2019-04-21
java notify怎么用_java 如何使用notify()
2019-04-21
java metrics 怎么样,Java metrics
2019-04-21
普朗克公式matlab,用MATLAB实现普朗克函数积分的快捷计算.pdf
2019-04-21
swoolec+%3c?php,PHP+Swoole并发编程的魅力
2019-04-21
php 404配置,phpcms如何配置404
2019-04-21
matlab wash矩阵产生,洗衣机净衣效能与衣损程度的关系分析
2019-04-21
python多线程实现kmeans,3种方式实现python多线程并发处理
2019-04-21