簡體   English   中英

使用python更新elasticsearch記錄

[英]update elasticsearch record using python

我可以連接到索引並從中選擇第一條記錄。 但是我需要使用“ newval”參數更新記錄。 並且更新方法失敗並顯示錯誤:

AttributeError: 'NoneType' object has no attribute 'copy'

這是代碼

import elasticsearch
from elasticsearch import helpers
es = elasticsearch.Elasticsearch('http://172.31.73.228:9200')

myquery={"query": {"match_all": {}}}
res = es.search(index="packetbeat-2017.06.12", body=myquery)

for i in range(2):
    print (res['hits']['hits'][i] )

返回的記錄我需要更新:

{'_index': 'packetbeat-2017.06.12', '_type': 'flow', '_id': 'AVyZmvuW4pXRFgxKGB7c', '_score': 1.0, '_source': {'@timestamp': '2017-06-12T00:01:30.000Z', 'beat': {'hostname': 'ip-172-31-73-228', 'name': 'ip-172-31-73-228', 'version': '5.4.1'}, 'dest': {'ip': '172.31.73.228', 'port': 9200, 'stats': {'net_bytes_total': 10015, 'net_packets_total': 46}}, 'final': True, 'flow_id': 'EAT/////AP//////CP8AAAFzfHHGrB9J5JLr8CM', 'last_time': '2017-06-12T00:00:30.732Z', 'source': {'ip': '115.124.113.198', 'port': 60306, 'stats': {'net_bytes_total': 141066, 'net_packets_total': 81}}, 'start_time': '2017-06-12T00:00:30.732Z', 'transport': 'tcp', 'type': 'flow'}}

我的更新方法:

mybody={'doc': {'newval': 24}}

es.update('packetbeat-2017.06.12', 'flow', 'AVyZmvuW4pXRFgxKGB7c', body=mybody, params=None)

我無法在上述記錄中添加“ newval”變量。

以下代碼段可以正常工作:

import elasticsearch
es = elasticsearch.Elasticsearch('http://localhost:9200')

res = es.search(index="packetbeat-2017.06.12", body={"query": {"match_all": {}}})

for single in res['hits']['hits']:
    print (single)

es.update('packetbeat-2017.06.12', 'flow', 'AVyZmvuW4pXRFgxKGB7c', body={'doc': {'newval': 24}})

請考慮避免執行此類請求。 檢查有關查詢更新的文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM