使用Python请求查询ElasticSearch

p4tfgftt  于 2022-11-02  发布在  ElasticSearch
关注(0)|答案(1)|浏览(163)

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

xkrw2x1b1#

我认为你不需要在python版本中转义星星*(去掉斜杠):

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

但是,我也建议使用python client library而不是requests

相关问题