Python-automated bulk request for Elasticsearch not working “must be terminated by a newline”

孤者浪人 提交于 2021-01-28 05:16:28

问题


I am trying to automate a bulk request for Elasticsearch via Python.

Therefore, i am preparing the data for the request body as follows (saved in a list as separate rows):

data = [{"index":{"_id": ID}}, {"tag": {"input": [tag], "weight":count}}]

Then i will use requests to do the Api call:

r = requests.put(endpoint, json = data, auth = auth)

This is giving me the Error: b'{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"The bulk request must be terminated by a newline [\\n]"}],"type":"illegal_argument_exception","reason":"The bulk request must be terminated by a newline [\\n]"},"status":400}'

I know that i need to put a newline at the end of the request, and there lies my problem: How can i append a newline to that given data structure? I tried to append '\n' to my list at the end but that didnt work out.

Thank you guys!


回答1:


The payload's content type must be ndjson and the index attribute needs to include a name. Here's a working snippet:

import requests
import json

endpoint = 'http://localhost:9200/_bulk'


#                  vvvvvv
data = [{"index": {"_index": "123", "_id": 123}},
        {"tag": {"input": ['tag'], "weight":10}}]


#         vvv                                              vvv
payload = '\n'.join([json.dumps(line) for line in data]) + '\n'

r = requests.put(endpoint,
                 # `data` instead of `json`!
                 data=payload,
                 headers={           
                     # it's a requirement
                     'Content-Type': 'application/x-ndjson'
                 })

print(r.json())

P.S.: You may want to consider the bulk helper in the official py client.



来源:https://stackoverflow.com/questions/61866140/python-automated-bulk-request-for-elasticsearch-not-working-must-be-terminated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!