python調(diào)用Elasticsearch執(zhí)行增刪改查操作
基本操作
1.連接Elasticsearch數(shù)據(jù)庫
首先連接Elasticsearch數(shù)據(jù)庫,然后創(chuàng)建一個(gè)自定義的索引
from elasticsearch import Elasticsearch import random from elasticsearch import helpers # 連接到本地的 Elasticsearch 服務(wù) es = Elasticsearch(hosts=["http://localhost:9200"]) index_name = "my_index" # 創(chuàng)建一個(gè)索引名為 "my_index" 的索引 if es.indices.exists(index=index_name): # 如果索引存在,刪除 es.indices.delete(index=index_name) es.indices.create(index=index_name) # 新建索引
2.新增隨機(jī)數(shù)據(jù)
這里我們創(chuàng)建隨機(jī)數(shù)據(jù)項(xiàng),包含value_num與value_choice項(xiàng),
# 新增隨機(jī)數(shù)據(jù)項(xiàng) add_value_list: list = [] for _ in range(1000): num = random.randint(1, 100) add_value_list.append({ "_index": index_name, # 注意!個(gè)參數(shù)決定要插入到哪個(gè)索引中 "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ù)項(xiàng)
這里,我們將查詢出的數(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ù)項(xiàng)
# 刪除操作 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. 針對(duì)某個(gè)確定的值/字符串的查詢:term、match
match會(huì)執(zhí)行多個(gè)term操作,term操作精度更高
_body_query = { "query": { "match": { "value_choice": "c1" } } }
_body_query = { "query": { "term": { "value_choice": "c1" } } }
3. 在多個(gè)選項(xiàng)中有一個(gè)匹配,就查出來: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. 多個(gè)條件同時(shí)觸發(fā) bool
布爾查詢可以同時(shí)查詢多個(gè)條件,也稱為組合查詢,構(gòu)造查詢的字典數(shù)據(jù)時(shí),query后緊跟bool,之后再跟bool的判斷條件,判斷條件有下面幾個(gè):
- filter:過濾器
- must:類似and,需要所有條件都滿足
- should:類似or,只要能滿足一個(gè)即可
- must_not:需要都不滿足
寫完判斷條件后,在判斷條件的list里再緊跟查詢操作的具體細(xì)節(jié)
_body_query = { "query": { "bool": { "should": [ { "match": {"value_choice": "c1"} # value_choice = "c1" }, { "range": {"value_num": {"lte": 50}} # value_num <= 50 } ] } }, }
6. 指定返回值個(gè)數(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)建一個(gè)索引名為 "my_index" 的索引 if es.indices.exists(index=index_name): # 如果索引存在,刪除 es.indices.delete(index=index_name) es.indices.create(index=index_name) # 新建索引 # 生成隨機(jī)數(shù)據(jù) add_value_list: list = [] for _ in range(1000): num = random.randint(1, 100) add_value_list.append({ "_index": index_name, # 注意!個(gè)參數(shù)決定要插入到哪個(gè)索引中 "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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python base64圖片互轉(zhuǎn),解決base64字符串轉(zhuǎn)PIL圖片對(duì)象報(bào)錯(cuò):binascii.Error:
在Base64編碼中,若字符串長度不是4的倍數(shù),需在末尾添加等號(hào)作為填充,不符合此規(guī)則會(huì)導(dǎo)致在轉(zhuǎn)換為圖片時(shí)出現(xiàn)binascii.Error:Incorrectpadding錯(cuò)誤,正確的填充確保編碼后的字符串可以正確轉(zhuǎn)換成圖片,避免轉(zhuǎn)換錯(cuò)誤2024-09-09Python使用itchat模塊實(shí)現(xiàn)簡單的微信控制電腦功能示例
這篇文章主要介紹了Python使用itchat模塊實(shí)現(xiàn)簡單的微信控制電腦功能,結(jié)合實(shí)例形式分析了Python基于itchat模塊控制電腦實(shí)現(xiàn)運(yùn)行程序、截圖等相關(guān)操作技巧,需要的朋友可以參考下2019-08-08Python 靜態(tài)導(dǎo)入與動(dòng)態(tài)導(dǎo)入的實(shí)現(xiàn)示例
Python靜態(tài)導(dǎo)入和動(dòng)態(tài)導(dǎo)入是指導(dǎo)入模塊或模塊內(nèi)部函數(shù)的兩種方式,本文主要介紹了Python 靜態(tài)導(dǎo)入與動(dòng)態(tài)導(dǎo)入的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05用Python實(shí)現(xiàn)協(xié)同過濾的教程
這篇文章主要介紹了用Python實(shí)現(xiàn)協(xié)同過濾的教程,主要用于從大數(shù)據(jù)中抽取用戶信息偏好等等,需要的朋友可以參考下2015-04-04- 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(避坑指南),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01python調(diào)用動(dòng)態(tài)鏈接庫的基本過程詳解
這篇文章主要介紹了python調(diào)用動(dòng)態(tài)鏈接庫的基本過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06python3中類的繼承以及self和super的區(qū)別詳解
今天小編就為大家分享一篇python3中類的繼承以及self和super的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06python 實(shí)現(xiàn)Harris角點(diǎn)檢測算法
這篇文章主要介紹了python 實(shí)現(xiàn)Harris角點(diǎn)檢測算法,幫助大家更好的利用python處理圖像,感興趣的朋友可以了解下2020-12-12最新評(píng)論