Elasticsearch DSL 通过扫描更新?

问题描述

是否可以使用 Python 的 elasticsearch-dsl 库中的 scan() 方法进行更新?

例如:

search = Search(index=INDEX).query(query).params(request_timeout=6000)

print('Scanning Query and updating.')
for hit in search.scan():
    _id = hit.Meta.id
    # sql query to get the record from th database
    record = get_record_by_id(_id)
    hit.column1 = record['column1']
    hit.column2 = record['column2']
    # not sure what use to update here

解决方法

这可以通过使用 elasticsearch 客户端来完成:

for hit in search.scan():
    _id = hit.meta.id
    print(_id)

    # get data from database
    record = get_record_by_id(_id)

    body = dict(record)
    body.pop('_id')

    # update the elastichsearch client
    elastic_client.update(
        index=INDEX,id=_id,body={"doc": body}
    )

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...