使用Python请求查询ElasticSearch

时间:2017-05-18 16:53:34

标签: python json elasticsearch python-requests

,当我尝试使用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

1 个答案:

答案 0 :(得分:0)

我认为你不需要在python版本中删除星*(删除斜杠):

elastic_url = 'https://localhost:9200/logstash-kafka-wga-blueid-*/_search'

但是,我建议使用python client library代替requests