使用python脚本索引到elasticsearch 6.1
我尝试使用python 2.7脚本对elasticsearch进行索引,如下所示:使用python脚本索引到elasticsearch 6.1,python,
elasticsearch,Python,
elasticsearch,我尝试使用python 2.7脚本对elasticsearch进行索引,如下所示: from __future__ import print_function import urllib, urllib2 #FORMDATA is a json format string that has been taken from a file ELASTIC_URL = 'http://1.2.3.9:9200/indexname/entry/ req = urllib2.Request(ELASTI
from __future__ import print_function
import urllib, urllib2
#FORMDATA is a json format string that has been taken from a file
ELASTIC_URL = 'http://1.2.3.9:9200/indexname/entry/
req = urllib2.Request(ELASTIC_URL)
req.add_header('contentType', 'application/x-www-form-urlencoded')
response = urllib2.urlopen(req, FORMDATA, timeout=4).read()
print(response)
我一直收到错误HTTP错误406:不可接受:HTTPError
我还尝试过用urllib.quote(FORMDATA)格式化数据,但得到了相同的错误。数据不是字典,而是转换为json时是多维的字符串
我认为这与req头需要指定contentType为正确格式这一事实有关,但我很难确定这是什么。我设法在elasticsearch 5.x上进行了导入,但现在在3.x上它似乎不起作用
任何想法???几乎所有elasticsearch API调用都在标题中使用
内容类型:application/json
——这应该是您需要的
另外请注意,如果您要提交数据,则需要采用
POST
(或PUT
的形式,如果生成您自己的id),而不是GET
请求:谢谢您的回复。这就是所需的内容类型,它现在正在工作。请注意,如果您指定要发送的数据,urllib2.urlopen()方法将作为默认值发送post请求。