利用kibana插件对Elasticsearch进行bool查询

#bool查询
#老版本的filtered查询已经被bool代替
#用 bool包括 must should must_not filter来完成 ,格式如下:
#bool:{
#  "filter":[],
#  "must":[],
#  "should":[],
#  "must_not"[],
#}
#must 数组内的所有查询都必须满足
#should 数组内只需要满足一个
#must_not 一个都不能满足

#建立测试数据
POST lagou/testdb/_bulk
{"index":{"_id":1}}
{"salary":10,"title":"python"}
{"index":{"_id":2}}
{"salary":20,"title":"Scrapy"}
{"index":{"_id":3}}
{"salary":30,"title":"Django"}
{"index":{"_id":4}}
{"salary":40,"title":"Elasticsearch"}

 


#简单过滤查询

#最简单的fiter查询
#select * from testdb where salary=20
#filter 薪资为20的工作

GET lagou/testdb/_search
{
  "query":{
    "bool": {
      "must":{
        "match_all":{}
      },
      "filter": {
        "terms": {
          "salary": [20,10]  
        }
      }
    }
  }
}

 


#filter里面也能写多值查询

#select * from testdb where title="python"
GET lagou/testdb/_search
{
  "query":{
    "bool": {
      "must":{
        "match_all":{}
      },
      "filter": {
        "term": {
          "title": "Python"
        }
      }
    }
  }
}

 


#数据在入库的时候就都已经进行大小写处理了,所以现在用term查询的时候是找不到需要的内容的,但是用match的时候就可以了

 

#查看分析器的解析结果
GET _analyze
{
  "analyzer": "ik_max_word",
  "text":"python网络"
}

 


#bool过滤查询,可以组合过滤查询

# select * from testdb where (salary=20 OR title=Python) AND (salary != 30)
# 查询薪资等于20k或者工作为python的工作,排除价格为30k的

GET lagou/testdb/_search
{
  "query":{
    "bool": {
      "should":[
        {"term": {"salary": 20}},
        {"term": {"title": "python"}}
        ],
        "must_not": [
          {"term":{"salary":30}},
          {"term":{"salary":10}}
        ]
    }
  }
} x

 


#嵌套查询

#select * from testdb where title="python" or ( title="django" AND salary=30)

GET lagou/testdb/_search
{
  "query":{
    "bool": {
      "should":[
        {"term": {"title": "python"}},
        {"bool": {
          "must": [
            {"term": {"title":  "django"}},
            {"term": {"salary": 30}}
          ]
        }}
        ]
    }
  }
} 

 


#过滤空和非空


#建立测试数
#select tags from testdb2 where tags is not NULL

GET lagou/testdb2/_bulk
{"index":{"_id":"1"}}
{"tags":["salary"]}
{"index":{"_id":"2"}}
{"tags":["salary","python"]}
{"index":{"_id":"3"}}
{"other_fields":["some data"]}
{"index":{"_id":"4"}}
{"tags":null}
{"index":{"_id":"5"}}
{"tags":["salary",null]}

 

 


#处理null空值的方法

#select tags from testdb2 where tags is not NULL

GET lagou/testdb2/_search
{
  "query": {
    "bool": {
      "must_not": {
        "exists": {
          "field": "tags"
        }
      }
    }
  }
}

 

转载于:https://www.cnblogs.com/fengshuihuan/p/7928193.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值