Python-ElasticSearch搜索查詢(xún)的講解
Elasticsearch 是一個(gè)開(kāi)源的搜索引擎,建立在一個(gè)全文搜索引擎庫(kù) Apache Lucene™ 基礎(chǔ)之上。 Lucene 可能是目前存在的,不論開(kāi)源還是私有的,擁有最先進(jìn),高性能和全功能搜索引擎功能的庫(kù)。但是 Lucene 僅僅只是一個(gè)庫(kù)。為了利用它,你需要編寫(xiě) Java 程序,并在你的 java 程序里面直接集成 Lucene 包。 更壞的情況是,你需要對(duì)信息檢索有一定程度的理解才能明白 Lucene 是怎么工作的。Lucene 是 很 復(fù)雜的。
在上一篇文章中介紹了ElasticSearch的簡(jiǎn)單使用,接下來(lái)記錄一下ElasticSearch的查詢(xún):
查詢(xún)所有數(shù)據(jù)
# 搜索所有數(shù)據(jù) es.search(index="my_index",doc_type="test_type") # 或者 body = { "query":{ "match_all":{} } } es.search(index="my_index",doc_type="test_type",body=body)
term與terms
# term body = { "query":{ "term":{ "name":"python" } } } # 查詢(xún)name="python"的所有數(shù)據(jù) es.search(index="my_index",doc_type="test_type",body=body) # terms body = { "query":{ "terms":{ "name":[ "python","android" ] } } } # 搜索出name="python"或name="android"的所有數(shù)據(jù) es.search(index="my_index",doc_type="test_type",body=body)
match與multi_match
# match:匹配name包含python關(guān)鍵字的數(shù)據(jù) body = { "query":{ "match":{ "name":"python" } } } # 查詢(xún)name包含python關(guān)鍵字的數(shù)據(jù) es.search(index="my_index",doc_type="test_type",body=body) # multi_match:在name和addr里匹配包含深圳關(guān)鍵字的數(shù)據(jù) body = { "query":{ "multi_match":{ "query":"深圳", "fields":["name","addr"] } } } # 查詢(xún)name和addr包含"深圳"關(guān)鍵字的數(shù)據(jù) es.search(index="my_index",doc_type="test_type",body=body)
ids
body = { "query":{ "ids":{ "type":"test_type", "values":[ "1","2" ] } } } # 搜索出id為1或2d的所有數(shù)據(jù) es.search(index="my_index",doc_type="test_type",body=body)
復(fù)合查詢(xún)bool
bool有3類(lèi)查詢(xún)關(guān)系,must(都滿(mǎn)足),should(其中一個(gè)滿(mǎn)足),must_not(都不滿(mǎn)足)
body = { "query":{ "bool":{ "must":[ { "term":{ "name":"python" } }, { "term":{ "age":18 } } ] } } } # 獲取name="python"并且age=18的所有數(shù)據(jù) es.search(index="my_index",doc_type="test_type",body=body)
切片式查詢(xún)
body = { "query":{ "match_all":{} } "from":2 # 從第二條數(shù)據(jù)開(kāi)始 "size":4 # 獲取4條數(shù)據(jù) } # 從第2條數(shù)據(jù)開(kāi)始,獲取4條數(shù)據(jù) es.search(index="my_index",doc_type="test_type",body=body)
范圍查詢(xún)
body = { "query":{ "range":{ "age":{ "gte":18, # >=18 "lte":30 # <=30 } } } } # 查詢(xún)18<=age<=30的所有數(shù)據(jù) es.search(index="my_index",doc_type="test_type",body=body)
前綴查詢(xún)
body = { "query":{ "prefix":{ "name":"p" } } } # 查詢(xún)前綴為"趙"的所有數(shù)據(jù) es.search(index="my_index",doc_type="test_type",body=body)
通配符查詢(xún)
body = { "query":{ "wildcard":{ "name":"*id" } } } # 查詢(xún)name以id為后綴的所有數(shù)據(jù) es.search(index="my_index",doc_type="test_type",body=body)
排序
body = { "query":{ "match_all":{} } "sort":{ "age":{ # 根據(jù)age字段升序排序 "order":"asc" # asc升序,desc降序 } } }
filter_path
響應(yīng)過(guò)濾
# 只需要獲取_id數(shù)據(jù),多個(gè)條件用逗號(hào)隔開(kāi) es.search(index="my_index",doc_type="test_type",filter_path=["hits.hits._id"]) # 獲取所有數(shù)據(jù) es.search(index="my_index",doc_type="test_type",filter_path=["hits.hits._*"])
count
執(zhí)行查詢(xún)并獲取該查詢(xún)的匹配數(shù)
# 獲取數(shù)據(jù)量 es.count(index="my_index",doc_type="test_type")
度量類(lèi)聚合
- 獲取最小值
body = { "query":{ "match_all":{} }, "aggs":{ # 聚合查詢(xún) "min_age":{ # 最小值的key "min":{ # 最小 "field":"age" # 查詢(xún)"age"的最小值 } } } } # 搜索所有數(shù)據(jù),并獲取age最小的值 es.search(index="my_index",doc_type="test_type",body=body)
- 獲取最大值
body = { "query":{ "match_all":{} }, "aggs":{ # 聚合查詢(xún) "max_age":{ # 最大值的key "max":{ # 最大 "field":"age" # 查詢(xún)"age"的最大值 } } } } # 搜索所有數(shù)據(jù),并獲取age最大的值 es.search(index="my_index",doc_type="test_type",body=body)
- 獲取和
body = { "query":{ "match_all":{} }, "aggs":{ # 聚合查詢(xún) "sum_age":{ # 和的key "sum":{ # 和 "field":"age" # 獲取所有age的和 } } } } # 搜索所有數(shù)據(jù),并獲取所有age的和 es.search(index="my_index",doc_type="test_type",body=body)
- 獲取平均值
body = { "query":{ "match_all":{} }, "aggs":{ # 聚合查詢(xún) "avg_age":{ # 平均值的key "sum":{ # 平均值 "field":"age" # 獲取所有age的平均值 } } } } # 搜索所有數(shù)據(jù),獲取所有age的平均值 es.search(index="my_index",doc_type="test_type",body=body)
更多的搜索用法:
https://elasticsearch-py.readthedocs.io/en/master/api.html
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
python爬蟲(chóng) 爬取超清壁紙代碼實(shí)例
這篇文章主要介紹了python爬蟲(chóng)學(xué)習(xí) 爬取超清壁紙代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08Flask的圖形化管理界面搭建框架Flask-Admin的使用教程
Flask-Admin是一個(gè)為Python的Flask框架服務(wù)的微型框架,可以像Django-Admin那樣為用戶(hù)生成Model層面的數(shù)據(jù)管理界面,接下來(lái)就一起來(lái)看一下Flask的圖形化管理界面搭建框架Flask-Admin的使用教程2016-06-06python區(qū)塊鏈創(chuàng)建多個(gè)交易教程
這篇文章主要為大家介紹了python區(qū)塊鏈創(chuàng)建多個(gè)交易的實(shí)現(xiàn)示例教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05用Python的Flask框架結(jié)合MySQL寫(xiě)一個(gè)內(nèi)存監(jiān)控程序
這篇文章主要介紹了用Python的Flask框架結(jié)合MySQL些一個(gè)內(nèi)存監(jiān)控程序的例子,并且能將結(jié)果作簡(jiǎn)單的圖形化顯示,需要的朋友可以參考下2015-11-11python讀取mat文件生成h5文件的實(shí)現(xiàn)
這篇文章主要介紹了python讀取mat文件生成h5文件的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07python爬蟲(chóng)實(shí)現(xiàn)獲取下一頁(yè)代碼
在本篇文章里小編給大家整理了關(guān)于python爬蟲(chóng)實(shí)現(xiàn)獲取下一頁(yè)代碼內(nèi)容,需要的朋友們可以參考學(xué)習(xí)下。2020-03-03