❤️ 博客主页:水滴技术
🚀 支持水滴:点赞👍 + 收藏⭐ + 留言💬
🌸 订阅专栏:大数据核心技术从入门到精通
文章目录
大家好,我是水滴~~
Elasticsearch 提供了一个完整的基于 JSON 的 DSL(Domain Specific Language,领域特定语言) 查询语言,它非常的丰富和灵活,并支持构建更加复杂和健壮的查询。
本篇我们主要讲述常用的三种 DSL 查询:全文搜索、精确匹配、布尔查询。
一、全文搜索
全文搜索是 Elasticsearch 的核心功能,在创建索引时要将字段映射设为
text
类型,并可指定分词器,我们方可使用该功能。关于数据类型和分词器可以参考之前的一些文章,这里不再赘述。
1.1 查询所有(match_all)
可以使用
match_all
来查询指定索引下的所有文档,下面例子查询“my_product”字段中所有文档:
GET/mt_product/_search
{"query":{"match_all":{}}}
响应结果:
{"took":0,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":12,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"mt_product","_type":"_doc","_id":"1","_score":1.0,"_source":{"id":1,"name":"招牌海苔单人餐","tags":["寿司"],"price":9.9,"sales":1000,"score":4.7,"store_id":1,"store_name":"M多寿司","create_time":"2023-01-18 08:01:00"}},...]}}
1.2 全文检索(match)
可以使用
match
进行全文搜索匹配(类似于 SQL 中的
like %%
),搜索的字段类型要是
text
类型才能生效。下例中搜索"my_product"索引,搜索字段使用 “name”:
GET/mt_product/_search
{"query":{"match":{"name":"好吃的寿司"}}}
响应:
{"took":0,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":2,"relation":"eq"},"max_score":1.7955686,"hits":[{"_index":"mt_product","_type":"_doc","_id":"3","_score":1.7955686,"_source":{"id":3,"name":"三文鱼寿司","tags":["寿司,鱼肉"],"price":16.9,"sales":820,"score":4.9,"store_id":2,"store_name":"爱食寿司","create_time":"2023-01-18 08:03:00"}},{"_index":"mt_product","_type":"_doc","_id":"4","_score":1.6760855,"_source":{"id":4,"name":"极上全品寿司套餐","tags":["寿司"],"price":25,"sales":1500,"score":4.6,"store_id":2,"store_name":"爱食寿司","create_time":"2023-01-18 08:04:00"}}]}}
1.3 多字段全文检索(multi_match)
有的时候同一个搜索请求,要应用到多个字段上,那么使用
multi_match
即可实现,如下例:
GET/mt_product/_search
{"query":{"multi_match":{"query":"好吃的寿司","fields":["name","store_name"]}}}
响应:
{"took":0,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":4,"relation":"eq"},"max_score":1.7955686,"hits":[{"_index":"mt_product","_type":"_doc","_id":"3","_score":1.7955686,"_source":{"id":3,"name":"三文鱼寿司","tags":["寿司,鱼肉"],"price":16.9,"sales":820,"score":4.9,"store_id":2,"store_name":"爱食寿司","create_time":"2023-01-18 08:03:00"}},{"_index":"mt_product","_type":"_doc","_id":"4","_score":1.6760855,"_source":{"id":4,"name":"极上全品寿司套餐","tags":["寿司"],"price":25,"sales":1500,"score":4.6,"store_id":2,"store_name":"爱食寿司","create_time":"2023-01-18 08:04:00"}},{"_index":"mt_product","_type":"_doc","_id":"1","_score":0.84174097,"_source":{"id":1,"name":"招牌海苔单人餐","tags":["寿司"],"price":9.9,"sales":1000,"score":4.7,"store_id":1,"store_name":"M多寿司","create_time":"2023-01-18 08:01:00"}},{"_index":"mt_product","_type":"_doc","_id":"2","_score":0.84174097,"_source":{"id":2,"name":"1-2人招牌双拼套餐","tags":["寿司"],"price":18.9,"sales":1200,"score":4.8,"store_id":1,"store_name":"M多寿司","create_time":"2023-01-18 08:02:00"}}]}}
二、精确匹配
精确匹配应用查找结构化的数据,一般使用
keyword
类型,应避免使用
text
类型。
2.1 精确查询(term)
根据精确值进行查询,类似于 SQL 中的
=
,示例:
GET/mt_product/_search
{"query":{"term":{"store_id":{"value":"1"}}}}
响应结果:
{"took":0,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":2,"relation":"eq"},"max_score":1.6486585,"hits":[{"_index":"mt_product","_type":"_doc","_id":"1","_score":1.6486585,"_source":{"id":1,"name":"招牌海苔单人餐","tags":["寿司"],"price":9.9,"sales":1000,"score":4.7,"store_id":1,"store_name":"M多寿司","create_time":"2023-01-18 08:01:00"}},{"_index":"mt_product","_type":"_doc","_id":"2","_score":1.6486585,"_source":{"id":2,"name":"1-2人招牌双拼套餐","tags":["寿司"],"price":18.9,"sales":1200,"score":4.8,"store_id":1,"store_name":"M多寿司","create_time":"2023-01-18 08:02:00"}}]}}
2.2 精确查询(terms)
terms
可以匹配多个值,类似于 SQL 中的
in(...)
,示例:
GET/mt_product/_search
{"query":{"terms":{"store_id":[1,2]}}}
响应结果:
{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":4,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"mt_product","_type":"_doc","_id":"1","_score":1.0,"_source":{"id":1,"name":"招牌海苔单人餐","tags":["寿司"],"price":9.9,"sales":1000,"score":4.7,"store_id":1,"store_name":"M多寿司","create_time":"2023-01-18 08:01:00"}},{"_index":"mt_product","_type":"_doc","_id":"2","_score":1.0,"_source":{"id":2,"name":"1-2人招牌双拼套餐","tags":["寿司"],"price":18.9,"sales":1200,"score":4.8,"store_id":1,"store_name":"M多寿司","create_time":"2023-01-18 08:02:00"}},{"_index":"mt_product","_type":"_doc","_id":"3","_score":1.0,"_source":{"id":3,"name":"三文鱼寿司","tags":["寿司,鱼肉"],"price":16.9,"sales":820,"score":4.9,"store_id":2,"store_name":"爱食寿司","create_time":"2023-01-18 08:03:00"}},{"_index":"mt_product","_type":"_doc","_id":"4","_score":1.0,"_source":{"id":4,"name":"极上全品寿司套餐","tags":["寿司"],"price":25,"sales":1500,"score":4.6,"store_id":2,"store_name":"爱食寿司","create_time":"2023-01-18 08:04:00"}}]}}
2.3 主键查询(ids)
根据多个主键查询,类似于 SQL 中的
id in (...)
,示例:
GET/mt_product/_search
{"query":{"ids":{"values":[10,11]}}}
响应结果:
{"took":0,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":2,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"mt_product","_type":"_doc","_id":"10","_score":1.0,"_source":{"id":10,"name":"双层原味板烧鸡腿麦满分四件套","tags":["汉堡,鸡肉"],"price":29,"sales":3000,"score":4.8,"store_id":5,"store_name":"麦当劳","create_time":"2023-01-18 08:10:00"}},{"_index":"mt_product","_type":"_doc","_id":"11","_score":1.0,"_source":{"id":11,"name":"火腿扒麦满分组合","tags":["汉堡"],"price":8,"sales":100000,"score":4.9,"store_id":5,"store_name":"麦当劳","create_time":"2023-01-18 08:11:00"}}]}}
2.4 范围查询(range)
查找一个范围,类似于 SQL 中的
>
、
≥
、
<
、
≤
,示例:
GET/mt_product/_search
{"query":{"range":{"price":{"gte":10,"lte":20}}}}
响应结果:
{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":3,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"mt_product","_type":"_doc","_id":"2","_score":1.0,"_source":{"id":2,"name":"1-2人招牌双拼套餐","tags":["寿司"],"price":18.9,"sales":1200,"score":4.8,"store_id":1,"store_name":"M多寿司","create_time":"2023-01-18 08:02:00"}},{"_index":"mt_product","_type":"_doc","_id":"3","_score":1.0,"_source":{"id":3,"name":"三文鱼寿司","tags":["寿司,鱼肉"],"price":16.9,"sales":820,"score":4.9,"store_id":2,"store_name":"爱食寿司","create_time":"2023-01-18 08:03:00"}},{"_index":"mt_product","_type":"_doc","_id":"9","_score":1.0,"_source":{"id":9,"name":"霸道小酥鸡+薯霸王","tags":["汉堡,鸡肉"],"price":19,"sales":300,"score":4.2,"store_id":4,"store_name":"汉堡王","create_time":"2023-01-18 08:09:00"}}]}}
三、布尔查询(bool)
布尔查询就是一个或多个子句的组合,每一个子句都是一个子查询,根据组合的方式可分为下面几种类型:
must
:必须匹配每个子句,类似于 SQL 中的and
,参与评分。should
:可以匹配任意子句,类似于 SQL 中的or
,参与评分。must_not
:必须不匹配每个子类,类似于 SQL中的not in
,不参与评分。filter
:过滤上下文,它与must
的不同之处是不会影响匹配文档的分数。
3.1 必须匹配(must)
我们要查询 tag 为“寿司”,并且价格小于等于 15 块钱,就可以使用这样:
GET/mt_product/_search
{"query":{"bool":{"must":[{"match":{"tags":"寿司"}},{"range":{"price":{"lte":15}}}]}}}
响应结果:
{"took":0,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":2.228378,"hits":[{"_index":"mt_product","_type":"_doc","_id":"1","_score":2.228378,"_source":{"id":1,"name":"招牌海苔单人餐","tags":["寿司"],"price":9.9,"sales":1000,"score":4.7,"store_id":1,"store_name":"M多寿司","create_time":"2023-01-18 08:01:00"}}]}}
3.2 可以匹配(should)
查询 tag 为“鱼肉”,或者 store_name 为“麦当劳”,可以这样查询:
GET/mt_product/_search
{"query":{"bool":{"should":[{"match":{"tags":"鱼肉"}},{"match":{"store_name":"麦当劳"}}]}}}
响应结果:
{"took":0,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":4,"relation":"eq"},"max_score":1.9003463,"hits":[{"_index":"mt_product","_type":"_doc","_id":"3","_score":1.9003463,"_source":{"id":3,"name":"三文鱼寿司","tags":["寿司,鱼肉"],"price":16.9,"sales":820,"score":4.9,"store_id":2,"store_name":"爱食寿司","create_time":"2023-01-18 08:03:00"}},{"_index":"mt_product","_type":"_doc","_id":"10","_score":1.6119244,"_source":{"id":10,"name":"双层原味板烧鸡腿麦满分四件套","tags":["汉堡,鸡肉"],"price":29,"sales":3000,"score":4.8,"store_id":5,"store_name":"麦当劳","create_time":"2023-01-18 08:10:00"}},{"_index":"mt_product","_type":"_doc","_id":"11","_score":1.6119244,"_source":{"id":11,"name":"火腿扒麦满分组合","tags":["汉堡"],"price":8,"sales":100000,"score":4.9,"store_id":5,"store_name":"麦当劳","create_time":"2023-01-18 08:11:00"}},{"_index":"mt_product","_type":"_doc","_id":"12","_score":1.6119244,"_source":{"id":12,"name":"原味板烧鸡腿麦满组件","tags":["汉堡,鸡肉"],"price":9.9,"sales":140000,"score":4.9,"store_id":5,"store_name":"麦当劳","create_time":"2023-01-18 08:12:00"}}]}}
3.3 不匹配(must_not)
查询 store_name 不是“麦当劳”,并且 tags 不包含“寿司”,可以这样查询:
GET/mt_product/_search
{"query":{"bool":{"must_not":[{"match":{"store_name":"麦当劳"}},{"match":{"tags":"寿司"}}]}}}
响应结果:
{"took":0,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":5,"relation":"eq"},"max_score":0.0,"hits":[{"_index":"mt_product","_type":"_doc","_id":"5","_score":0.0,"_source":{"id":5,"name":"劲脆鸡腿汉堡","tags":["汉堡,鸡肉"],"price":21.5,"sales":200,"score":4.5,"store_id":3,"store_name":"肯德基","create_time":"2023-01-18 08:05:00"}},{"_index":"mt_product","_type":"_doc","_id":"6","_score":0.0,"_source":{"id":6,"name":"香辣鸡腿汉堡","tags":["汉堡,鸡肉"],"price":21.5,"sales":98,"score":4.4,"store_id":3,"store_name":"肯德基","create_time":"2023-01-18 08:06:00"}},{"_index":"mt_product","_type":"_doc","_id":"7","_score":0.0,"_source":{"id":7,"name":"20块香辣鸡翅","tags":["鸡肉"],"price":99,"sales":5,"score":4.8,"store_id":3,"store_name":"肯德基","create_time":"2023-01-18 08:07:00"}},{"_index":"mt_product","_type":"_doc","_id":"8","_score":0.0,"_source":{"id":8,"name":"3层芝士年堡套餐","tags":["汉堡"],"price":29,"sales":4000,"score":4.9,"store_id":4,"store_name":"汉堡王","create_time":"2023-01-18 08:08:00"}},{"_index":"mt_product","_type":"_doc","_id":"9","_score":0.0,"_source":{"id":9,"name":"霸道小酥鸡+薯霸王","tags":["汉堡,鸡肉"],"price":19,"sales":300,"score":4.2,"store_id":4,"store_name":"汉堡王","create_time":"2023-01-18 08:09:00"}}]}}
3.4 过滤器(filter)
filter
过滤的结果不会影响原查询的得分。比如我们在上一条查询的基础上,增加 store_name 为“汉堡王”,其查询结果的得分,与上一条查询的得分是一样的。
GET/mt_product/_search
{"query":{"bool":{"must_not":[{"match":{"store_name":"麦当劳"}},{"match":{"tags":"寿司"}}],"filter":[{"match":{"store_name":"汉堡王"}}]}}}
响应结果:
{"took":6,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":2,"relation":"eq"},"max_score":0.0,"hits":[{"_index":"mt_product","_type":"_doc","_id":"8","_score":0.0,"_source":{"id":8,"name":"3层芝士年堡套餐","tags":["汉堡"],"price":29,"sales":4000,"score":4.9,"store_id":4,"store_name":"汉堡王","create_time":"2023-01-18 08:08:00"}},{"_index":"mt_product","_type":"_doc","_id":"9","_score":0.0,"_source":{"id":9,"name":"霸道小酥鸡+薯霸王","tags":["汉堡,鸡肉"],"price":19,"sales":300,"score":4.2,"store_id":4,"store_name":"汉堡王","create_time":"2023-01-18 08:09:00"}}]}}
附录
附录一:mt_product 索引 demo 脚本
PUT mt_product
{"mappings":{"properties":{"id":{"type":"long"},"name":{"type":"text","analyzer":"ik_max_word"},"tags":{"type":"text","analyzer":"ik_max_word"},"price":{"type":"float"},"sales":{"type":"integer"},"score":{"type":"float"},"store_id":{"type":"keyword"},"store_name":{"type":"text","analyzer":"ik_max_word"},"create_time":{"type":"date"}}}}
附录二:mt_product 数据 demo 脚本
POST/mt_product/_doc/1{"id":1,"name":"招牌海苔单人餐","tags":["寿司"],"price":9.9,"sales":1000,"score":4.7,"store_id":1,"store_name":"M多寿司","create_time":"2023-01-18 08:01:00"}POST/mt_product/_doc/2{"id":2,"name":"1-2人招牌双拼套餐","tags":["寿司"],"price":18.9,"sales":1200,"score":4.8,"store_id":1,"store_name":"M多寿司","create_time":"2023-01-18 08:02:00"}POST/mt_product/_doc/3{"id":3,"name":"三文鱼寿司","tags":["寿司,鱼肉"],"price":16.9,"sales":820,"score":4.9,"store_id":2,"store_name":"爱食寿司","create_time":"2023-01-18 08:03:00"}POST/mt_product/_doc/4{"id":4,"name":"极上全品寿司套餐","tags":["寿司"],"price":25,"sales":1500,"score":4.6,"store_id":2,"store_name":"爱食寿司","create_time":"2023-01-18 08:04:00"}POST/mt_product/_doc/5{"id":5,"name":"劲脆鸡腿汉堡","tags":["汉堡,鸡肉"],"price":21.5,"sales":200,"score":4.5,"store_id":3,"store_name":"肯德基","create_time":"2023-01-18 08:05:00"}POST/mt_product/_doc/6{"id":6,"name":"香辣鸡腿汉堡","tags":["汉堡,鸡肉"],"price":21.5,"sales":98,"score":4.4,"store_id":3,"store_name":"肯德基","create_time":"2023-01-18 08:06:00"}POST/mt_product/_doc/7{"id":7,"name":"20块香辣鸡翅","tags":["鸡肉"],"price":99,"sales":5,"score":4.8,"store_id":3,"store_name":"肯德基","create_time":"2023-01-18 08:07:00"}POST/mt_product/_doc/8{"id":8,"name":"3层芝士年堡套餐","tags":["汉堡"],"price":29,"sales":4000,"score":4.9,"store_id":4,"store_name":"汉堡王","create_time":"2023-01-18 08:08:00"}POST/mt_product/_doc/9{"id":9,"name":"霸道小酥鸡+薯霸王","tags":["汉堡,鸡肉"],"price":19,"sales":300,"score":4.2,"store_id":4,"store_name":"汉堡王","create_time":"2023-01-18 08:09:00"}POST/mt_product/_doc/10{"id":10,"name":"双层原味板烧鸡腿麦满分四件套","tags":["汉堡,鸡肉"],"price":29,"sales":3000,"score":4.8,"store_id":5,"store_name":"麦当劳","create_time":"2023-01-18 08:10:00"}POST/mt_product/_doc/11{"id":11,"name":"火腿扒麦满分组合","tags":["汉堡"],"price":8,"sales":100000,"score":4.9,"store_id":5,"store_name":"麦当劳","create_time":"2023-01-18 08:11:00"}POST/mt_product/_doc/12{"id":12,"name":"原味板烧鸡腿麦满组件","tags":["汉堡,鸡肉"],"price":9.9,"sales":140000,"score":4.9,"store_id":5,"store_name":"麦当劳","create_time":"2023-01-18 08:12:00"}
系列文章
🔥 Elasticsearch 核心技术(一):Elasticsearch 安装、配置、运行(Windows 版)
🔥 Elasticsearch 核心技术(二):elasticsearch-head 插件安装和使用
🔥 Elasticsearch 核心技术(三):Kibana 安装、配置、运行(Windows 版)
🔥 Elasticsearch 核心技术(四):索引管理、映射管理、文档管理(REST API)
🔥 Elasticsearch 核心技术(五):常用数据类型详解
🔥 Elasticsearch 核心技术(六):内置的 8 种分词器详解 + 代码示例
🔥 Elasticsearch 核心技术(七):IK 中文分词器的安装、使用、自定义字典
热门专栏
👍 《Python入门核心技术》
👍 《IDEA 教程:从入门到精通》
👍 《Java 教程:从入门到精通》
👍 《MySQL 教程:从入门到精通》
👍 《大数据核心技术从入门到精通》
版权归原作者 水滴技术 所有, 如有侵权,请联系我们删除。