Python3: Elastic Cloud の filters の使い方


こちらと同じことを python3 で記述しました。
Elastic Cloud の filters の使い方

elastic_filter.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#	elastic_filter.py
#
#					Apr/01/2022
#
# ------------------------------------------------------------------
import  sys
import  json
import  requests
# ------------------------------------------------------------------
def file_to_str_proc(file_in):
	str_out = ""
	try:
		fp_in = open(file_in,encoding='utf-8')
		str_out = fp_in.read()
		fp_in.close()
	except Exception as ee:
		sys.stderr.write("*** error *** file_to_str_proc ***\n")
		sys.stderr.write(str (ee))
#
	return	str_out
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
host="https://mar29-project.ent.asia-northeast1.gcp.cloud.es.io"
search_key="search-******"
name_engine="national-parks-demo"
json_query="query01.json"

url=host + "/api/as/v1/engines/" + name_engine + "/search"
headers = {"Content-Type": "application/json",
	"Authorization": "Bearer " + search_key}
json_str = file_to_str_proc(json_query)
#
#
rr=requests.post(url, headers=headers, data=json_str)
#print(rr.text)
#
dict_data = json.loads(rr.text)
#
for unit_aa in dict_data['results']:
	print(unit_aa['states']['raw'])
#
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------
query01.json
{
  "query": "",
  "filters" : { "states": [ "Texas", "Minnesota" ] }
}

実行結果

$ ./elastic_filter.py 
*** 開始 ***
['Texas']
['Minnesota']
['Texas']

次のバージョンで確認しました。

$ python --version
Python 3.9.7