0

i use everytime this programm in python to update string's fields of my database on elasticsearch. now i want to multiple the a value of Field with 1000, new_Value= Old_Value*1000 can someone tell me, what i have to change in my code?

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:

the error was here:

"inline": "ctx._source.Value={}".format(NewValue), # change it in code above 
AhmyOhlin
  • 519
  • 4
  • 8
  • 18

1 Answers1

0

The code example provided is not self contained, so I could not test it.

If I unterstand you correctly, you are trying to set ctx._source.Value to NewValue.

What happens if you just replace "inline": "ctx._source.Value= NewValue", with "inline": "ctx._source.Value={}".format(NewValue),?

mab
  • 2,658
  • 26
  • 36