0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Python3: Elastic Cloud の filters の使い方

Last updated at Posted at 2022-04-01

こちらと同じことを 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=sys.argv[1]
json_query=sys.argv[2]

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 national-parks-demo query01.json
*** 開始 ***
['Texas']
['Minnesota']
['Texas']
*** 終了 ***

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

$ python --version
Python 3.9.7
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?