• Elasticsearch语法知多少之Boolean query


    目录

    目标

    ES版本信息

    官方文档

    什么是布尔查询

    实战

    新增测试数据

    must

    filter

    should

    must_not


    目标

    掌握布尔查询,分析子句的多种类型使用方法。


    ES版本信息

    7.17.5


    官方文档

    Boolean queryicon-default.png?t=M666https://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必须不匹配,不贡献算分。相当于!。

    实战

    新增测试数据

    1. PUT /boss_db
    2. {
    3. "settings": {
    4. "index": {
    5. "analysis.analyzer.default.type": "ik_max_word"
    6. }
    7. }
    8. }
    9. PUT /boss_db/_bulk
    10. {"index":{"_id":"1"}}
    11. {"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"]}
    12. {"index":{"_id":"2"}}
    13. {"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","微服务"]}
    14. {"index":{"_id":"3"}}
    15. {"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","微服务"]}
    16. {"index":{"_id":"4"}}
    17. {"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"]}
    18. {"index":{"_id":"5"}}
    19. {"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","数据结构"]}
    20. {"index":{"_id":"6"}}
    21. {"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"]}
    22. {"index":{"_id":"7"}}
    23. {"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}
    24. {"index":{"_id":"8"}}
    25. {"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","容器技术","微服务技术"]}
    26. {"index":{"_id":"9"}}
    27. {"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","云原生技术","电商"]}

    must

    需求:查询招聘信息,公司必须位于深圳市,且要求工作年限在3到5年之间。计算得分。

    1. #查询招聘信息,公司必须位于深圳市,且要求工作年限在3到5年之间。
    2. GET /boss_db/_search
    3. {
    4. "query": {
    5. "bool": {
    6. "must": [
    7. {
    8. "term": {
    9. "city.keyword": "深圳市"
    10. }
    11. },
    12. {
    13. "range": {
    14. "min_work_time": {
    15. "gte": 3
    16. }
    17. }
    18. },
    19. {
    20. "range": {
    21. "max_work_time": {
    22. "lte": 5
    23. }
    24. }
    25. }
    26. ]
    27. }
    28. }
    29. }

    filter

    需求:查询招聘信息,公司必须位于深圳市,且要求工作年限在3到5年之间。不计算得分。

    1. #查询招聘信息,公司必须位于深圳市,且要求工作年限在3到5年之间。
    2. GET /boss_db/_search
    3. {
    4. "query": {
    5. "bool": {
    6. "filter": [
    7. {
    8. "term": {
    9. "city.keyword": "深圳市"
    10. }
    11. },
    12. {
    13. "range": {
    14. "min_work_time": {
    15. "gte": 3
    16. }
    17. }
    18. },
    19. {
    20. "range": {
    21. "max_work_time": {
    22. "lte": 5
    23. }
    24. }
    25. }
    26. ]
    27. }
    28. }
    29. }

    should

    需求一:查询招聘信息,要求学历为本科或者公司地点位于天河区。

    1. GET /boss_db/_search
    2. {
    3. "query": {
    4. "bool": {
    5. "should": [
    6. {
    7. "term": {
    8. "qualification.keyword": "本科"
    9. }
    10. },
    11. {
    12. "term": {
    13. "county.keyword": "天河区"
    14. }
    15. }
    16. ]
    17. }
    18. }
    19. }

    需求二:查询招聘信息,要求学历为本科、公司地点位于天河区、公司人数大于2000人,三个条件必须符合两个。

    1. GET /boss_db/_search
    2. {
    3. "query": {
    4. "bool": {
    5. "minimum_should_match": 2,
    6. "should": [
    7. {
    8. "term": {
    9. "qualification.keyword": "本科"
    10. }
    11. },
    12. {
    13. "term": {
    14. "county.keyword": "天河区"
    15. }
    16. },
    17. {
    18. "range": {
    19. "max_num": {
    20. "gt": 2000
    21. }
    22. }
    23. }
    24. ]
    25. }
    26. }
    27. }

    需求三:查询招聘信息,要求学历为本科或者公司地点位于天河区,但公司人数必须大于2000人。

    1. GET /boss_db/_search
    2. {
    3. "query": {
    4. "bool": {
    5. "minimum_should_match": 1,
    6. "should": [
    7. {
    8. "term": {
    9. "qualification.keyword": "本科"
    10. }
    11. },
    12. {
    13. "term": {
    14. "county.keyword": "天河区"
    15. }
    16. }
    17. ],
    18. "must": [
    19. {
    20. "range": {
    21. "max_num": {
    22. "gt": 2000
    23. }
    24. }
    25. }
    26. ]
    27. }
    28. }
    29. }

    must_not

    需求:查询招聘信息,要求学历为本科或者公司地点位于天河区,但公司人数必须不大于2000人。

    1. GET /boss_db/_search
    2. {
    3. "query": {
    4. "bool": {
    5. "should": [
    6. {
    7. "term": {
    8. "qualification.keyword": "本科"
    9. }
    10. },
    11. {
    12. "term": {
    13. "county.keyword": "天河区"
    14. }
    15. }
    16. ],
    17. "must_not": [
    18. {
    19. "range": {
    20. "max_num": {
    21. "gt": 2000
    22. }
    23. }
    24. }
    25. ]
    26. }
    27. }
    28. }

  • 相关阅读:
    创建型设计模式六兄弟
    3D模型轻量化,看云端地球如何做...
    机器学习:详解朴素贝叶斯分类原理 | 例题分析 | Python实现
    Kotlin前置检测判断check,require,requireNotNull
    Maven 集成 Wagon
    day51
    Zookeeper选举Leader源码剖析(二)
    chatGPT 使用随想
    Python 基础入门
    GO微服务实战第三十二节 如何使用 ELK 进行日志采集以及统一处理?
  • 原文地址:https://blog.csdn.net/qq_39706570/article/details/126415622