Elasticsearch の CRUD (Ruby)

ライブラリーは faraday を使います。

Arch Linux でのインストール方法

>```bash
sudo pacman -S ruby-faraday
```

Create

```ruby:elastic_create.rb
#! /usr/bin/ruby
# -*- encoding: utf-8 -*-
#
#	ruby/create/elastic_create.rb
#
#					Oct/07/2018
#
# ---------------------------------------------------------------------
require 'faraday'
require 'json'
#
# ---------------------------------------------------------------------
def dict_append_proc (dict_aa,id,name,population,date_mod)
	unit = {}
	unit['name'] = name
	unit['population'] = population
	unit['date_mod'] = date_mod
	key = id.to_s
	dict_aa[key] = unit
	return dict_aa
end
# ---------------------------------------------------------------------
def data_prepare_proc ()
dict_aa = {}
dict_aa=dict_append_proc(dict_aa,"t0921","宇都宮",93428,"2006-5-25")
dict_aa=dict_append_proc(dict_aa,"t0922","小山",47136,"2006-9-27")
dict_aa=dict_append_proc(dict_aa,"t0923","佐野",14725,"2006-3-8")
dict_aa=dict_append_proc(dict_aa,"t0924","足利",27658,"2006-1-22")
dict_aa=dict_append_proc(dict_aa,"t0925","日光",87429,"2006-5-11")
dict_aa=dict_append_proc(dict_aa,"t0926","下野",25916,"2006-4-17")
dict_aa=dict_append_proc(dict_aa,"t0927","さくら",36927,"2006-4-18")
dict_aa=dict_append_proc(dict_aa,"t0928","矢板",71983,"2006-8-15")
dict_aa=dict_append_proc(dict_aa,"t0929","真岡",83915,"2006-10-11")
dict_aa=dict_append_proc(dict_aa,"t0930","栃木",45126,"2006-7-12")
dict_aa=dict_append_proc(dict_aa,"t0931","大田原",58762,"2006-8-22")
dict_aa=dict_append_proc(dict_aa,"t0932","鹿沼",81394,"2006-9-17")
dict_aa=dict_append_proc(dict_aa,"t0933","那須塩原",32547,"2006-12-29")
dict_aa=dict_append_proc(dict_aa,"t0934","那須烏山",69812,"2006-10-30")

return dict_aa
end
# ---------------------------------------------------------------------
STDERR.puts	"*** 開始 ***"
#
dict_aa=data_prepare_proc()
#
Url_base = "http://localhost:9200/cities/tochigi"
#
dict_aa.sort.each {|key, value|
	puts key
	url = Url_base + "/" + key
	con = Faraday.new
	res = con.put do |req|
		req.url url
		req.headers['Content-Type'] = 'application/json'
		req.body = JSON.pretty_generate(value)
	end
	}
#
STDERR.puts	"*** 終了 ***"
#
# ---------------------------------------------------------------------
```

Read

```rb:elastic_read.rb
#! /usr/bin/ruby
# -*- encoding: utf-8 -*-
#
#	elastic_read.rb
#
#					Oct/07/2018
#
# ---------------------------------------------------------------------
require 'faraday'
require 'json'
#
# ---------------------------------------------------------------------
def dict_display_proc (dict_aa)
	dict_aa.sort.each {|key, value|
		if (key.to_s != '_id') && (key.to_s != '_rev')
			print(key,"\t",value['name'],"\t")
			print(value['population'],"\t")
			print(value['date_mod'],"\n")
		end
	}
end
# ---------------------------------------------------------------------
STDERR.puts	"*** 開始 ***"
#
URL="http://localhost:9200/cities/tochigi/_search?q=*&size=100"
#
res = Faraday.get URL

json_str = res.body

dict_data=JSON.parse(json_str)
#
list_aa = dict_data['hits']['hits']
#
dict_aa = {}
list_aa.each {|unit|
	key = unit['_id']
	ss = unit['_source']
	dict_aa[key] = ss
	}
#
dict_display_proc(dict_aa)
#
STDERR.puts	"*** 終了 ***"
# ---------------------------------------------------------------------
```

Update

```rb:elastic_update.rb
#! /usr/bin/ruby
# -*- encoding: utf-8 -*-
#
#	ruby/update/elastic_update.rb
#
#					Oct/07/2018
#
# ---------------------------------------------------------------------
require 'faraday'
require 'json'
require 'date'
#
# ---------------------------------------------------------------------
STDERR.puts	"*** 開始 ***"
key_in = ARGV[0]
population_in = ARGV[1].to_i
#
puts key_in,population_in
#
URL = "http://localhost:9200/cities/tochigi/" + key_in + "/_update"
puts URL
#
str_date_mod = Date.today
#
args = {}
args['script'] = {}
args['script']['source'] = "ctx._source.population = params.population; ctx._source.date_mod = params.date_mod"

args['script']['lang'] = "painless"
args['script']['params'] = {}
args['script']['params']['population'] = population_in
args['script']['params']['date_mod'] = str_date_mod
#
puts args["script"]['params']['population']
puts args["script"]['params']['date_mod']
#
con = Faraday.new 
res = con.post do |req|
    req.url URL
    req.headers['Content-Type'] = 'application/json'
    req.body = JSON.pretty_generate(args)
end
#
STDERR.puts	"*** 終了 ***"
#
# ---------------------------------------------------------------------
```

Delete

```rb:elastic_delete.rb
#! /usr/bin/ruby
# -*- encoding: utf-8 -*-
#
#	ruby/delete/elastic_delete.rb
#
#					Oct/07/2018
#
# ---------------------------------------------------------------------
require 'faraday'
#
# ---------------------------------------------------------------------
STDERR.puts	"*** 開始 ***"
key_in = ARGV[0]
#
puts key_in
#
URL = "http://localhost:9200/cities/tochigi/" + key_in
#
begin
	Faraday.delete URL
rescue => error
	puts error
end
#
STDERR.puts	"*** 終了 ***"
#
# ---------------------------------------------------------------------
```