UpdateByQuery是否可以在ElasticSearch中执行批量更新?

pinkon5k  于 12个月前  发布在  ElasticSearch
关注(0)|答案(1)|浏览(160)

我有一个序列列表,例如1350, 1351, 1352, 1353, 1354, 1355, 1356, 1357, 1358, 1359, 1390和一个sensor_pkrecord_type。所有的序列文档都共享一个共同的sensor_pkrecord_type。我想改变sensor_pk一次,而不是遍历所有的序列并单独进行。下面是我在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()

字符串
响应显示一切正常,但sensor_pk未更新
{'took': 165, 'timed_out': False, 'total': 502, 'updated': 5...}
我做错了什么

xnifntxz

xnifntxz1#

我更新了代码以使用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)

字符串

相关问题