python調(diào)用Elasticsearch執(zhí)行增刪改查操作
基本操作
1.連接Elasticsearch數(shù)據(jù)庫
首先連接Elasticsearch數(shù)據(jù)庫,然后創(chuàng)建一個自定義的索引
from elasticsearch import Elasticsearch import random from elasticsearch import helpers # 連接到本地的 Elasticsearch 服務(wù) es = Elasticsearch(hosts=["http://localhost:9200"]) index_name = "my_index" # 創(chuàng)建一個索引名為 "my_index" 的索引 if es.indices.exists(index=index_name): # 如果索引存在,刪除 es.indices.delete(index=index_name) es.indices.create(index=index_name) # 新建索引
2.新增隨機數(shù)據(jù)
這里我們創(chuàng)建隨機數(shù)據(jù)項,包含value_num與value_choice項,
# 新增隨機數(shù)據(jù)項 add_value_list: list = [] for _ in range(1000): num = random.randint(1, 100) add_value_list.append({ "_index": index_name, # 注意!個參數(shù)決定要插入到哪個索引中 "value_num": random.randint(1, 100), "value_choice": ["c1", 'c2', 'c3'][random.randint(0, 2)], }) # 批量插入數(shù)據(jù) helpers.bulk(es, add_value_list)
3.查詢操作
# 查詢操作 _body_query = { "query": { "range": { "value_num": { "gte": 40, # >= 40 "lte": 60 # <= 60 } } }, "size": 20, # 查詢20條 } response = es.search(index=index_name, body=_body_query) # 打印查詢的結(jié)果 for _hit in response["hits"]["hits"]: _value = _hit['_source'] print("value_num:", _value["value_num"], " value_choice:", _value['value_choice'])
4.更新數(shù)據(jù)項
這里,我們將查詢出的數(shù)據(jù)中,通過文檔ID與修改的數(shù)據(jù)重新為數(shù)據(jù)賦值
# 更新操作 for _hit in response["hits"]["hits"]: update_body = {"doc": { "value_choice": "c4", # 更新value_choice字段為c4 }} res = es.update(index=index_name, id=_hit['_id'], body=update_body)
5.刪除數(shù)據(jù)項
# 刪除操作 for _hit in response["hits"]["hits"]: res = es.delete(index=index_name, id=_hit['_id'])
更多查詢方法
1. 查詢?nèi)繑?shù)據(jù)
_body_query = { "query":{ "match_all":{} } }
2. 針對某個確定的值/字符串的查詢:term、match
match會執(zhí)行多個term操作,term操作精度更高
_body_query = { "query": { "match": { "value_choice": "c1" } } }
_body_query = { "query": { "term": { "value_choice": "c1" } } }
3. 在多個選項中有一個匹配,就查出來:terms
_body_query = { "query": { "terms": { "value_choice": ["c1", "c2"], } } }
4. 數(shù)值范圍查詢:range
查詢>=40且<=60的數(shù)據(jù)
_body_query = { "query": { "range": { "value_num": { "gte": 40, # >= 40 "lte": 60 # <= 60 } } } }
5. 多個條件同時觸發(fā) bool
布爾查詢可以同時查詢多個條件,也稱為組合查詢,構(gòu)造查詢的字典數(shù)據(jù)時,query后緊跟bool,之后再跟bool的判斷條件,判斷條件有下面幾個:
- filter:過濾器
- must:類似and,需要所有條件都滿足
- should:類似or,只要能滿足一個即可
- must_not:需要都不滿足
寫完判斷條件后,在判斷條件的list里再緊跟查詢操作的具體細節(jié)
_body_query = { "query": { "bool": { "should": [ { "match": {"value_choice": "c1"} # value_choice = "c1" }, { "range": {"value_num": {"lte": 50}} # value_num <= 50 } ] } }, }
6. 指定返回值個數(shù) size
在構(gòu)造的字典中添加size關(guān)鍵字即可
_body_query = { "query": { "range": { "value_num": { "gte": 40, # >= 40 "lte": 60 # <= 60 } } }, "size": 20, }
7. 返回指定列 _source
_body_query = { "query": { "range": { "value_num": { "gte": 40, # >= 40 "lte": 60 # <= 60 } } }, "_source": ["value_num"] # 這里指定返回的fields }
完整示例程序
from elasticsearch import Elasticsearch import random from elasticsearch import helpers # 連接到本地的 Elasticsearch 服務(wù) es = Elasticsearch(hosts=["http://localhost:9200"]) index_name = "my_index" # 創(chuàng)建一個索引名為 "my_index" 的索引 if es.indices.exists(index=index_name): # 如果索引存在,刪除 es.indices.delete(index=index_name) es.indices.create(index=index_name) # 新建索引 # 生成隨機數(shù)據(jù) add_value_list: list = [] for _ in range(1000): num = random.randint(1, 100) add_value_list.append({ "_index": index_name, # 注意!個參數(shù)決定要插入到哪個索引中 "value_num": random.randint(1, 100), "value_choice": ["c1", 'c2', 'c3'][random.randint(0, 2)], }) # 批量插入數(shù)據(jù) helpers.bulk(es, add_value_list) # ================== 開始增刪改查 ================== _body_query = { "query": { "range": { "value_num": { "gte": 40, # >= 40 "lte": 60 # <= 60 } } }, "size": 20, } response = es.search(index=index_name, body=_body_query) # 查詢10條 # 打印查詢的結(jié)果 for _hit in response["hits"]["hits"]: _value = _hit['_source'] print("value_num:", _value["value_num"], " value_choice:", _value['value_choice']) # 更新操作 for _hit in response["hits"]["hits"]: update_body = {"doc": { "value_choice": "c4", # 更新value_choice字段為c4 }} res = es.update(index=index_name, id=_hit['_id'], body=update_body) # 刪除操作 for _hit in response["hits"]["hits"]: res = es.delete(index=index_name, id=_hit['_id'])
到此這篇關(guān)于python調(diào)用Elasticsearch執(zhí)行增刪改查操作的文章就介紹到這了,更多相關(guān)python Elasticsearch增刪改查操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python base64圖片互轉(zhuǎn),解決base64字符串轉(zhuǎn)PIL圖片對象報錯:binascii.Error:
在Base64編碼中,若字符串長度不是4的倍數(shù),需在末尾添加等號作為填充,不符合此規(guī)則會導(dǎo)致在轉(zhuǎn)換為圖片時出現(xiàn)binascii.Error:Incorrectpadding錯誤,正確的填充確保編碼后的字符串可以正確轉(zhuǎn)換成圖片,避免轉(zhuǎn)換錯誤2024-09-09Python使用itchat模塊實現(xiàn)簡單的微信控制電腦功能示例
這篇文章主要介紹了Python使用itchat模塊實現(xiàn)簡單的微信控制電腦功能,結(jié)合實例形式分析了Python基于itchat模塊控制電腦實現(xiàn)運行程序、截圖等相關(guān)操作技巧,需要的朋友可以參考下2019-08-08Python 靜態(tài)導(dǎo)入與動態(tài)導(dǎo)入的實現(xiàn)示例
Python靜態(tài)導(dǎo)入和動態(tài)導(dǎo)入是指導(dǎo)入模塊或模塊內(nèi)部函數(shù)的兩種方式,本文主要介紹了Python 靜態(tài)導(dǎo)入與動態(tài)導(dǎo)入的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下2024-05-05- python中的easy_install工具,類似于Php中的pear,或者Ruby中的gem,或者Perl中的cpan,那是相當(dāng)?shù)乃嵬崃巳绻胧褂?/div> 2013-02-02
WIn10+Anaconda環(huán)境下安裝PyTorch(避坑指南)
這篇文章主要介紹了WIn10+Anaconda環(huán)境下安裝PyTorch(避坑指南),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01python調(diào)用動態(tài)鏈接庫的基本過程詳解
這篇文章主要介紹了python調(diào)用動態(tài)鏈接庫的基本過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06python3中類的繼承以及self和super的區(qū)別詳解
今天小編就為大家分享一篇python3中類的繼承以及self和super的區(qū)別詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06最新評論