1. 程式人生 > >elasticsearch查詢

elasticsearch查詢

AC 大於 red 網絡 source mysql-mmm pan 組合 index


#註意 以下字段 均默認text類型   
# match查詢
GET article2/info/_search
{
  "query":{
    "match":{
      "title":"MySQL-MMM"
    }
  },
  "from": 0,
  "size": 3
}


# term查詢 (屬於精確查找) 
GET article2/info/_search
{
  "query":{
    "term": {
      "title":"MySQL-MMM"
    }
  }
}


# terms查詢 #滿足一個詞就能查詢出來
GET article2/info/
_search { "query":{ "terms":{ "title":["linux", "振興"] } } } # match_all查詢 GET article2/info/_search { "query":{ "match_all":{} }, "from": 1, "size": 2 } # match_phrase查詢 #可以設置關鍵詞之間距離大於幾 GET article2/info/_search { "query":{ "match_phrase": { "title": { "query"
:"mysql集群", "slop": 2 } } } } # multi_match查詢 GET article2/info/_search { "query":{ "multi_match": { "query": "mysql", "fields": ["title^3","content"] } } } # 指定返回字段 GET article2/info/_search { "stored_fields": ["content"], "query":{ "match": { "title"
: "mysql" } } } # sort排序 GET article2/info/_search { "query":{ "match": { "title": "mysql" } }, "sort": [{ "_score": { "order": "desc" } }] } # 範圍查詢 GET article2/info/_search { "query":{ "range":{ "publish_date": { "gte":"2018/05/18", "lte":"now", "boost":1.0 } } } } # wildcard查詢 GET article2/info/_search { "query":{ "wildcard": { "title": { "value": "pyth*n", "boost": 2 } } } } # bool查詢 filtered被替換 # bool包括must,should,must_not,filter 4種 # bool:{ # "must":[], # "should":[], # "must_not":[], # "filter":[], # } # 添加模擬數據 POST test2/info/_bulk {"index":{"_id":1}} {"salary":100, "title":"Python"} {"index":{"_id":2}} {"salary":80, "title":"mysql"} {"index":{"_id":3}} {"salary":40, "title":"linux"} {"index":{"_id":4}} {"salary":90, "title":"java"} {"index":{"_id":5}} {"salary":50, "title":"ruby"} # 最簡單的filter查詢 不參與打分 # select * from info where salary=100; GET test2/info/_search { "query":{ "bool":{ "must":[ { "match_all":{} } ], "filter":[ { "term":{ "salary":100 } } ] } } } # select * from info where title=Python; GET test2/info/_search { "query":{ "bool":{ "must":[ { "match_all":{} } ], "filter":[ { "term":{ "title":"python" } } ] } } } # 用term無結果,因為大寫分詞時會成小寫,精確查找(filter)是找不到 # 要麽換成match, 要麽改成小寫python # 查看分詞器 GET _analyze { "analyzer": "ik_max_word", "text": "Python網絡" } # 論證 大寫分詞時會成小寫 # bool的組合過濾查詢 # select * from info where (salary=70 OR title=Python) AND (salary!=30) GET test2/info/_search { "query":{ "bool":{ "should":[ { "term":{ "salary":70 } }, { "term":{ "title":"python" } } ], "must_not":[ { "term":{ "salary":30 } } ] } } } # 為空和不為空查詢 # 模擬數據 POST test2/info2/_bulk {"index":{"_id":1}} {"tags":"lily"} {"index":{"_id":2}} {"tags":"jack"} {"index":{"_id":3}} {"tags":null} {"index":{"_id":4}} {"tags":"lucy"} {"index":{"_id":5}} {"tags":null} {"index":{"_id":6}} {"tags":"queen"} # 查詢不為空的tags GET test2/info2/_search { "query":{ "bool":{ "must": [ { "exists":{ "field":"tags" } } ] } } } # 查詢空tags(不查詢存在的tags ) GET test2/info2/_search { "query":{ "bool":{ "must_not": [ { "exists":{ "field":"tags" } } ] } } }

elasticsearch查詢