0
我每次使用這個程序在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
謝謝。這正是我需要的。現在你的建議代碼工作正常。 – AhmyOhlin