[英]Elasticsearch search using python
使用 curl 查詢。
curl -XGET http://localhost:9200/users/_search?pretty=true -H 'Content-Type: application/json' -d '
{
"from" : 0, "size" : 5,
"query" : {
"query_string" : {
"query" : "Port"
}
}
}
'
我試圖在 python 請求中做同樣的事情:
url = f'{URL}/{INDEX}/_search/'
es_query = {
"query" : {
"query_string" : {
"query" : "Port"
}
}
}
res = requests.get(url, data=es_query)
print(res.json())
執行此操作時出現錯誤。
`{'error': 'Content-Type header [application/x-www-form-urlencoded] is not supported', 'status': 406}`
請看看可能是什么問題
您可以使用curlconverter.com輕松地將 cURL 命令轉換為 Python 請求。 這是 cURL 命令的 output :
import requests
params = {
'pretty': 'true',
}
json_data = {
'from': 0,
'size': 5,
'query': {
'query_string': {
'query': 'Port',
},
},
}
response = requests.get('http://localhost:9200/users/_search', params=params, headers=headers, json=json_data)
你的 URL 是錯誤的
url = f'{URL}/{INDEX}/_bulk/'
應該
url = f'{URL}/{INDEX}/_search/'
您還需要指定以下請求 header
Content-type: application/json
像這樣:
res = requests.get(url, json=es_query, headers={"Content-type": "application/json"})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.