elasticsearch用于匹配_id的文档的更新字段

时间:2017-11-28 21:21:14

标签: python python-3.x elasticsearch python-3.6

以下是我的ES数据库中典型文档的样子:

{
  "_index": "test_index",
  "_type": "data_pt",
  "_id": "AWAEXNYdkjIRDAUZyu8d",
  "_version": 1,
  "_score": 1,
  "_source": {
    "state": "state_a",
    ...
  }
}

在我的代码中,我已经使用查询进行了搜索,并为他们存储了_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]

我尝试使用从state_id的匹配'state_a'来更新每个文档的'state_b'字段:

for _id in queried_id_list:    
    es.update(index='test_index',id='_id,doc_type='data_pt',
              body=update_query)

但是,这会增加一大笔开销,因为它会为每个文档调用update()

如果我尝试直接放置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.

如何调用单个update()来完成此操作?

1 个答案:

答案 0 :(得分:0)

找到解决方案。为了解决同一问题的其他人的利益:

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)