Elasticsearch之基本查詢及組合查詢操作示例
更新時間:2022年04月19日 17:54:57 作者:Jeff的技術棧
這篇文章主要為大家介紹了Elasticsearch之基本查詢及組合查詢操作示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
Elasticsearch查詢
查詢分類:
基本查詢:使用es內置查詢條件進行查詢
組合查詢:把多個查詢組合在一起進行復合查詢
過濾:查詢的同時,通過filter條件在不影響打分的情況下篩選數(shù)據(jù)
一 基本查詢
#添加映射 PUT lago { "mappings": { "properties":{ "title":{ "stort":true, "type":"text", "analyzer":"ik_max_word" }, "company_name":{ "stort":true, "type":"keyword", }, "desc":{ "type":"text" }, "comments":{ "type":"integer" }, "add_time":{ "type":"date", "format":"yyy-MM-dd" } } } } #測試數(shù)據(jù) POST lago/job { "title":"python django 開發(fā)工程師", "company_name":"美團科技有限公司", "desc":"對django熟悉,掌握mysql和非關系型數(shù)據(jù)庫,網(wǎng)站開發(fā)", "comments:200, "add_time":"2018-4-1" } POST lago/job { "title":"python數(shù)據(jù)分析", "company_name":"百度科技有限公司", "desc":"熟悉python基礎語法,熟悉數(shù)據(jù)分析", "comments:5, "add_time":"2018-10-1" } POST lago/job { "title":"python自動化運維", "company_name":"上海華為", "desc":"熟悉python基礎語法,精通Linux", "comments:90, "add_time":"2019-9-18" }
1.1 match查詢
GET lagou/job/_search { "query":{ "match":{ "title":"python" } } } #因為title字段做了分詞,python都能搜索出來 #搜索python網(wǎng)站也能搜索出來,把python和網(wǎng)站分成兩個詞 #搜索爬取也能搜索到,把爬和取分詞,去搜索 #只搜取 搜不到
1.2 term查詢
GET lagou/_search { "query":{ "term":{ "title":"python" } } } #會拿著要查詢的詞不做任何處理,直接查詢 #用python爬蟲,查不到,用match就能查到 { "query":{ "term":{ "company_name":"美團" } } } #通過美團,就查詢不到
1.3 terms查詢
GET lagou/_search { "query":{ "terms":{ "title":["工程師","django","運維"] } } } #三個詞,只要有一個,就會查詢出來
1.4 控制查詢的返回數(shù)量(分頁)
GET lagou/_search { "query":{ "match":{ "title":"python" } }, "form":1, "size":2 } #從第一條開始,大小為2
1.5 match_all 查詢
GET lagou/_search { "query":{ "match_all":{} } } #所有數(shù)據(jù)都返回
1.6 match_phrase查詢
GET lagou/_search { "query":{ "match_phrase":{ "title":{ "query":"python系統(tǒng)", "slop":6 } } } } #短語查詢, #會把查詢條件python和系統(tǒng)分詞,放到列表中,再去搜索的時候,必須滿足python和系統(tǒng)同時存在的才能搜出來 #"slop":6 :python和系統(tǒng)這兩個詞之間最小的距離
1.7 multi_match
GET lagou/_search { "query":{ "multy_match":{ "query":"python", "fields":["title","desc"] } } } #可以指定多個字段 #比如查詢title和desc這個兩個字段中包含python關鍵詞的文檔 #"fields":["title^3","desc"]:權重,title中的python是desc中的三倍
1.8 指定返回的字段
GET lagou/_search { "query":{ "stored_fields":["title","company_name"] "match":{ "title":"python" } } } #只返回title和company_name字段 #"stored_fields":["title","company_name",'dsc'],不會返回dsc,因為我們要求stroed_fields,之前desc字段設為false(默認),不會顯示
1.9 sort 結果排序
GET lagou/_search { "query":{ "match_all":{} }, "sort":[ { "comments":{ "order":"desc" } } ] } #查詢所有文檔,按comments按desc降序排序
1.10 range范圍查詢
GET lagou/_search { "query":{ "range":{ "comments":{ "gte":10, "lte":20, "boost":2.0 } } } } #指定comments字段大于等于10,小于等于20 #boost:權重 GET lagou/_search { "query":{ "range":{ "add_time":{ "gte":"2019-10-11", "lte":"now", } } } } #對時間進行查詢
1.11 wildcard查詢
GET lagou/_search { "query":{ "wildcard":{ "title":{ "value":"pyth*n", "boost":2.0 } } } } #模糊查詢,title中,有pyth任意值n得都能查出來
1.12 exists存在
exists:字段包含,存在的 # 包含followers_count字段 GET user_toutiao/_search { "query": { "bool": { "must": [ {"exists": { "field": "followers_count" }} ] } } } # 不包含followers_count字段 GET user_toutiao/_count { "query": { "bool": { "must_not": [ {"exists": { "field": "followers_count" }} ] } } } # 不包含followers_count且updata_timestamp>1614221216 GET user_toutiao/_count { "query": { "bool": { "must_not": [ { "exists": { "field": "followers_count" } } ], "must": [ {"range": { "updata_timestamp": { "gt": 1614221216 } }} ] } } }
二 組合查詢
2.1 bool查詢
#bool查詢包括must should must_not filter ''' bool:{ "filter":[], 字段過濾 "must":[], 所有查詢條件都滿足 "should":[], 滿足一個或多個 "must_not":{} 都不滿足于must相反 } ''' # 建立測試數(shù)據(jù) POST lago/testjob/_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":30,"title":"Elasticsearch"}
2.2 簡單過濾查詢
#select * from testjob where salary=20 GET lagou/testjob/_search { "query":{ "bool":{ "must":{ "match_all":{} }, "filter":{ "term":{ "salary":20 } } } } }
2.3 查詢多個值
#查詢薪資是10k或20k的 GET lagou/testjob/_search { "query":{ "bool":{ "must":{ "match_all":{} }, "filter":{ "terms":{ "salary":[10,20] } } } } } #select * from testjob where title="python" GET lagou/testjob/_search { "query":{ "bool":{ "must":{ "match_all":{} }, "filter":{ "term":{ "title":"Python" } } } } } #title 是text字段,會做大小寫轉換,term不會預處理,拿著大寫Python去查查不到 #可以改成小寫,或者用match來查詢 ''' "filter":{ "match":{ "title":"Python" } } ''' #查看分析器解析結果 GET _analyze { "analyzer":"ik_max_word", "text":"python網(wǎng)絡開發(fā)工程師" }
2.4 bool過濾查詢,可以做組合過濾查詢
#select * from testjob where (salary=20 or title=Python) and (salary!=30) #查詢薪資等于20k或者工作為python的工作,排除價格為30k的 { "query":{ "bool":{ "should":[ {"term":{"salary":20}}, {"term":{"title":"python"}} ], "must_not":{ "term":{"salary":30} } } } } #select * from testjob where title=python or (title=django and salary=30) { "query":{ "bool":{ "should":[ {"term":{"title":"python"}}, { "bool":{ "must":[ {"term":{"title":"django"}}, {"term":{"salary":30}} ] } } ] } } }
以上就是Elasticsearch之基本查詢及組合查詢操作示例的詳細內容,更多關于Elasticsearch基本查詢組合查詢的資料請關注腳本之家其它相關文章!
相關文章
Loongnix安裝PyCharm Community 2020.2.3的教程詳解
這篇文章主要介紹了Loongnix安裝PyCharm Community 2020.2.3的教程詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11Typora?免費版下載安裝入門使用教程(超簡單親測適用于Windows)
Typora是一款輕便簡潔的Markdown編輯器,支持即時渲染技術,這也是與其他Markdown編輯器最顯著的區(qū)別,這篇文章主要介紹了Typora?免費版下載安裝(超簡單親測適用于Windows)與入門,需要的朋友可以參考下2023-09-09都2019年了,還問http中GET和POST的區(qū)別
最近看了一些同學的面經,發(fā)現(xiàn)無論什么技術崗位,還是會問到 get 和 post 的區(qū)別,而搜索出來的答案并不能讓我們裝得一手好逼,那就讓我們從 HTTP 報文的角度來擼一波,從而搞明白他們的區(qū)別2019-02-02vs2019+cmake實現(xiàn)Linux遠程開發(fā)的方法步驟
這篇文章主要介紹了vs2019+cmake實現(xiàn)Linux遠程開發(fā)的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04