Elasticsearch与Python请求:MSSearch请求必须以换行符终止
我正在对Python请求使用msearch,出现以下错误: msearch请求必须由换行符终止[\n] 我已经看过许多其他相关的问题/答案,但它们要么使用cURL,一个带有查询的文本文件,要么使用Python es API。我需要使用请求,我的查询生成为列表/字典Elasticsearch与Python请求:MSSearch请求必须以换行符终止,python,
elasticsearch,Python,
elasticsearch,我正在对Python请求使用msearch,出现以下错误: msearch请求必须由换行符终止[\n] 我已经看过许多其他相关的问题/答案,但它们要么使用cURL,一个带有查询的文本文件,要么使用Python es API。我需要使用请求,我的查询生成为列表/字典 url = <host>+"/" + 'books/_msearch' # books is the index region = <region> service = 'es' cre
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)
同样的错误。这里有一个想法:
r = requests.post(url,
data="\n".join(
[json.dumps(elem) for elem in payload]
)+"\n",
headers={'Content-Type': 'application/x-ndjson'})
Accept:text/plain
是一个。感谢@wholevinski的回答(在问题评论中)。我需要将json更改为数据并添加标题。此外,我还需要执行\n循环
payload = [{},
{"query": {"bool": {"filter": [{"terms": {"user_id": [504401]}}]}}, "size": 0},
{},
{"query": {"bool": {"filter": [{"terms": {"user_id": [504401]}}]}}, "size": 0}
] # same as in question
data_as_str = ""
for d in payload:
data_as_str += json.dumps(d) + "\n"
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
# "Accept: text/plain" may not be necessary
r = requests.post(url, headers=headers, auth=awsauth, data=data_as_str)
query_results = json.loads(r.text)
您可以尝试使用data arg而不是json吗?这给了我另一个错误:Content-Type header是missingSet参数
headers
到headers={'Content-Type':'application/json','Accept':'text/plain'}
。另外,你能把你的帖子正文改成一张单子而不是一张单子吗!我需要添加一个标题,将json更改为数据,还需要添加“for d in search\u arr:”位!祝你好运!谢谢这也应该行得通。我写下了@wholevinski提供的答案,因为我就是这么做的。酷。仅仅因为它有效并不意味着它是正确的