Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.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 6.1_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 6.1

使用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

我尝试使用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(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请求。