【发布时间】:2020-08-08 20:06:45
【问题描述】:
我正在使用带有 Python 请求的 msearch 并收到以下错误:
msearch 请求必须以换行符终止 [\n]
我查看了许多其他相关的问题/答案,但它们要么使用 cURL、带有查询的文本文件,要么使用 Python es API。我需要使用请求,并且我的查询生成为列表/字典。
url = <host>+"/" + 'books/_msearch' # books is the index
region = <region>
service = 'es'
credentials = boto3.Session().get_credentials()
awsauth = AWS4Auth(credentials.access_key, credentials.secret_key, region, service, session_token=credentials.token)
payload = [{},
{"query": {"bool": {"filter": [{"terms": {"user_id": [504401]}}]}}, "size": 0},
{},
{"query": {"bool": {"filter": [{"terms": {"user_id": [504401]}}]}}, "size": 0}
]
r = requests.post(url, auth=awsauth, json=payload)
query_results = json.loads(r.text)
我也试过了:
payload = json.dumps(payload) + "\n"
同样的错误。
我也试过了:
r = ""
for d in payload:
r += json.dumps(d) + "\n"
r = requests.post(url, auth=awsauth, json=r)
同样的错误。
【问题讨论】:
-
你可以试试data arg而不是json吗?
-
这给了我另一个错误:缺少 Content-Type 标头
-
将参数
headers设置为headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}。另外,您可以将 POST 正文更改为单个字典而不是列表吗? -
做到了!我需要添加一个标头,将 json 更改为数据,还需要添加“for d in search_arr:”位以添加 \n
-
不错!祝你好运!
标签: python elasticsearch