Elasticsearch通过查询“价值”更新* 1000使用python
问题描述:
我每次使用这个程序在Python中更新我的数据库的字段在elasticsearch上的字段。 现在我想多个1000的字段值,new_Value= Old_Value*1000
有人可以告诉我,我必须改变我的代码?Elasticsearch通过查询“价值”更新* 1000使用python
from elasticsearch import Elasticsearch
es = Elasticsearch()
index_to_search = 'testindex'
doc_type= 'trends'
Ergebnisse = es.search(index = index_to_search, doc_type = doc_type, size = 100,body = {"query":{"bool":{"must":[{"range":{"Value":{"gt":"0"}}}],"must_not":[],"should":[]}},"from":0,"size":1000,"sort":[]}
)
data = (Ergebnisse ['hits']['hits'])
print('Alle Suchergebnisse: ', data)
#print(data['Value'])
for Eintrag in data:
Sensor = Eintrag
sensortupel = {"Index": Sensor['_index'],"Value":Sensor['_source']['Value']}
OldValue= float(sensortupel['Value'])
print("\nOldValue:", OldValue)
NewValue = OldValue *1000
print('NewValue:',NewValue)
q = {
"query": {
"match_all": {}
},
"script": {
"inline": "ctx._source.Value= NewValue",
# write the name of field and with which new value should be updated
}
}
result = es.update_by_query(body=q, doc_type=doc_type, index=index_to_search)
print('Result is : ', result
编辑:
的错误是在这里:
"inline": "ctx._source.Value={}".format(NewValue), # change it in code above
答
提供的代码示例是不独立的,所以我无法测试它。
如果我没有正确地介绍您,请尝试将ctx._source.Value
设置为NewValue
。
如果您只是用"inline": "ctx._source.Value={}".format(NewValue),
替换"inline": "ctx._source.Value= NewValue",
,会发生什么情况?
谢谢。这正是我需要的。现在你的建议代码工作正常。 – AhmyOhlin