Elasticsearch filter过滤查询
发布日期:2021-07-28 08:29:41
浏览次数:2
分类:技术文章
本文共 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/115873158 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年03月23日 15时11分42秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
说说如何使用 Canvas 绘制弧线与曲线
2019-04-26
系统架构设计笔记(64)—— 嵌入式系统设计
2019-04-26
系统架构设计笔记(65)—— 项目的范围、时间与成本
2019-04-26
系统架构设计笔记(66)—— 配置管理与文档管理
2019-04-26
说说 Python 元组的高级用法
2019-04-26
系统架构设计笔记(66)—— 配置管理与文档管理
2019-04-26
系统架构设计笔记(67)—— 软件需求管理
2019-04-26
系统架构设计笔记(68)—— 软件开发的质量与风险
2019-04-26
系统架构设计笔记(69)—— 人力资源管理
2019-04-26
系统架构设计笔记(70)—— 软件运行评价与过程改进
2019-04-26
系统架构设计笔记(71)—— 信息系统概述
2019-04-26
说说 Canvas 的旋转功能
2019-04-26
说说 Canvas 的缩放功能
2019-04-26
系统架构设计笔记(72)—— 信息系统工程
2019-04-26
系统架构设计笔记(73)—— 政府信息化与电子政务
2019-04-26
系统架构设计笔记(74)—— 企业信息化与电子商务
2019-04-26
系统架构设计笔记(75)—— 知识管理与商业智能
2019-04-26
说说在 log4j 中如何把日志记录到不同的文件中
2019-04-26