我每次在 Python 中使用这个程序来更新我在 Elasticsearch 上的数据库中的字符串字段。现在我想将 Field 的 a 值乘以 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
提供的代码示例不是独立的,所以我无法测试它。
如果我正确地解开你,你正在尝试将ctx._source.Value
设置为NewValue
.
如果只是用"inline": "ctx._source.Value={}".format(NewValue),
替换"inline": "ctx._source.Value= NewValue",
会发生什么?