我每次在python中使用这个程序来更新elasticsearch上我的数据库的字符串字段。
现在我想要将Field的值加倍1000,new_Value= Old_Value*1000
谁能告诉我,我的代码需要改变什么?
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
答案 0 :(得分:0)
提供的代码示例不是自包含的,因此我无法对其进行测试。
如果我正确地告诉您,您正试图将ctx._source.Value
设为NewValue
。
如果您只是将"inline": "ctx._source.Value= NewValue",
替换为"inline": "ctx._source.Value={}".format(NewValue),
会怎样?