Elasticsearch の CRUD (python3)

## Create ##

```py:elastic_create.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#	elastic_create.py
#
#					Oct/05/2018
#
import	sys
import	json
import	requests
#
# -------------------------------------------------------------------------
def dict_append_proc(dict_aa,key,name,population,date_mod):
	dict_aa[key] = {'name':name,'population':population,'date_mod':date_mod}
#
	return dict_aa
#
#-------------------------------------------------------------------------
def data_prepare_proc():
	dict_aa = {}
	dict_aa = dict_append_proc(dict_aa,'t0921','宇都宮',48291,'2003-7-15')
	dict_aa = dict_append_proc(dict_aa,'t0922','小山',31759,'2003-5-18')
	dict_aa = dict_append_proc(dict_aa,'t0923','佐野',75214,'2003-6-9')
	dict_aa = dict_append_proc(dict_aa,'t0924','足利',29138,'2003-7-24')
	dict_aa = dict_append_proc(dict_aa,'t0925','日光',73612,'2003-8-11')
	dict_aa = dict_append_proc(dict_aa,'t0926','下野',82951,'2003-10-14')
	dict_aa = dict_append_proc(dict_aa,'t0927','さくら',94823,'2003-5-24')
	dict_aa = dict_append_proc(dict_aa,'t0928','矢板',57916,'2003-2-12')
	dict_aa = dict_append_proc(dict_aa,'t0929','真岡',64257,'2003-11-14')
	dict_aa = dict_append_proc(dict_aa,'t0930','栃木',82154,'2003-7-04')
	dict_aa = dict_append_proc(dict_aa,'t0931','大田原',72981,'2003-9-17')
	dict_aa = dict_append_proc(dict_aa,'t0932','鹿沼',28149,'2003-7-20')
	dict_aa = dict_append_proc(dict_aa,'t0933','那須塩原',72359,'2003-3-12')
	dict_aa = dict_append_proc (dict_aa,'t0934','那須烏山',61538,'2003-8-17')
#
	return	dict_aa
#
# -------------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
#
dict_aa = data_prepare_proc()
#
url_base = "http://localhost:9200/cities/_doc"
#
for key in dict_aa.keys():
	unit_aa = dict_aa[key]
	json_str = json.dumps(unit_aa)
	url = url_base + "/" + key
	print(url)
	headers = {'content-type': 'application/json'}
	rr=requests.put(url,data=json_str,headers=headers)
#	rr=requests.post(url,data=json_str,headers=headers)
	print(rr)
#	print(rr.headers)
#
sys.stderr.write("*** 終了 ***\n")
# -------------------------------------------------------------------------
```

## Read ##

```py:elastic_read.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#	elastic_read.py
#
#					Oct/05/2018
#
# ------------------------------------------------------------------
import	sys
import	json
import	requests
#
def dict_append_proc(dict_aa,key,name,population,date_mod):
	dict_aa[key] = {'name':name,'population':population,'date_mod':date_mod}
#
	return dict_aa
#
# ---------------------------------------------------------------
def dict_display_proc(dict_aa):
	for key in sorted(dict_aa.keys()):
		if ((key != '_id') and (key != '_rev')):
			unit = dict_aa[key]
			name = unit['name']
#			str_out = key+"\t"+ str(name)
			str_out = str(key) +"\t"+ str(name)
			str_out += "\t" + str(unit['population'])
			str_out += "\t" + str(unit['date_mod'])
			print(str_out)
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
args = {"q": "*","size": 100}
#
url = "http://localhost:9200/cities/_search"
#
rr=requests.get(url,args)
dict_data = json.loads(rr.text)
llx = len(dict_data['hits']['hits'])
sys.stderr.write("llx = %d\n" % llx)
dict_aa = {}
for it in range(llx):
	try:
		ddx = dict_data['hits']['hits'][it]
		key = ddx['_id']
		name = ddx['_source']['name']
		population = ddx['_source']['population']
		date_mod = ddx['_source']['date_mod']
		dict_aa = dict_append_proc(dict_aa,key,name,population,date_mod)
	except Exception as ee:
		sys.stderr.write("*** error ***\n")
		sys.stderr.write(str(ee) + "\n")
#
dict_display_proc(dict_aa)
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------
```

## Update ##

```py:elastic_update.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#	elastic_update.py
#
#					Oct/06/2018
#
# -------------------------------------------------------------------
import	sys
import	requests
import	json
import	datetime
#
# -------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
key_in = sys.argv[1]
population_in = int(sys.argv[2])
#
print("%s\t%d" % (key_in, population_in))
date_mod = datetime.date.today()
str_date_mod = '%s' % date_mod
#
url = "http://localhost:9200/cities/tochigi/_update/" + key_in
print(url)
#
args = {"script" : {
	"source": "ctx._source.population = params.population; ctx._source.date_mod = params.date_mod",
        "lang": "painless",
        "params" : {
            "population" : population_in,
            "date_mod" : str_date_mod
	}
	}
	}
#
print(args["script"]['params']['population'])
print(args["script"]['params']['date_mod'])
#
headers = {'content-type': 'application/json'}
#
json_str = json.dumps(args)
print(json_str)
rr=requests.post(url,data=json_str,headers=headers)
#
print(rr)
print(rr.text)
sys.stderr.write("*** 終了 ***\n")
# -------------------------------------------------------------------
```

実行スクリプト

```bash
./elastic_update.py t0923 5432800
```

## Delete ##

```py:elastic_delete.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#	elastic_delete.py
#
#					Oct/05/2018
#
# -------------------------------------------------------------------
import	sys
import	requests
#
# -------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
key_in = sys.argv[1]
print("%s" % key_in)
#
url = "http://localhost:9200/cities/_doc/" + key_in
rr=requests.delete(url)
#
sys.stderr.write("*** 終了 ***\n")
# -------------------------------------------------------------------
```

実行スクリプト

```bash
./elastic_delete.sh t0928
```

## 確認したバージョン ##

```text
$ python --version
Python 3.9.7
```