Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在elasticsearch中更新索引时发生RequestError_Python_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch - Fatal编程技术网 elasticsearch,Python,elasticsearch" /> elasticsearch,Python,elasticsearch" />

Python 在elasticsearch中更新索引时发生RequestError

Python 在elasticsearch中更新索引时发生RequestError,python,elasticsearch,Python,elasticsearch,我在elasticsearch中索引了一条记录,带有特定的时间戳。我正在尝试使用以下代码(python)更新记录: 我得到了以下错误: RequestError: TransportError(400, u'ActionRequestValidationException[Validation Failed: 1: script or doc is missing;]') 原因可能是什么?消息编号400表示您有一个“错误的请求”。请求正文/URL不是预期的内容 在这种情况下,这是因为您没有在正

我在elasticsearch中索引了一条记录,带有特定的时间戳。我正在尝试使用以下代码(python)更新记录:

我得到了以下错误:

RequestError: TransportError(400, u'ActionRequestValidationException[Validation Failed: 1: script or doc is missing;]')

原因可能是什么?

消息编号400表示您有一个“错误的请求”。请求正文/URL不是预期的内容

在这种情况下,这是因为您没有在正文中使用脚本或文档。有关更多信息,请查看

以下代码解决了您的问题:

from elasticsearch import Elasticsearch
from datetime import datetime
import pytz

es = Elasticsearch()
time = datetime.utcnow().replace(tzinfo=pytz.utc)
msg = {'_id': 1, 'text': 'Hello World'}
es.index(index='idx', doc_type='dtype', id=msg['_id'], body=msg, timestamp=time, ttl='30d')
msg2 = '''{"doc": {"text": "New Message"}}'''
es.update(index='idx', doc_type='dtype', id=msg['_id'], body=msg2, timestamp=time, ttl='30d')

通过用
doc
标记包围您想要更改的信息,您告诉ElasticSearch您想要用部分文档的值替换这些值。

Hey@mirchi,我希望我的回答对您有所帮助。如果是这样,请不要忘记接受:)谢谢@heschoon。我不知道如何接受相同的答案。要接受问题,您必须单击答案左侧向上/向下投票箭头下的大绿色“V”。它“关闭”问题,将问题标记为已解决。
from elasticsearch import Elasticsearch
from datetime import datetime
import pytz

es = Elasticsearch()
time = datetime.utcnow().replace(tzinfo=pytz.utc)
msg = {'_id': 1, 'text': 'Hello World'}
es.index(index='idx', doc_type='dtype', id=msg['_id'], body=msg, timestamp=time, ttl='30d')
msg2 = '''{"doc": {"text": "New Message"}}'''
es.update(index='idx', doc_type='dtype', id=msg['_id'], body=msg2, timestamp=time, ttl='30d')