我需要在python腳本中構建Elasticsearch查詢。在Elasticsearch查詢中,我需要傳遞外部python變量值進行搜索。將Python變量值傳遞給Elasticsearch查詢
下面是我試過自己的python腳本。但它不需要外部變量參數,當我手動設置查詢內的值時,它工作正常。
import master
mst = master.Master()
sourceip = "192.168.1.1" // External variable and its value
get_query_result = mst.build_query('{"query": {"bool": {"must": [{"match": { "source": "server_one" }},{"match": {"srcip": sourceip }}],"filter":[ {"range" : {"timestamp" :{"gte": "now-1d", "lte": "now"}}}]}}}')
total_query_result = get_query_result['hits']['total']
print(total_query_result)
當我在sourcesearch查詢中放入sourceip變量時,它不會接受變量值並生成結果。它提出了一個錯誤
但是當我在Elasticsearch查詢內手動設置IP地址值時,腳本返回結果成功。
如何在Elasticsearch查詢中傳遞python變量。