2015-02-10 49 views
3

我在elasticsearch中記錄了帶有特定時間戳的索引記錄。我試圖更新記錄使用下面的代碼(在python):在更新elasticsearch中的索引時發生RequestError

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') 
msg['text'] = 'New Message' 
es.update(index='idx', doc_type='dtype', id=msg['_id'], body=msg, timestamp=time, ttl='30d') 

而且我收到以下錯誤:

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

可能是什麼相同的理由嗎?

+0

嘿@mirchi,我希望我的回答對你有幫助。如果是這種情況,請不要忘記接受:) – Heschoon 2016-03-10 16:49:20

+0

謝謝@heschoon。我不知道如何接受。 – 2016-03-12 02:46:23

+0

接受一個問題,你必須點擊答案左邊的upvote/downvote箭頭下的綠色「V」。它「關閉」了問題,標誌着問題解決了。 – Heschoon 2016-03-13 19:34:34

回答

10

消息編號400意味着您有「錯誤的請求」。請求正文/網址不是預期的。

在這種情況下,這是由於您沒有在主體中使用腳本或文檔。查看Update API documentation瞭解更多信息。

下面的代碼可以解決你的問題:

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') 

通過周圍要通過一個文檔應答器改變的信息,你告訴ElasticSearch要與那些部分文件的替換值。

相關問題