Can UpdateByQuery be used to perform bulk update in elastic search.?

37 views Asked by At

I have a list of sequences e.g 1350, 1351, 1352, 1353, 1354, 1355, 1356, 1357, 1358, 1359, 1390 and a single sensor_pk and record_type. All the sequences documents share a common sensor_pk and record_type. I want to change the sensor_pk once instead of going through all the sequences and doing it individually. Here's what I have in python elasticsearch_dsl

    ubq = UpdateByQuery(using=es.es, index=es.ALIAS_NAME)

    query = Q('bool', filter=[
        Q('term', sensor_pk=sensor_pk),
        Q('terms', sequence=sequences),
        Q('term', type=record_type)
    ])

    ubb = ubq.query(query)
    ubb.script(source=f'ctx._source.sensor_pk = -{sensor_pk};')
    response = ubb.execute()

the response shows everything is okay but the sensor_pk doesn't get updated

{'took': 165, 'timed_out': False, 'total': 502, 'updated': 5...}

what might I be doing wrongly?

1

There are 1 answers

0
E_K On

I updated the code to use update_by_query

def update_records(sequences, record_type, sensor_pk, es):
    if not sequences:
        return

    body = {
        "script": {
            "source": "ctx._source.sensor_pk = params.sensor_pk",
            "lang": "painless",
            "params": {
            "sensor_pk": f"-{sensor_pk}"
            }
        },
        "query": {
            "bool": {
            "filter": [
                {"term": {"sensor_pk": sensor_pk}},
                {"terms": {"sequence": list(set(sequences))}}
            ]
            }
        }
    }

    response = es.es.update_by_query(index=es.ALIAS_NAME, body=body)