,当我尝试使用 python 请求查询 ElasticSearch 时,我不太能够检索结果。这是我的代码:
json_data = updateJson(sys.argv[1])
headers={'Accept': 'application/json', 'Content-type': 'application/json'}
elastic_url ='https://localhost:9200/logstash-kafka-wga-blueid-\*/_search'
query = json.dumps(json_data)
response = requests.get(elastic_url, data = query, auth=('xxx','xxx'), verify=False, headers = headers)
print response.text
我总是得到以下输出:
{"took":1,"timed_out":false,"_shards":{"total":0,"successful":0,"failed":0},"hits":{"total":0,"max_score":0.0,"hits":[]}}
但是如果我尝试使用以下 CURL 命令,我会得到正确的结果。上面代码中的json_data,从abc.json文件中读取json。上面的代码有什么不正确的地方吗?
curl -X GET -k -u xxx:xxx https://localhost:9200/logstash-kafka-wga-blueid-\*/_search -d @temporaryRundeckReport.json
这是我的 updateJson() 方法:
def updateJson(fileName):
with open(fileName, 'r') as file:
json_data = file.read()
json_data = json_data.replace('%X-FORWARDED-HOST%', sys.argv[2]);
json_data = json_data.replace('%TIME%', sys.argv[3]);
json_data = json_data.replace('%INTERVAL%', sys.argv[4]);
with open('temporaryRundeckReport.json', 'w+') as file:
os.chmod('temporaryRundeckReport.json',0o777)
file.write(json_data)
return json_data
最佳答案
我觉得你不需要逃星*
在 python 版本中(删除斜杠):
elastic_url = 'https://localhost:9200/logstash-kafka-wga-blueid-*/_search'
而且,我建议使用 python client library而不是
requests
.
关于python - 使用 Python 请求查询 ElasticSearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44053745/