目录
目标
ES版本信息
官方文档
什么是布尔查询
实战
新增测试数据
must
filter
should
must_not
掌握布尔查询,分析子句的多种类型使用方法。
7.17.5
Boolean queryhttps://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-bool-query.html#query-dsl-bool-query
由一个或多个布尔子句构成的查询,且这些子句的得分会被合并计分。子句有以下四种类型:
类型 | 描述 | 备注 |
---|---|---|
must | 必须匹配,贡献算分。相当于&&。 | |
filter | 必须匹配,不贡献算分。相当于&&。 | |
should | 选择新匹配,贡献算分。相当于||。 | 通过minimum_should_match属性修改必须匹配的数量。 如果没有filter子句和must子句,则默认值为1;否则为0。 |
must_not | 必须不匹配,不贡献算分。相当于!。 |
PUT /boss_db
{
"settings": {
"index": {
"analysis.analyzer.default.type": "ik_max_word"
}
}
}
PUT /boss_db/_bulk
{"index":{"_id":"1"}}
{"company":"星耀科技有限公司","min_num":0,"max_num":20,"province":"广东省","city":"深圳市","county":"南山区","post":"前端开发实习生","min_salary":10,"max_salary":16,"qualification":"本科","min_work_time":3,"max_work_time":5,"skill":["html","css","vue","js"]}
{"index":{"_id":"2"}}
{"company":"恒和科技有限公司","min_num":100,"max_num":500,"province":"广东省","city":"广州市","county":"天河区","post":"JAVA开发工程师","min_salary":20,"max_salary":30,"qualification":"硕士","min_work_time":1,"max_work_time":3,"skill":["k8s","springboot","mybatis","微服务"]}
{"index":{"_id":"3"}}
{"company":"天心科技有限公司","min_num":2000,"max_num":5000,"province":"广东省","city":"广州市","county":"天河区","post":"JAVA架构师","min_salary":40,"max_salary":50,"qualification":"博士","min_work_time":3,"max_work_time":5,"skill":["mybatis","spring","kafka","微服务"]}
{"index":{"_id":"4"}}
{"company":"黄河科技有限公司","min_num":2000,"max_num":5000,"province":"广东省","city":"广州市","county":"天河区","post":"JAVA","min_salary":40,"max_salary":50,"qualification":"博士","min_work_time":3,"max_work_time":5,"skill":["es","mysql","分布式","soa"]}
{"index":{"_id":"5"}}
{"company":"长江科技有限公司","min_num":2000,"max_num":5000,"province":"广东省","city":"深圳市","county":"龙岗区","post":"资深大数据开发工程师","min_salary":40,"max_salary":50,"qualification":"博士","min_work_time":0,"max_work_time":5,"skill":["redis","kafka","mq","数据结构"]}
{"index":{"_id":"6"}}
{"company":"黄山科技有限公司","min_num":2000,"max_num":5000,"province":"广东省","city":"深圳市","county":"龙岗区","post":"前端开发","min_salary":20,"max_salary":30,"qualification":"大专","min_work_time":0,"max_work_time":5,"skill":["html","css","js","vue"]}
{"index":{"_id":"7"}}
{"company":"黄山科技有限公司","min_num":2000,"max_num":5000,"province":"广东省","city":"深圳市","county":"龙岗区","post":"前端开发实习生","min_salary":10,"max_salary":13,"qualification":"不限","min_work_time":0,"max_work_time":5}
{"index":{"_id":"8"}}
{"company":"银河大数据科技有限公司","min_num":2000,"max_num":5000,"province":"广东省","city":"深圳市","county":"龙岗区","post":"大数据实习生","min_salary":10,"max_salary":13,"qualification":"不限","min_work_time":0,"max_work_time":5,"skill":["电商","spring","容器技术","微服务技术"]}
{"index":{"_id":"9"}}
{"company":"银河大数据科技有限公司","min_num":2000,"max_num":5000,"province":"广东省","city":"深圳市","county":"龙岗区","post":"JAVA实习生","min_salary":30,"max_salary":60,"qualification":"本科","min_work_time":0,"max_work_time":5,"skill":["数据结构","k8s","云原生技术","电商"]}
需求:查询招聘信息,公司必须位于深圳市,且要求工作年限在3到5年之间。计算得分。
#查询招聘信息,公司必须位于深圳市,且要求工作年限在3到5年之间。
GET /boss_db/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"city.keyword": "深圳市"
}
},
{
"range": {
"min_work_time": {
"gte": 3
}
}
},
{
"range": {
"max_work_time": {
"lte": 5
}
}
}
]
}
}
}
需求:查询招聘信息,公司必须位于深圳市,且要求工作年限在3到5年之间。不计算得分。
#查询招聘信息,公司必须位于深圳市,且要求工作年限在3到5年之间。
GET /boss_db/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"city.keyword": "深圳市"
}
},
{
"range": {
"min_work_time": {
"gte": 3
}
}
},
{
"range": {
"max_work_time": {
"lte": 5
}
}
}
]
}
}
}
需求一:查询招聘信息,要求学历为本科或者公司地点位于天河区。
GET /boss_db/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"qualification.keyword": "本科"
}
},
{
"term": {
"county.keyword": "天河区"
}
}
]
}
}
}
需求二:查询招聘信息,要求学历为本科、公司地点位于天河区、公司人数大于2000人,三个条件必须符合两个。
GET /boss_db/_search
{
"query": {
"bool": {
"minimum_should_match": 2,
"should": [
{
"term": {
"qualification.keyword": "本科"
}
},
{
"term": {
"county.keyword": "天河区"
}
},
{
"range": {
"max_num": {
"gt": 2000
}
}
}
]
}
}
}
需求三:查询招聘信息,要求学历为本科或者公司地点位于天河区,但公司人数必须大于2000人。
GET /boss_db/_search
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"term": {
"qualification.keyword": "本科"
}
},
{
"term": {
"county.keyword": "天河区"
}
}
],
"must": [
{
"range": {
"max_num": {
"gt": 2000
}
}
}
]
}
}
}
需求:查询招聘信息,要求学历为本科或者公司地点位于天河区,但公司人数必须不大于2000人。
GET /boss_db/_search
{
"query": {
"bool": {
"should": [
{
"term": {
"qualification.keyword": "本科"
}
},
{
"term": {
"county.keyword": "天河区"
}
}
],
"must_not": [
{
"range": {
"max_num": {
"gt": 2000
}
}
}
]
}
}
}