This repository has been archived by the owner on Aug 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.rb
61 lines (51 loc) · 1.52 KB
/
export.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require 'json'
require 'net/http'
require 'uri'
require 'fileutils'
# Export Elasticsearch index as a folder
class Export
def self.call(index, output)
Export.new(index, output).export
end
def initialize(index, output)
@index = index
@output = output
end
def export
$stdout.puts "Exporting '#{ES_BASE_URI}/#{index}' to '#{output}'..."
fetch_docs.each { |doc| save doc }
$stdout.puts 'Exporting done.'
end
private
ES_BASE_URI = ENV.fetch('ES_BASE_URI') { 'http://localhost:9200' }.freeze
HEADER = { 'Content-Type' => 'application/json' }.freeze
attr_reader :index, :output
def fetch_docs
uri = URI.parse "#{ES_BASE_URI}/#{index}/_search?size=1000"
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri, HEADER)
res = http.request(request)
body = JSON.parse(res.body)
docs = Array(body.dig('hits', 'hits'))
docs.sort_by { |hit| hit.dig('_id') }
end
def save(doc)
id = doc['_id']
URI_TO_FILE.each { |k, v| id.sub!(k, v) }
path = "#{output}/#{doc['_index']}/#{doc['_type']}"
FileUtils.mkdir_p path
file_name = "#{path}/#{id}.json"
File.write file_name, JSON.pretty_generate(doc['_source'])
puts "Saved #{file_name}."
end
URI_TO_FILE = {
'*' => '_x_',
':' => '_;_'
}.freeze
def to_file_name(doc)
file_name = doc['_id']
URI_TO_FILE.each { |k, v| file_name.sub!(k, v) }
"#{path}/#{doc['_index']}/#{doc['_type']}/#{file_name}}"
end
end
Export.call(ARGV[0] || '.kibana', ARGV[1] || 'export')