2017-04-11 122 views
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 

回答

0

提供的代碼示例是不獨立的,所以我無法測試它。

如果我沒有正確地介紹您,請嘗試將ctx._source.Value設置爲NewValue

如果您只是用"inline": "ctx._source.Value={}".format(NewValue),替換"inline": "ctx._source.Value= NewValue",,會發生什麼情況?

+0

謝謝。這正是我需要的。現在你的建議代碼工作正常。 – AhmyOhlin