【问题标题】:Elasticsearch update by query "value" *1000 using python使用 python 通过查询“值”* 1000 更新 Elasticsearch
【发布时间】:2017-09-06 15:03:29
【问题描述】:

我每次在 python 中使用这个程序来更新我在 elasticsearch 上的数据库的字符串字段。 现在我想将 Field 的值与 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 

【问题讨论】:

    标签: python string elasticsearch elasticsearch-5


    【解决方案1】:

    提供的代码示例不是独立的,所以我无法测试它。

    如果我没有理解错,您正在尝试将ctx._source.Value 设置为NewValue

    如果您只是将"inline": "ctx._source.Value= NewValue", 替换为"inline": "ctx._source.Value={}".format(NewValue),,会发生什么情况?

    【讨论】:

    • 谢谢。这正是我所需要的。根据您的建议,代码现在运行良好。
    猜你喜欢
    • 2017-07-18
    • 2017-01-02
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    相关资源
    最近更新 更多