'Querying ElasticSearch with Python Requests
,I am not quite able to retrieve the results when I try to query ElasticSearch using python requests. Here is my code:
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
I always get the following output:
{"took":1,"timed_out":false,"_shards":{"total":0,"successful":0,"failed":0},"hits":{"total":0,"max_score":0.0,"hits":[]}}
But if I try to use the following CURL command, I get the right results. In the above code json_data, reads the json from abc.json file. Is there something incorrect in the above code?
curl -X GET -k -u xxx:xxx https://localhost:9200/logstash-kafka-wga-blueid-\*/_search -d @temporaryRundeckReport.json
Here is my updateJson() method:
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
Solution 1:[1]
I think you don't need to escape star * in python version (remove slash):
elastic_url = 'https://localhost:9200/logstash-kafka-wga-blueid-*/_search'
But also, I would recommend using the python client library instead of requests.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Boris Serebrov |
