Elasticsearch DSL update by scan?

302 views Asked by At

Is it possible to update with the scan() method from Python's elasticsearch-dsl library?

For example:

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
1

There are 1 answers

0
spitfiredd On

This can be done by using the elasticsearch client:

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}
    )