繁体   English   中英

使用python通过查询“值” * 1000更新Elasticsearch

[英]Elasticsearch update by query “value” *1000 using python

我每次在python中使用此程序时,都会在elasticsearch上更新数据库的字符串字段。 现在我想将Field的值new_Value= Old_Value*1000 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= NewValue",替换为"inline": "ctx._source.Value={}".format(NewValue),什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM