ej 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 551d0015a2a0b74774c85195fdf145f1b531e8c6
4
- data.tar.gz: 791210a77c5f2051d745969fc77612e39675f416
3
+ metadata.gz: b0e265c64a78dc3bbee03d836b6014ff02230cf5
4
+ data.tar.gz: 99a5bc9767afe77467139e167c6e29f9ba15f295
5
5
  SHA512:
6
- metadata.gz: cdcca2d81f858557e71ca906f790421ac837cbf54c652b542b6aabe6e7492d5cd3ebf8246163842595fa00b080300b47c9dd3b0010e94e8ca4e99d584e8d78f6
7
- data.tar.gz: 85d6ca97247aa458b11305393447a9f766ebfa2747b4e626aee4abc554a58ba747af7050e76dfa3ae51b9ae14b771fdab60658a7a9e52b7e068794a8ba722010
6
+ metadata.gz: fa8937a41e19a3ef15ffc673a90625be86a59907005de7398e75e7d3f3fd80c4aa71c16ea3b6673250e697cf3b26e618e1f0bb70ae39e4ebaec50eae4fb4cf04
7
+ data.tar.gz: fa7f6f3316420ff29b4a064c23968b389cbb1b49f0b5e30ac34df9c9cd81dd0f0aac08157c3354353082769ee5e3618424dbe7d6d4f35b81ccc01eb7fd8904c6
data/.travis.yml ADDED
@@ -0,0 +1,17 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.1.*
5
+ - 2.2.*
6
+ - 2.3.3
7
+ - 2.4.0
8
+
9
+ gemfile:
10
+ - Gemfile
11
+
12
+ os:
13
+ - linux
14
+
15
+ before_install: gem update bundler
16
+
17
+ script: 'bundle exec rake spec'
data/README.md CHANGED
@@ -1,8 +1,11 @@
1
- # Ej
1
+ # Ej
2
+
3
+ [![Build Status](https://secure.travis-ci.org/toyama0919/ej.png?branch=master)](http://travis-ci.org/toyama0919/ej)
4
+ [![Gem Version](https://badge.fury.io/rb/ej.svg)](http://badge.fury.io/rb/ej)
2
5
 
3
6
  elasticsearch command line utility
4
7
 
5
- required ruby version >= 2.0
8
+ support ruby version >= 2.1
6
9
 
7
10
  ![ej4.gif](https://qiita-image-store.s3.amazonaws.com/0/26670/116a381c-98f6-aa72-fbd9-ddc4b179b744.gif)
8
11
 
@@ -35,7 +38,7 @@ ej -s -h other_host:9201
35
38
 
36
39
  ### query search and index
37
40
  ```bash
38
- ej -s "ip_address: 127.0.0.1" -i logstash-2014.07.01
41
+ ej -s "ip_address: 127.0.0.1" -i logstash-2014.07.01 -h other_host
39
42
  ```
40
43
 
41
44
  ### index list
@@ -45,17 +48,12 @@ ej -l -h other_host
45
48
 
46
49
  ### count
47
50
  ```bash
48
- ej -c "log_date: 2014-01-15"
49
- ```
50
-
51
- ### facet
52
- ```bash
53
- ej -f session_id -q "log_date: 2014-01-15"
51
+ ej -c "log_date: 2014-01-15" -h other_host
54
52
  ```
55
53
 
56
54
  ### mapping
57
55
  ```bash
58
- ej -m
56
+ ej -m -h other_host
59
57
  ```
60
58
 
61
59
  ### delete index
@@ -63,6 +61,22 @@ ej -m
63
61
  ej delete -i logstash-2014.07.01 -h other_host
64
62
  ```
65
63
 
64
+ ### copy index from remote to remote
65
+ ```bash
66
+ ej copy --source remote_host1:9200 --dest remote_host2:9200 -i logstash-2017.01.27 -q 'size: 631'
67
+ ```
68
+
69
+ ## monitor
70
+
71
+ ### node stats
72
+ ```bash
73
+ ej nodes_stats -h remote_host1
74
+ ```
75
+
76
+ ### settings
77
+ ```bash
78
+ ej settings -h remote_host1
79
+ ```
66
80
 
67
81
  ## Contributing
68
82
 
data/ej.gemspec CHANGED
@@ -18,8 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.5"
22
- spec.add_development_dependency "rake"
21
+ spec.add_development_dependency "bundler"
22
+ spec.add_development_dependency "rake", '< 11.0'
23
23
  spec.add_development_dependency "pry"
24
24
  spec.add_development_dependency "pry-doc"
25
25
  spec.add_development_dependency 'rspec', '~> 2.4'
data/lib/ej.rb CHANGED
@@ -2,5 +2,11 @@
2
2
  # !/usr/bin/env ruby
3
3
  # coding: utf-8
4
4
  require 'ej/version'
5
+ require 'ej/constants'
6
+ require 'ej/values'
7
+ require 'ej/util'
5
8
  require 'ej/core'
9
+ require 'ej/indices'
10
+ require 'ej/nodes'
11
+ require 'ej/cluster'
6
12
  require 'ej/commands'
data/lib/ej/cluster.rb ADDED
@@ -0,0 +1,17 @@
1
+ module Ej
2
+ class Cluster
3
+ def initialize(values)
4
+ @logger = values.logger
5
+ @index = values.index
6
+ @client = values.client
7
+ end
8
+
9
+ def health
10
+ @client.cluster.health
11
+ end
12
+
13
+ def state
14
+ @client.cluster.state
15
+ end
16
+ end
17
+ end
data/lib/ej/commands.rb CHANGED
@@ -1,5 +1,3 @@
1
- #!/usr/bin/env ruby
2
- # coding: utf-8
3
1
  require 'ej/core'
4
2
  require 'thor'
5
3
  require 'yajl'
@@ -17,13 +15,16 @@ module Ej
17
15
  map '-l' => :indices
18
16
  map '-a' => :aliases
19
17
  map '-m' => :mapping
20
- map '-e' => :debug_eval
21
18
  map '--health' => :health
22
19
 
23
20
  def initialize(args = [], options = {}, config = {})
24
21
  super(args, options, config)
25
22
  @global_options = config[:shell].base.options
26
- @core = Ej::Core.new(@global_options[:host], @global_options[:index], @global_options[:debug])
23
+ values = Values.new(@global_options)
24
+ @core = Ej::Core.new(values)
25
+ @indices = Ej::Indices.new(values)
26
+ @cluster = Ej::Cluster.new(values)
27
+ @nodes = Ej::Nodes.new(values)
27
28
  end
28
29
 
29
30
  desc '-s [lucene query]', 'search'
@@ -31,7 +32,7 @@ module Ej
31
32
  option :size, type: :numeric, aliases: '-n', default: nil, desc: 'size'
32
33
  option :from, type: :numeric, aliases: '--from', default: 0, desc: 'from'
33
34
  option :fields, type: :array, aliases: '--fields', default: nil, desc: 'fields'
34
- option :source_only, type: :boolean, aliases: '--so', default: true, desc: 'from'
35
+ option :meta, type: :boolean, default: false, desc: 'meta'
35
36
  option :query, type: :string, aliases: '-q', default: nil, desc: 'query'
36
37
  option :sort, type: :hash, aliases: '--sort', default: nil, desc: 'ex. --sort @timestamp:desc'
37
38
  def search(query = options[:query])
@@ -39,7 +40,7 @@ module Ej
39
40
  query,
40
41
  options[:size],
41
42
  options[:from],
42
- options[:source_only],
43
+ options[:meta],
43
44
  nil,
44
45
  options[:fields],
45
46
  options[:sort]
@@ -65,16 +66,14 @@ module Ej
65
66
  option :dest, type: :string, aliases: '--dest', required: true, desc: 'dest host'
66
67
  option :query, type: :string, aliases: '-q', default: nil, desc: 'query'
67
68
  option :per, type: :numeric, default: nil, desc: 'per'
68
- option :proc_num, type: :numeric, default: 1, desc: 'proc num for multi thread'
69
- option :from, type: :numeric, default: 0, desc: 'from'
69
+ option :scroll, type: :string, default: "1m", desc: 'scroll'
70
70
  def copy
71
71
  @core.copy(
72
72
  options[:source],
73
73
  options[:dest],
74
74
  options[:query],
75
75
  options[:per],
76
- options[:proc_num],
77
- options[:from]
76
+ options[:scroll]
78
77
  )
79
78
  end
80
79
 
@@ -112,51 +111,63 @@ module Ej
112
111
  puts_with_format(@core.max(options[:term]))
113
112
  end
114
113
 
115
- desc '-b', 'bulk import STDIN JSON'
114
+ desc '-b', 'bulk import JSON'
116
115
  option :index, aliases: '-i', type: :string, default: "logstash-#{Time.now.strftime('%Y.%m.%d')}", required: true, desc: 'index'
117
116
  option :type, type: :string, aliases: '-t', default: nil, required: true, desc: 'type'
118
117
  option :timestamp_key, aliases: '--timestamp_key', type: :string, desc: 'timestamp key', default: nil
119
118
  option :add_timestamp, type: :boolean, default: true, desc: 'add_timestamp'
120
119
  option :id_keys, type: :array, aliases: '--id', default: nil, desc: 'id'
120
+ option :inputs, type: :array, aliases: '--inputs', default: [], desc: 'input files path.'
121
121
  def bulk
122
- @core.bulk(options[:timestamp_key], options[:type], options[:add_timestamp], options[:id_keys], options[:index])
122
+ inputs = options[:inputs].empty? ? [STDIN] : options[:inputs]
123
+ inputs.each do |key|
124
+ buffer = (key.class == STDIN.class) ? STDIN.read : File.read(key)
125
+ @core.bulk(
126
+ options[:timestamp_key],
127
+ options[:type],
128
+ options[:add_timestamp],
129
+ options[:id_keys],
130
+ options[:index],
131
+ Util.parse_json(buffer)
132
+ )
133
+ end
123
134
  end
124
135
 
125
136
  desc 'health', 'health'
126
137
  def health
127
- puts_with_format(@core.health)
138
+ puts_with_format(@cluster.health)
128
139
  end
129
140
 
130
141
  desc '-a', 'list aliases'
131
142
  def aliases
132
- puts_with_format(@core.aliases)
143
+ puts_with_format(@indices.aliases)
133
144
  end
134
145
 
135
146
  desc 'state', 'state'
136
147
  def state
137
- puts_with_format(@core.state)
148
+ puts_with_format(@cluster.state)
138
149
  end
139
150
 
140
151
  desc 'indices', 'show indices summary'
141
152
  def indices
142
- puts_with_format(@core.indices)
153
+ puts_with_format(@indices.indices)
143
154
  end
144
155
 
145
156
  desc 'stats', 'index stats'
146
157
  def stats
147
- puts_with_format(@core.stats)
158
+ puts_with_format(@indices.stats)
148
159
  end
149
160
 
150
161
  desc 'mapping', 'show mapping'
151
162
  def mapping
152
- puts_with_format(@core.mapping)
163
+ puts_with_format(@indices.mapping)
153
164
  end
154
165
 
155
166
  desc 'not_analyzed', 'not analyzed'
156
167
  def not_analyzed
157
168
  json = File.read(File.expand_path('../../../template/not_analyze_template.json', __FILE__))
158
169
  hash = Yajl::Parser.parse(json)
159
- puts_with_format(@core.put_template('ej_init', hash))
170
+ puts_with_format(@indices.put_template('ej_init', hash))
160
171
  end
161
172
 
162
173
  desc 'put_routing', "put routing.\nexsample. ej put_routing -i someindex -t sometype --path somecolumn"
@@ -165,69 +176,69 @@ module Ej
165
176
  option :path, type: :string, default: nil, required: true, desc: 'path'
166
177
  def put_routing
167
178
  body = { options[:type] => {"_routing"=>{"required"=>true, "path"=>options[:path]}}}
168
- puts_with_format(@core.put_mapping(options[:index], options[:type], body))
179
+ puts_with_format(@indices.put_mapping(options[:index], options[:type], body))
169
180
  end
170
181
 
171
182
  desc 'put_template', 'put template'
172
183
  def put_template(name)
173
184
  hash = Yajl::Parser.parse(STDIN.read)
174
- puts_with_format(@core.put_template(name, hash))
185
+ puts_with_format(@indices.put_template(name, hash))
175
186
  end
176
187
 
177
188
  desc 'create_aliases', 'create aliases'
178
189
  option :alias, type: :string, aliases: '-a', default: nil, required: true, desc: 'alias name'
179
190
  option :indices, type: :array, aliases: '-x', default: nil, required: true, desc: 'index array'
180
191
  def create_aliases
181
- @core.create_aliases(options[:alias], options[:indices])
192
+ @indices.create_aliases(options[:alias], options[:indices])
182
193
  end
183
194
 
184
195
  desc 'recovery', 'recovery'
185
196
  def recovery
186
- @core.recovery
197
+ @indices.recovery
187
198
  end
188
199
 
189
200
  desc 'delete', 'delete'
190
201
  option :index, aliases: '-i', type: :string, default: nil, required: true, desc: 'index'
191
202
  option :type, type: :string, aliases: '-t', default: nil, desc: 'type'
192
- option :query, type: :string, aliases: '-q', default: nil, desc: 'query'
203
+ option :query, type: :hash, aliases: '-q', default: nil, desc: 'query'
193
204
  def delete
194
- @core.delete(options[:index], options[:type], options[:query])
205
+ @indices.delete(options[:index], options[:type], options[:query])
195
206
  end
196
207
 
197
208
  desc 'delete_template --name [name]', 'delete_template'
198
209
  option :name, type: :string, default: nil, required: true, desc: 'template name'
199
210
  def delete_template
200
- @core.delete_template(options[:name])
211
+ @indices.delete_template(options[:name])
201
212
  end
202
213
 
203
214
  desc 'template', 'show template'
204
215
  def template
205
- puts_with_format(@core.template)
216
+ puts_with_format(@indices.template)
206
217
  end
207
218
 
208
219
  desc 'settings', 'show template'
209
220
  def settings
210
- puts_with_format(@core.settings)
221
+ puts_with_format(@indices.settings)
211
222
  end
212
223
 
213
224
  desc 'warmer', 'warmer'
214
225
  def warmer
215
- puts_with_format(@core.warmer)
226
+ puts_with_format(@indices.warmer)
216
227
  end
217
228
 
218
229
  desc 'refresh', 'refresh'
219
230
  def refresh
220
- puts_with_format(@core.refresh)
231
+ puts_with_format(@indices.refresh)
221
232
  end
222
233
 
223
234
  desc 'nodes_info', 'view nodes info'
224
235
  def nodes_info
225
- puts_with_format @core.nodes_info
236
+ puts_with_format @nodes.nodes_info
226
237
  end
227
238
 
228
239
  desc 'nodes_stats', 'view nodes stats'
229
240
  def nodes_stats
230
- puts_with_format @core.nodes_stats
241
+ puts_with_format @nodes.nodes_stats
231
242
  end
232
243
 
233
244
  private
@@ -0,0 +1,3 @@
1
+ module Ej
2
+ DEFAULT_PER = 1000
3
+ end
data/lib/ej/core.rb CHANGED
@@ -1,5 +1,3 @@
1
- #!/usr/bin/env ruby
2
- # coding: utf-8
3
1
  require 'yajl'
4
2
  require 'elasticsearch'
5
3
  require 'hashie'
@@ -12,16 +10,13 @@ end
12
10
 
13
11
  module Ej
14
12
  class Core
15
- DEFAULT_PER = 1000
16
- def initialize(host, index, debug)
17
- @logger = Logger.new($stderr)
18
- @logger.level = debug ? Logger::DEBUG : Logger::INFO
19
-
20
- @index = index
21
- @client = Elasticsearch::Client.new hosts: host, logger: @logger, index: @index
13
+ def initialize(values)
14
+ @logger = values.logger
15
+ @index = values.index
16
+ @client = values.client
22
17
  end
23
18
 
24
- def search(type, query, size, from, source_only, routing = nil, fields = nil, sort = nil)
19
+ def search(type, query, size, from, meta, routing = nil, fields = nil, sort = nil)
25
20
  body = { from: from }
26
21
  body[:size] = size unless size.nil?
27
22
  if sort
@@ -36,7 +31,7 @@ module Ej
36
31
  search_option[:routing] = routing unless routing.nil?
37
32
  search_option[:_source] = fields.nil? ? nil : fields.join(',')
38
33
  results = HashWrapper.new(@client.search(search_option))
39
- source_only ? get_sources(results) : results
34
+ meta ? results : Util.get_sources(results)
40
35
  end
41
36
 
42
37
  def distinct(term, type, query)
@@ -45,42 +40,6 @@ module Ej
45
40
  @client.search index: @index, type: type, body: body
46
41
  end
47
42
 
48
- def copy(source, dest, query, per_size, proc_num, define_from = 0)
49
- per = per_size || DEFAULT_PER
50
- logger = Logger.new($stdout)
51
- source_client = Elasticsearch::Client.new hosts: source, index: @index
52
- dest_client = Elasticsearch::Client.new hosts: dest
53
- calculate_body = { size: 0 }
54
- calculate_body[:query] = { query_string: { query: query } } unless query.nil?
55
- calculate_data = HashWrapper.new(source_client.search index: @index, body: calculate_body)
56
- total = calculate_data.hits.total
57
- payloads = ((total/per) + 1).times.to_a
58
- Parallel.map(payloads, in_processes: proc_num) do |num|
59
- from = num * per
60
- if from < define_from
61
- logger.info("skip index (#{num} #{from}-#{from + per})/#{total}")
62
- next
63
- end
64
- body = { size: per, from: from }
65
- body[:query] = { query_string: { query: query } } unless query.nil?
66
- data = HashWrapper.new(source_client.search index: @index, body: body)
67
- docs = data.hits.hits
68
- bulk_message = []
69
- docs.each do |doc|
70
- source = doc.delete('_source')
71
- doc.delete('_score')
72
- ['_id', '_type', '_index'].each do |meta_field|
73
- source.delete(meta_field)
74
- end
75
- bulk_message << { index: doc.to_h }
76
- bulk_message << source
77
- end
78
- send_with_retry(dest_client, bulk_message)
79
-
80
- logger.info("copy complete (#{num} #{from}-#{from + docs.size})/#{total}")
81
- end
82
- end
83
-
84
43
  def dump(query, per_size)
85
44
  per = per_size || DEFAULT_PER
86
45
  num = 0
@@ -170,151 +129,88 @@ module Ej
170
129
  @client.search index: @index, body: body, size: 0
171
130
  end
172
131
 
173
- def aliases
174
- @client.indices.get_aliases
175
- end
176
-
177
- def health
178
- @client.cluster.health
179
- end
180
-
181
- def state
182
- @client.cluster.state
183
- end
184
-
185
- def indices
186
- @client.cat.indices format: 'json'
187
- end
188
-
189
- def stats
190
- @client.indices.stats index: @index
191
- end
192
-
193
- def put_mapping(index, type, body)
194
- @client.indices.create index: index unless @client.indices.exists index: index
195
- @client.indices.put_mapping index: index, type: type, body: body
196
- end
197
-
198
- def mapping
199
- data = @client.indices.get_mapping index: @index
200
- @index == '_all' ? data : data[@index]['mappings']
201
- end
202
-
203
- def put_template(name, hash)
204
- @client.indices.put_template name: name, body: hash
205
- end
206
-
207
- def create_aliases(als, indices)
208
- actions = []
209
- indices.each do |index|
210
- actions << { add: { index: index, alias: als } }
211
- end
212
- @client.indices.update_aliases body: {
213
- actions: actions
214
- }
215
- end
216
-
217
- def recovery
218
- @client.indices.recovery index: @index
219
- end
220
-
221
- def delete(index, type, query)
222
- if query.nil?
223
- if type.nil?
224
- @client.indices.delete index: index
132
+ def bulk(timestamp_key, type, add_timestamp, id_keys, index, data)
133
+ template = id_keys.map { |key| '%s' }.join('_') unless id_keys.nil?
134
+ bulk_message = []
135
+ data.each do |record|
136
+ if timestamp_key.nil?
137
+ timestamp = Time.now.to_datetime.to_s
225
138
  else
226
- @client.delete_by_query index: index, type: type, q: '*'
139
+ timestamp = record[timestamp_key].to_time.to_datetime.to_s
227
140
  end
228
- else
229
- @client.delete_by_query index: index, type: type, q: query
141
+ record.merge!('@timestamp' => timestamp) if add_timestamp
142
+ meta = { index: { _index: index, _type: type } }
143
+ meta[:index][:_id] = Util.generate_id(template, record, id_keys) unless id_keys.nil?
144
+ bulk_message << meta
145
+ bulk_message << record
230
146
  end
147
+ connect_with_retry { @client.bulk body: bulk_message unless bulk_message.empty? }
231
148
  end
232
149
 
233
- def template
234
- @client.indices.get_template
235
- end
150
+ def copy(source, dest, query, per_size, scroll)
151
+ source_client = Elasticsearch::Client.new hosts: source
152
+ dest_client = Elasticsearch::Client.new hosts: dest
236
153
 
237
- def delete_template(name)
238
- @client.indices.delete_template name: name
239
- end
154
+ scroll_option = get_scroll_option(@index, query, per_size, scroll)
155
+ r = connect_with_retry { source_client.search(scroll_option) }
156
+ total = r['hits']['total']
157
+ i = 0
158
+ i += bulk_results(r, dest_client, i, total)
240
159
 
241
- def settings
242
- @client.indices.get_settings
243
- end
244
-
245
- def warmer
246
- @client.indices.get_warmer index: @index
160
+ while r = connect_with_retry { source_client.scroll(scroll_id: r['_scroll_id'], scroll: scroll) } and
161
+ (not r['hits']['hits'].empty?) do
162
+ i += bulk_results(r, dest_client, i, total)
163
+ end
247
164
  end
248
165
 
249
- def refresh
250
- @client.indices.refresh index: @index
251
- end
166
+ private
252
167
 
253
- def nodes_info
254
- @client.nodes.info
168
+ def bulk_results(results, dest_client, before_size, total)
169
+ bulk_message = convert_results(results)
170
+ connect_with_retry do
171
+ dest_client.bulk body: bulk_message unless bulk_message.empty?
172
+ to_size = before_size + (bulk_message.size/2)
173
+ @logger.info "copy complete (#{before_size}-#{to_size})/#{total}"
174
+ end
175
+ return (bulk_message.size/2)
255
176
  end
256
177
 
257
- def nodes_stats
258
- @client.nodes.stats
178
+ def get_scroll_option(index, query, size, scroll)
179
+ body = {}
180
+ body[:query] = { query_string: { query: query } } unless query.nil?
181
+ search_option = { index: index, scroll: scroll, body: body, size: (size || DEFAULT_PER) }
182
+ search_option
259
183
  end
260
184
 
261
- def bulk(timestamp_key, type, add_timestamp, id_keys, index)
262
- data = parse_json(STDIN.read)
263
- template = id_keys.map { |key| '%s' }.join('_') unless id_keys.nil?
185
+ def convert_results(search_results)
186
+ data = HashWrapper.new(search_results)
187
+ docs = data.hits.hits
264
188
  bulk_message = []
265
- data.each do |record|
266
- if timestamp_key.nil?
267
- timestamp = Time.now.to_datetime.to_s
268
- else
269
- timestamp = record[timestamp_key].to_time.to_datetime.to_s
189
+ docs.each do |doc|
190
+ source = doc.delete('_source')
191
+ doc.delete('_score')
192
+ ['_id', '_type', '_index'].each do |meta_field|
193
+ source.delete(meta_field)
270
194
  end
271
- record.merge!('@timestamp' => timestamp) if add_timestamp
272
- meta = { index: { _index: index, _type: type } }
273
- meta[:index][:_id] = generate_id(template, record, id_keys) unless id_keys.nil?
274
- bulk_message << meta
275
- bulk_message << record
276
- end
277
- bulk_message.each_slice(10000).each do |block|
278
- send_with_retry(@client, block)
195
+ bulk_message << { index: doc.to_h }
196
+ bulk_message << source
279
197
  end
198
+ bulk_message
280
199
  end
281
200
 
282
- private
283
-
284
- def send_with_retry(client, bulk_message, retry_on_failure = 5)
201
+ def connect_with_retry(retry_on_failure = 5)
285
202
  retries = 0
286
203
  begin
287
- client.bulk body: bulk_message unless bulk_message.empty?
204
+ yield if block_given?
288
205
  rescue => e
289
206
  if retries < retry_on_failure
290
207
  retries += 1
291
- @logger.warn "Could not push logs to Elasticsearch, resetting connection and trying again. #{e.message}"
208
+ @logger.warn "Could not connect to Elasticsearch, resetting connection and trying again. #{e.message}"
292
209
  sleep 2**retries
293
210
  retry
294
211
  end
295
- raise "Could not push logs to Elasticsearch after #{retries} retries. #{e.message}"
296
- end
297
- end
298
-
299
- def parse_json(buffer)
300
- begin
301
- data = Yajl::Parser.parse(buffer)
302
- rescue => e
303
- data = []
304
- buffer.split("\n").each do |line|
305
- data << Yajl::Parser.parse(line)
306
- end
212
+ raise "Could not connect to Elasticsearch after #{retries} retries. #{e.message}"
307
213
  end
308
- data.class == Array ? data : [data]
309
214
  end
310
-
311
- def generate_id(template, record, id_keys)
312
- template % id_keys.map { |key| record[key] }
313
- end
314
-
315
- def get_sources(results)
316
- results.hits.hits.map { |result| result._source }
317
- end
318
-
319
215
  end
320
216
  end
data/lib/ej/indices.rb ADDED
@@ -0,0 +1,86 @@
1
+ module Ej
2
+ class Indices
3
+ def initialize(values)
4
+ @logger = values.logger
5
+ @index = values.index
6
+ @client = values.client
7
+ end
8
+
9
+ def aliases
10
+ @client.indices.get_aliases
11
+ end
12
+
13
+ def indices
14
+ @client.cat.indices format: 'json'
15
+ end
16
+
17
+ def stats
18
+ @client.indices.stats index: @index
19
+ end
20
+
21
+ def put_template(name, hash)
22
+ @client.indices.put_template name: name, body: hash
23
+ end
24
+
25
+ def create_aliases(als, indices)
26
+ actions = []
27
+ indices.each do |index|
28
+ actions << { add: { index: index, alias: als } }
29
+ end
30
+ @client.indices.update_aliases body: {
31
+ actions: actions
32
+ }
33
+ end
34
+
35
+ def recovery
36
+ @client.indices.recovery index: @index
37
+ end
38
+
39
+ def put_mapping(index, type, body)
40
+ @client.indices.create index: index unless @client.indices.exists index: index
41
+ @client.indices.put_mapping index: index, type: type, body: body
42
+ end
43
+
44
+ def mapping
45
+ data = @client.indices.get_mapping index: @index
46
+ @index == '_all' ? data : data[@index]['mappings']
47
+ end
48
+
49
+ def delete(index, type, query)
50
+ if query.nil?
51
+ if type.nil?
52
+ @client.indices.delete index: index
53
+ else
54
+ @client.delete_by_query index: index, type: type, q: '*'
55
+ end
56
+ else
57
+ body = {
58
+ query: {
59
+ match: query
60
+ }
61
+ }
62
+ @client.delete_by_query index: index, body: body
63
+ end
64
+ end
65
+
66
+ def template
67
+ @client.indices.get_template
68
+ end
69
+
70
+ def delete_template(name)
71
+ @client.indices.delete_template name: name
72
+ end
73
+
74
+ def settings
75
+ @client.indices.get_settings
76
+ end
77
+
78
+ def warmer
79
+ @client.indices.get_warmer index: @index
80
+ end
81
+
82
+ def refresh
83
+ @client.indices.refresh index: @index
84
+ end
85
+ end
86
+ end
data/lib/ej/nodes.rb ADDED
@@ -0,0 +1,17 @@
1
+ module Ej
2
+ class Nodes
3
+ def initialize(values)
4
+ @logger = values.logger
5
+ @index = values.index
6
+ @client = values.client
7
+ end
8
+
9
+ def nodes_info
10
+ @client.nodes.info
11
+ end
12
+
13
+ def nodes_stats
14
+ @client.nodes.stats
15
+ end
16
+ end
17
+ end
data/lib/ej/util.rb ADDED
@@ -0,0 +1,23 @@
1
+ module Ej
2
+ class Util
3
+ def self.parse_json(buffer)
4
+ begin
5
+ data = Yajl::Parser.parse(buffer)
6
+ rescue => e
7
+ data = []
8
+ buffer.lines.each do |line|
9
+ data << Yajl::Parser.parse(line)
10
+ end
11
+ end
12
+ data.class == Array ? data : [data]
13
+ end
14
+
15
+ def self.generate_id(template, record, id_keys)
16
+ template % id_keys.map { |key| record[key] }
17
+ end
18
+
19
+ def self.get_sources(results)
20
+ results.hits.hits.map { |result| result._source }
21
+ end
22
+ end
23
+ end
data/lib/ej/values.rb ADDED
@@ -0,0 +1,18 @@
1
+ module Ej
2
+ class Values
3
+ attr_reader :client
4
+ attr_reader :index
5
+ attr_reader :logger
6
+
7
+ def initialize(global_options)
8
+ @logger = Logger.new($stderr)
9
+ @logger.level = global_options[:debug] ? Logger::DEBUG : Logger::INFO
10
+ @client = Elasticsearch::Client.new(
11
+ hosts: global_options[:host],
12
+ logger: @logger,
13
+ index: global_options[:index]
14
+ )
15
+ @index = global_options[:index]
16
+ end
17
+ end
18
+ end
data/lib/ej/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ej
2
- VERSION = '0.1.4'
2
+ VERSION = '0.1.5'
3
3
  end
@@ -14,7 +14,7 @@ describe Ej::Commands do
14
14
  output.should include('--query')
15
15
  output.should include('--size')
16
16
  output.should include('--from')
17
- output.should include('--source-only')
17
+ output.should include('--meta')
18
18
  end
19
19
 
20
20
  it "should exists copy option" do
data/spec/core_spec.rb CHANGED
@@ -3,29 +3,20 @@ require 'ej'
3
3
 
4
4
  describe Ej::Core do
5
5
  before do
6
- @core = Core.new('localhost', '_all', false)
6
+ values = Values.new(
7
+ {
8
+ index: '_all',
9
+ host: 'localhost',
10
+ debug: false
11
+ }
12
+ )
13
+ @core = Core.new(values)
7
14
  end
8
15
 
9
16
  it "core not nil" do
10
17
  @core.should_not nil
11
18
  end
12
19
 
13
- it "parse json" do
14
- @core.send(:parse_json, '{"id":1, "name":"rspec"}').should == [{"id" => 1, "name" => "rspec"}]
15
- end
16
-
17
- it "parse json" do
18
- json = %[{"id":1, "name":"rspec"}
19
- {"id":1, "name":"rspec"}
20
- {"id":1, "name":"rspec"}
21
- {"id":1, "name":"rspec"}]
22
- @core.send(:parse_json, json).should == [{"id" => 1, "name" => "rspec"}, {"id" => 1, "name" => "rspec"}, {"id" => 1, "name" => "rspec"}, {"id" => 1, "name" => "rspec"}]
23
- end
24
-
25
- it "generate id" do
26
- @core.send(:generate_id, '%s_%s', {"id" => 1, "name" => "rspec"}, ['id', 'name']).should == '1_rspec'
27
- end
28
-
29
20
  after do
30
21
  end
31
22
  end
data/spec/util_spec.rb ADDED
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+ require 'ej'
3
+
4
+ describe Ej::Util do
5
+ before do
6
+ end
7
+
8
+ it "parse json" do
9
+ Util.parse_json('{"id":1, "name":"rspec"}').should == [{"id" => 1, "name" => "rspec"}]
10
+ end
11
+
12
+ it "parse jsonl" do
13
+ json = %[{"id":1, "name":"rspec"}
14
+ {"id":1, "name":"rspec"}
15
+ {"id":1, "name":"rspec"}
16
+ {"id":1, "name":"rspec"}]
17
+ Util.parse_json(json).should == [{"id" => 1, "name" => "rspec"}, {"id" => 1, "name" => "rspec"}, {"id" => 1, "name" => "rspec"}, {"id" => 1, "name" => "rspec"}]
18
+ end
19
+
20
+ it "parse json array" do
21
+ json = %[[
22
+ {"id":1, "name":"rspec"},
23
+ {"id":2, "name":"rspec"},
24
+ {"id":3, "name":"rspec"},
25
+ {"id":4, "name":"rspec"}
26
+ ]]
27
+ Util.parse_json(json).should == [{"id" => 1, "name" => "rspec"}, {"id" => 2, "name" => "rspec"}, {"id" => 3, "name" => "rspec"}, {"id" => 4, "name" => "rspec"}]
28
+ end
29
+
30
+ it "generate id" do
31
+ Util.generate_id('%s_%s', {"id" => 1, "name" => "rspec"}, ['id', 'name']).should == '1_rspec'
32
+ end
33
+
34
+ after do
35
+ end
36
+ end
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ej
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - toyama0919
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-03-08 00:00:00.000000000 Z
11
+ date: 2017-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '1.5'
19
+ version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '1.5'
26
+ version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - "<"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: '11.0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - "<"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: '11.0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: pry
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -160,6 +160,7 @@ extra_rdoc_files: []
160
160
  files:
161
161
  - ".gitignore"
162
162
  - ".pryrc"
163
+ - ".travis.yml"
163
164
  - Gemfile
164
165
  - LICENSE.txt
165
166
  - README.md
@@ -167,13 +168,20 @@ files:
167
168
  - bin/ej
168
169
  - ej.gemspec
169
170
  - lib/ej.rb
171
+ - lib/ej/cluster.rb
170
172
  - lib/ej/commands.rb
173
+ - lib/ej/constants.rb
171
174
  - lib/ej/core.rb
175
+ - lib/ej/indices.rb
176
+ - lib/ej/nodes.rb
177
+ - lib/ej/util.rb
178
+ - lib/ej/values.rb
172
179
  - lib/ej/version.rb
173
180
  - spec/commands_spec.rb
174
181
  - spec/core_spec.rb
175
182
  - spec/ej_spec.rb
176
183
  - spec/spec_helper.rb
184
+ - spec/util_spec.rb
177
185
  - template/ejrc.erb
178
186
  - template/not_analyze_template.json
179
187
  homepage: https://github.com/toyama0919/ej
@@ -196,7 +204,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
196
204
  version: '0'
197
205
  requirements: []
198
206
  rubyforge_project:
199
- rubygems_version: 2.5.1
207
+ rubygems_version: 2.6.11
200
208
  signing_key:
201
209
  specification_version: 4
202
210
  summary: elasticsearch command line utility.
@@ -205,4 +213,4 @@ test_files:
205
213
  - spec/core_spec.rb
206
214
  - spec/ej_spec.rb
207
215
  - spec/spec_helper.rb
208
- has_rdoc:
216
+ - spec/util_spec.rb