Python連接es之es更新操作示例詳解
使用 Python 對數(shù)據(jù)進(jìn)行更新操作
對于 es 的更新的操作,不用到 Search() 方法,而是直接使用 es 的連接加上相應(yīng)的函數(shù)來操作,本篇筆記目錄如下:
- 獲取連接
- update()
- update_by_query()
- 批量更新
- UpdateByQuery()
1、獲取連接
如果使用的是之前的全局創(chuàng)建連接的方式:
from elasticsearch_dsl import connections connections.configure( default={"hosts": "localhost:9200"}, )
我們可以根據(jù)別名獲取相應(yīng)的連接:
conn = connections.connections.get_connection("default")
或者我們直接使用 elasticsearch.Elasticsearch 模塊來重新建立一個連接:
from elasticsearch import Elasticsearch conn = Elasticsearch(hosts="localhost:9200")
前面介紹過,我們安裝 elasticsearch_dsl 依賴的時候,會自動為我們安裝上相應(yīng)的 elasticsearch 模塊,我們這里直接使用即可。
然后通過 conn 連接可以直接對數(shù)據(jù)進(jìn)行更新,可用的方法有 update(),update_by_query() 以及一個批量的 bulk() 方法。
2、update()
update() 函數(shù)一般只用于指定 id 的更新操作,如果我們知道一條數(shù)據(jù)的 id,我們可以直接使用 update()。
比如對于 exam 這個 index 下 id=18 的數(shù)據(jù),我們想要更新它的 name 字段和 address 字段分別為 王五和湖南省,我們可以如下操作:
conn.update( index="exam", id=18, body={ "doc": { "name": "王五2", "address": "湖南省", } } )
在上面的操作中,index 為指定的索引,id 參數(shù)為我們需要更新的 id,body 內(nèi) doc 下的字段即為我們要更新的數(shù)據(jù)。
3、update_by_query()
update_by_query() 函數(shù)不局限于 id 的查詢更新,我們可以更新任意符合條件的數(shù)據(jù),以下是一個簡單的示例:
conn.update_by_query( index="exam", body={ "query": { "term": {"name": "張三豐"} }, "script": { "source": "ctx._source.address = params.address", "params": { "address": "新地址", } } } )
在這里,index 參數(shù)還是指向?qū)?yīng)的索引,body 內(nèi)包含了需要更新查詢的條件,這里都在 query 參數(shù)內(nèi),需要更新的數(shù)據(jù)在 script 下,通過腳本的形式來操作更新。
這里注意下,我這里用到的是 7.6.0 版本,所以 script 下使用的 source,更低一點版本用的字段可能是 inline,這里使用對應(yīng)版本的參數(shù)即可。
在 script.source 中,內(nèi)容為 ctx._source.address = params.address
,意思是將符合條件數(shù)據(jù)的 address 字段內(nèi)容更新為 params 的 address 的數(shù)據(jù)。
如果想要更改其他字段內(nèi)容,注意前面 ctx._source 為固定寫法,只需要更改后面的字段名即可。
在 script.params 中,我們則可以定義各種對應(yīng)的字段及其內(nèi)容。
更新多個字段
如果我們想同時更新多個字段,比如說符合條件的數(shù)據(jù)將 address 改為 新地址
,將 age 字段改為 28,我們則需要將多個條件在 script.source 中使用分號 ;
連接起來,示例如下:
conn.update_by_query( index="exam", body={ "query": { "term": {"name": "新張三豐2"} }, "script": { "source": "ctx._source.address = params.address; ctx._source.age = params.age", "params": { "address": "新地址3", "age": "28" } } } )
雖然這里更新多個字段需要使用分號連接,但是在實際的代碼中我們不用這么寫死,比如說我們需要更改三個字段,為 ["address", "name", "age"]
,我們?nèi)缦虏僮鳎?/p>
field_list = ["address", "name", "age"] source_list = [f"ctx._source.{key}=params.{key}" for key in field_list] params = { "address": "新地址3", "age": "28", "name": "new name" } conn.update_by_query( index="exam", body={ "query": { "term": {"name": "新張三豐3"} }, "script": { "source": ";".join(source_list), "params": params } } )
4、批量更新
如果我們想批量更新一批數(shù)據(jù),這批數(shù)據(jù)各個字段的值都不一致,自定義的程度很大,使用 update_by_query() 函數(shù)已經(jīng)不現(xiàn)實了,怎么辦?
好解決,我們可以使用 helpers.bulk() 批量更新方法。
首先引入這個模塊:
from elasticsearch import helpers
假設(shè)我們系統(tǒng)里現(xiàn)在有 id 為 21,23,24 的幾條數(shù)據(jù),還是在 exam 這個索引下,我們來構(gòu)造幾條需要更新的數(shù)據(jù)來操作:
action_1 = { "_op_type": "update", "_index": "exam", "_id": 21, "doc": {"age": 19, "name": "令狐沖", "address": "華山派"}, } action_2 = { "_op_type": "update", "_index": "exam", "_id": 23, "doc": {"age": 20, "name": "楊過", "address": "終南山"}, } action_3 = { "_op_type": "update", "_index": "exam", "_id": 24, "doc": {"age": 21, "name": "張無忌", "address": "武當(dāng)"}, } action_list = [action_1, action_2, action_3] helpers.bulk(conn, actions=action_list)
對于每一條需要更新的數(shù)據(jù),有這幾個參數(shù):
_op_type:如果是更新操作,其值則是 update
_index:表示需要更新的數(shù)據(jù)所在的索引,這里是 exam
_id:表示這條需要更新的數(shù)據(jù)的 id
doc:是一個 dict 數(shù)據(jù),其下包含了需要更新的字段及其對應(yīng)的值
至此,一條需要更新的數(shù)據(jù)的結(jié)構(gòu)就構(gòu)造完畢了。
然后對于 helpers.bulk() 函數(shù),接收的第一個參數(shù)為 es 連接,actions 參數(shù)是一個列表,其內(nèi)容就是我們前面構(gòu)造的數(shù)據(jù)的集合。
然后執(zhí)行這個操作就可以發(fā)現(xiàn) es 中對應(yīng)的值已經(jīng)更改了。
5、UpdateByQuery()
UpdateByQuery() 函數(shù)來源于 elasticsearch_dsl 模塊,它的使用和 Search() 方法差不多,都是通過 using 和 index 參數(shù)來獲取 es 連接和索引:
from elasticsearch_dsl import connections from elasticsearch_dsl import UpdateByQuery from elasticsearch_dsl import Q as ES_Q connections.configure( default={"hosts": "localhost:9200"}, ) ubq = UpdateByQuery(using="default", index="exam")
使用這個方法更新數(shù)據(jù)的具體語法和 update_by_query 差不多,都是通過 script 的方式來操作,以下是一個簡單示例:
ubq = UpdateByQuery(using="default", index="exam") q1 = ES_Q("term", name="郭靖") ubq = ubq.query(q1) ubq = ubq.script( source="ctx._source.address=params.address", params={ "address": "襄陽城" } ) ubq.execute()
與 Search() 函數(shù)一樣,都需要通過 execute() 函數(shù)來向 es 提交數(shù)據(jù)。
以上就是Python連接es之es更新操作示例詳解的詳細(xì)內(nèi)容,更多關(guān)于Python連接es更新的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python學(xué)習(xí)筆記整理3之輸入輸出、python eval函數(shù)
這篇文章主要介紹了Python學(xué)習(xí)筆記整理3之輸入輸出、python eval函數(shù)的相關(guān)資料,需要的朋友可以參考下2015-12-12python 實現(xiàn)矩陣上下/左右翻轉(zhuǎn),轉(zhuǎn)置的示例
今天小編就為大家分享一篇python 實現(xiàn)矩陣上下/左右翻轉(zhuǎn),轉(zhuǎn)置的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01Python無權(quán)點文件轉(zhuǎn)化成鄰接矩陣方式
這篇文章主要介紹了Python無權(quán)點文件轉(zhuǎn)化成鄰接矩陣方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12