使用请求将文档发布到Elasticsearch索引?

628mspwn  于 2023-02-21  发布在  ElasticSearch
关注(0)|答案(2)|浏览(140)

我在这方面有困难:下面所有的返回值都是405,我不知道为什么:

data = {
        "xxx" : "status",
        "value" : "incomplete"
    }
    headers = {'Content-type': 'application/json'}
    response = requests.post(f'http://localhost:9200/my_index', data=json.dumps(data)) 
    response = requests.post(f'http://localhost:9200/my_index', data=json.dumps(data), headers=headers) 
    response = requests.post(f'http://localhost:9200/my_index', json=data)
    response = requests.post(f'http://localhost:9200/my_index', json=data, headers=headers)

注意:我也尝试使用“http://localhost:9200/my_index/xxxx_docxx *”来代替:他们都得到406。
ES肯定在运行,我可以在CLI使用Postman和curl
requests.put执行上述尝试(以“.../_doc/1”结尾),将获得406或401,但以下情况除外:

response = requests.put(f'http://localhost:9200/my_index/_doc/2', data=json.dumps(data), headers=headers)

...其工作(201或200取决于索引是否已经存在)。
操作系统为Linux Mint 20,ES为7.16.3。

yi0zb3m4

yi0zb3m41#

根据文档,您必须在索引名称后放置_doc:

PUT /<target>/_doc/<_id>

POST /<target>/_doc/

您可以选择Elastic是否为您创建id,也可以指定一个id。
Index API

iezvtpos

iezvtpos2#

尝试在头中添加“accept”:“application/json”。

相关问题