我正在尝试在 elasticsearch python 中更新文档,
from elasticsearch import Elasticsearch
from datetime import datetime
from scapy.all import *
import json
# es = Elasticsearch()
es = Elasticsearch(['http://localhost:9200'])
doc = getDoc("1")
print doc
if doc != None:
doc['_op_type'] = 'update'
doc['_source']['macList'].append('new')
helpers.bulk(es, doc, stats_only=False)
但这行不通。有人可以告诉我我做错了什么吗?谢谢
这是错误:
Traceback (most recent call last):
File "./req.py", line 48, in <module>
helpers.bulk(es, doc, stats_only=False)
File "/usr/local/lib/python2.7/dist-packages/elasticsearch/helpers/__init__.py", line 188, in bulk
for ok, item in streaming_bulk(client, actions, **kwargs):
File "/usr/local/lib/python2.7/dist-packages/elasticsearch/helpers/__init__.py", line 160, in streaming_bulk
for result in _process_bulk_chunk(client, bulk_actions, raise_on_exception, raise_on_error, **kwargs):
File "/usr/local/lib/python2.7/dist-packages/elasticsearch/helpers/__init__.py", line 89, in _process_bulk_chunk
raise e
elasticsearch.exceptions.RequestError: TransportError(400, u'action_request_validation_exception', u'Validation Failed: 1: index is missing;2: type is missing;3: index is missing;4: type is missing;5: index is missing;6: type is missing;7: index is missing;8: type is missing;9: index is missing;10: type is missing;11: index is missing;12: type is missing;13: index is missing;14: type is missing;')
更新我试过这个:es.update(index='macs', doc_type='users', id="1", body=doc)
,我遇到了这个错误:回溯(最近一次调用):
File "./req.py", line 50, in <module>
es.update(index='macs', doc_type='users', id="1", body=doc)
File "/usr/local/lib/python2.7/dist-packages/elasticsearch/client/utils.py", line 69, in _wrapped
return func(*args, params=params, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/elasticsearch/client/__init__.py", line 460, in update
doc_type, id, '_update'), params=params, body=body)
File "/usr/local/lib/python2.7/dist-packages/elasticsearch/transport.py", line 329, in perform_request
status, headers, data = connection.perform_request(method, url, params, body, ignore=ignore, timeout=timeout)
File "/usr/local/lib/python2.7/dist-packages/elasticsearch/connection/http_urllib3.py", line 109, in perform_request
self._raise_error(response.status, raw_data)
File "/usr/local/lib/python2.7/dist-packages/elasticsearch/connection/base.py", line 108, in _raise_error
raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
elasticsearch.exceptions.RequestError: TransportError(400, u'action_request_validation_exception', u'Validation Failed: 1: script or doc is missing;').
邮递员很容易,但我真的不明白为什么 Elasticsearch(python)更新文档如此之大。 有人有其他想法可以帮忙吗?
由于 helpers.bulk() 方法中的第二个参数应该是可迭代的,请尝试将其包含在列表中。
同时查看您的代码,您似乎只期望来自 getDoc() 函数的一个文档。所以如果你使用简单的es.update()方法而不是使用helpers.bulk()会更好
这个解决方案确实对我有用。
doc= ess.get(...)
# modify my doc and ...
coll = Elasticsearch()
coll.update(index='stories-test',doc_type='news',id=hit.meta.id,
body= {"doc": doc['_source']} )
(如果有人遇到类似的问题,)我收到上面提到的第一个错误:
elasticsearch.exceptions.RequestError: TransportError(400,
'action_request_validation_exception', 'Validation Failed:
1: index is missing;2: type is missing;
以下内容为我修复了它:
obj.to_dict(include_meta=True)
将 obj 转换为字典时,我添加了include_meta=True
,它对我有用。