Python-ElasticSearch搜索查詢的講解
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的查詢:
查詢所有數(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"
}
}
}
# 查詢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"
}
}
}
# 查詢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"]
}
}
}
# 查詢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ù)合查詢bool
bool有3類查詢關(guān)系,must(都滿足),should(其中一個(gè)滿足),must_not(都不滿足)
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)
切片式查詢
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)
范圍查詢
body = {
"query":{
"range":{
"age":{
"gte":18, # >=18
"lte":30 # <=30
}
}
}
}
# 查詢18<=age<=30的所有數(shù)據(jù)
es.search(index="my_index",doc_type="test_type",body=body)
前綴查詢
body = {
"query":{
"prefix":{
"name":"p"
}
}
}
# 查詢前綴為"趙"的所有數(shù)據(jù)
es.search(index="my_index",doc_type="test_type",body=body)
通配符查詢
body = {
"query":{
"wildcard":{
"name":"*id"
}
}
}
# 查詢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í)行查詢并獲取該查詢的匹配數(shù)
# 獲取數(shù)據(jù)量 es.count(index="my_index",doc_type="test_type")
度量類聚合
- 獲取最小值
body = {
"query":{
"match_all":{}
},
"aggs":{ # 聚合查詢
"min_age":{ # 最小值的key
"min":{ # 最小
"field":"age" # 查詢"age"的最小值
}
}
}
}
# 搜索所有數(shù)據(jù),并獲取age最小的值
es.search(index="my_index",doc_type="test_type",body=body)
- 獲取最大值
body = {
"query":{
"match_all":{}
},
"aggs":{ # 聚合查詢
"max_age":{ # 最大值的key
"max":{ # 最大
"field":"age" # 查詢"age"的最大值
}
}
}
}
# 搜索所有數(shù)據(jù),并獲取age最大的值
es.search(index="my_index",doc_type="test_type",body=body)
- 獲取和
body = {
"query":{
"match_all":{}
},
"aggs":{ # 聚合查詢
"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":{ # 聚合查詢
"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)文章
django ORM之values和annotate使用詳解
這篇文章主要介紹了django ORM之values和annotate使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05
Python實(shí)現(xiàn)刪除list列表重復(fù)元素的方法總結(jié)
在Python編程中,我們經(jīng)常需要處理列表中的重復(fù)元素,這篇文章為大家介紹了五種高效的方法來(lái)刪除列表中的重復(fù)元素,希望對(duì)大家有所幫助2023-07-07
使用python+pandas讀寫(xiě)xlsx格式中的數(shù)據(jù)
這篇文章主要介紹了使用python+pandas讀寫(xiě)xlsx格式中的數(shù)據(jù),文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下2022-08-08
python實(shí)現(xiàn)簡(jiǎn)單中文詞頻統(tǒng)計(jì)示例
本篇文章主要介紹了python實(shí)現(xiàn)簡(jiǎn)單中文詞頻統(tǒng)計(jì)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
Python數(shù)據(jù)類型--字典dictionary
這篇文章主要介紹了Python數(shù)據(jù)類型字典dictionary,字典是另一種可變?nèi)萜髂P?,且可存?chǔ)任意類型對(duì)象。下面詳細(xì)內(nèi)容需要的小伙伴可以參考一下,希望對(duì)你有所幫助2022-02-02
python 定時(shí)器,實(shí)現(xiàn)每天凌晨3點(diǎn)執(zhí)行的方法
今天小編就為大家分享一篇python 定時(shí)器,實(shí)現(xiàn)每天凌晨3點(diǎn)執(zhí)行的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-02-02
Django 配置多站點(diǎn)多域名的實(shí)現(xiàn)步驟
這篇文章主要介紹了Django 配置多站點(diǎn)多域名的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
PyTorch中torch.nn.functional.cosine_similarity使用詳解
在pytorch中可以使用torch.cosine_similarity函數(shù)對(duì)兩個(gè)向量或者張量計(jì)算余弦相似度,這篇文章主要給大家介紹了關(guān)于PyTorch中torch.nn.functional.cosine_similarity使用的相關(guān)資料,需要的朋友可以參考下2022-03-03

