python中的Elasticsearch更新文档
我正在尝试用elasticsearch python更新文档python中的Elasticsearch更新文档,python,
elasticsearch,updates,Python,
elasticsearch,Updates,我正在尝试用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
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;')
使用postman很容易,但我真的不明白为什么要用Elasticsearch(python)来更新文档。有人有其他想法需要帮助吗?因为helpers.bulk()方法中的第二个参数应该是一个iterable,请尝试将其包含在列表中 再看看您的代码,您似乎只希望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转换为dict时,我添加了
include\u meta=True
,它对我起了作用。你有什么错误吗?谢谢你的回答:我尝试过:es.update(index='macs',doc\u type='users',id='1',body=doc)
,我从be style not working检查你的id是否正确中得到了示例。Elasticsearch为所有文档分配一个随机id,因此如果您没有手动将其设置为“1”,则1不能是id。此外,您可能希望在es.update()部分中尝试“doc\u as\u upsert”=True设置。你也可以发布你的文档的外观或可能的映射问题是elasticsearch更新不接受unicode dict。是否有任何解决方法?当我输入类似以下内容时,看看你是否犯了与此问题相同的错误[link](),但是当我制作es.get(…)时,该文档不再工作(它是unicode dict)。我想问题在于unicode
obj.to_dict(include_meta=True)