[英]elasticsearch update fields for documents with matching _id
Here's how a typical document looks like in my ES database: 这是我的ES数据库中典型文档的外观:
{
"_index": "test_index",
"_type": "data_pt",
"_id": "AWAEXNYdkjIRDAUZyu8d",
"_version": 1,
"_score": 1,
"_source": {
"state": "state_a",
...
}
}
In my code, I have already searched with a query, and have stored a list of _id
for them: 在我的代码中,我已经搜索了一个查询,并为它们存储了
_id
列表:
query = {
...
{
'term': 'state_a'
},
...
}
results = es.search(index='test_index',_source=True,body=query)
hits = results['hits']['hits']
queried_id_list = [doc['_id'] for doc in hits]
I'm trying to update state
field of each documents with the matching _id
from 'state_a'
to 'state_b'
: 我正在尝试将具有匹配
_id
的每个文档的state
字段从'state_a'
为'state_b'
:
for _id in queried_id_list:
es.update(index='test_index',id='_id,doc_type='data_pt',
body=update_query)
However, this adds a ridiculous amount of overhead, since it's calling update()
for every single document. 但是,这会增加大量的开销,因为它会为每个文档调用
update()
。
If I try to put queried_id_list
directly: 如果我尝试直接放置
queried_id_list
:
>>> es.update(index=test_index', id=queried_id_list, doc_type='data_pt', body=update_query)
Traceback (most recent call last):
...
File "/Users/username/anaconda/lib/python3.6/site-packages/elasticsearch/client/utils.py", line 76, in _wrapped
return func(*args, params=params, **kwargs)
File "/Users/username/anaconda/lib/python3.6/site-packages/elasticsearch/client/__init__.py", line 526, in update
raise ValueError("Empty value passed for a required argument.")
ValueError: Empty value passed for a required argument.
How can I call a single update()
to accomplish this? 如何调用单个
update()
完成此操作?
Found a solution. 找到了解决方案。 For the interest of others who struggles the same issue:
为了其他在同一问题上苦苦挣扎的人的利益:
update_query = {
'script': {
'inline': 'ctx._source.state = "state_b"',
'lang': 'painless'
},
'query': {
'terms': {
'_id': queried_id_list
}
}
}
es.update_by_query(index='test_index', doc_type='data_pt', body=update_query)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.