python - 使用python更新elasticsearch记录

标签 python elasticsearch

我可以连接到索引并从中选择第一条记录。但是我需要使用“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}})

请考虑避免执行此类请求。
查看有关update by query的文档。

关于python - 使用python更新elasticsearch记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44758036/

相关文章:

python - matplotlib 自定义图例中类别的副标题

python - Pandas 按多列分组和排序

ruby-on-rails - 多模型单索引方法 - 通过轮胎进行 Elasticsearch

elasticsearch-plugin - 如何将Elasticsearch Index导出到本地

elasticsearch - Spring Elastic Search 自定义字段名称

python - 将列表的第一个元素乘以给定数字并计算结果列表的累积乘积

python - 不支持 Pandas 逻辑回归混合类型?

python - 努力采取下一步如何存储我的数据

spring - 如何在应用程序中使用JPA的Spring Boot中使用ElasticSearch,我是否必须从表中获取记录并将其放入elasticsearch的索引中?

elasticsearch - 对两个字段进行汇总将返回其中一个的null