2017-04-11 9 views
0

私はelasticsearchで私のデータベースの文字列のフィールドを更新するためにこのプログラムをPythonで毎回使用します。 今、私は複数のフィールドの値を1000としたいと思っていますnew_Value= Old_Value*1000 誰かが私のコードで何を変えなければならないのですか?クエリ "値"による検索1000 * 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 

EDIT:

エラーがここにあった:提供

"inline": "ctx._source.Value={}".format(NewValue), # change it in code above 

答えて

0

のコード例では、自己完結型ではないので、私はそれをテストすることができませんでした。

正しく理解できない場合は、ctx._source.ValueNewValueに設定しようとしています。

"inline": "ctx._source.Value= NewValue","inline": "ctx._source.Value={}".format(NewValue),に置き換えた場合はどうなりますか?

+0

ありがとうございます。それはまさに私が必要なものです。コードは今あなたのアドバイスとうまく動作しています。 – AhmyOhlin

関連する問題