利用kibana插件對Elasticsearch進行bool查詢

#bool查詢
#老版本的filtered查詢已經被bool代替
#用 bool包括 must should must_not filter來完成 ,格式以下:
#bool:{
#  "filter":[],
#  "must":[],
#  "should":[],
#  "must_not"[],
#}
#must 數組內的全部查詢都必須知足
#should 數組內只須要知足一個
#must_not 一個都不能知足python

#創建測試數據
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的工做 django

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)spa

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 NULLcode

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 NULLblog

GET lagou/testdb2/_search
{
  "query": {
    "bool": {
      "must_not": {
        "exists": {
          "field": "tags"
        }
      }
    }
  }
}
相關文章
相關標籤/搜索