Elasticsearch-bool组合查询

本文介绍了Elasticsearch中bool查询的使用方法,包括filter、must、should、must_not等子句的含义和示例。例如,如何通过bool查询过滤特定条件的数据,如筛选出工资等于特定值或标题包含特定关键字的记录,并展示了处理空值的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# bool组合查询
# filter:过滤,不参与打分
# must:如果有多个条件,这些条件都必须满足  and与
# should:如果有多个条件,满足一个或多个即可 or或
# must_not:和must相反,必须都不满足条件才可以匹配到 !非


GET 51jobs/job/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      }, 
      "filter": {
        "term": {
          "salary": 6666
        }
      
      }
    }
  }
}

# select * from job where salary=6666 or salary=7777
# 查询所有数据,筛选出工资等于6666或者7777的数据
GET 51jobs/job/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      }, 
      "filter": {
        "terms": {
          "salary": [6666,7777]
        }
      
      }
    }
  }
}
# 查询salary等于6666或者title等于python、salary不等于7777、salary不等于8888

GET 51jobs/job/_search
{
  "query": {
    "bool": {
      "should": [
        {"term": {
          "salary": {
            "value": 6666
          }
        }},
        {"term": {
          "title": {
            "value": "python"
          }
        }}
      ],
      "must_not": [
        {"term": {
          "salary": {
            "value": 7777
          }
        }},
        {"term": {
          "salary": {
            "value": 8888
          }
        }}
      ]
    }
  }
}

# salary等于6666或者title等于python salary不等于9999 title不等于redis

GET 51jobs/job/_search
{
  "query": {
    "bool": {
      "should": [
        {"term": {
          "salary": {
            "value": 6666
          }
        }},
        {"term": {
          "title": {
            "value": "python"
          }
        }}
      ],
      "must_not": [
        {"term": {
          "salary": {
            "value": 9999
          }
        }},
        {
          "term": {
            "title": {
              "value": "redis"
            }
          }
        }
      ]
    }
  }
}
# 查询title为python或者(title为搜索并且salary等于6666)的数据
GET 51jobs/job/_search
{
  "query": {
    "bool": {
      "should": [
        {"term": {
          "title": {
            "value": "python"
          }
        }},
        {
          "bool": {
            "must": [
              {"term": {
                "title": {
                  "value": "搜索"
                }
              }},
              {
                "term": {
                  "salary": {
                    "value": 6666
                  }
                }
              }
            ]
          }
          
        }
      ]
    }
  }
}


# 添加空值测试数据
PUT nulldb/test/_bulk
{"index":{"_id":1}}
{"tags":["IT","python"]}
{"index":{"_id":2}}
{"tags":["java","python"]}
{"index":{"_id":3}}
{"tags":null}
{"index":{"_id":4}}
{"tags":["IT","php"]}
{"index":{"_id":5}}
{"tags":[null,"python"]}


# 处理null空值
# 过滤空值
# exists 处理值不为空或者值不为null的数据
GET nulldb/test/_search
{
  "query": {
    "bool": {
      "filter": {
        "exists": {
          "field": "tags"
        }
      }
    }
  }
}

# 筛选出tags值为空或者没有tags属性的数据
GET nulldb/test/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "exists":{
            "field":"tags"
          }
        }
      ]
    }
  }
}












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值