ej 0.0.10 → 0.0.12

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: e13c00ed9f42cd2d40691d5f1acfa125fffcf284
4
- data.tar.gz: 1aef0c43f773b32ac03d1f45773f52f94787a079
3
+ metadata.gz: 3b1ec1d5f0c2e1630c9dcf7aee4e101cc3f53215
4
+ data.tar.gz: 5a4e7a17581781b171bbcba5f336442ce124bf78
5
5
  SHA512:
6
- metadata.gz: 21fdb6d389963d441e71fa521a68e125bd2756076b455b19244219d6b30f0848e729fbf333db3504c3c34336a0eaf9c102fd9be8ae616a02158882769ae892c4
7
- data.tar.gz: 508a1b1389ea5b0b642b79a5710ce5f09176033aff2530333a539aebf4149aa039244a1ed3c2e2d8604d280704d9eebbe19e0ba9957ea217d8910435c1c56400
6
+ metadata.gz: 40aa492344a215acdfac37730b50ae3f57a8212903352bf84b80b6ac9bcfebc2617ad7887cb402556c4b113ae2c60caae424ebc4f94a11bce3336039b63e96ac
7
+ data.tar.gz: 9a281585c27d17bfb97855a869a7736e014aeff119452e48754104c31553f356ec202e37fe1b6b6cc233f6dd37551fc6fe4b53a90ee8dd34bc9ff28feeaf59a0
data/lib/ej/commands.rb CHANGED
@@ -13,6 +13,8 @@ module Ej
13
13
  class_option :index, aliases: '-i', type: :string, default: '_all', desc: 'index'
14
14
  class_option :host, aliases: '-h', type: :string, default: 'localhost', desc: 'host'
15
15
  class_option :debug, aliases: '-d', type: :string, default: false, desc: 'debug mode'
16
+ class_option :format, type: :string, default: 'json', enum: ['json', 'yaml'], desc: 'format'
17
+
16
18
  map '-s' => :search
17
19
  map '-f' => :facet
18
20
  map '-c' => :count
@@ -37,14 +39,16 @@ module Ej
37
39
  option :fields, type: :array, aliases: '--fields', default: nil, desc: 'fields'
38
40
  option :source_only, type: :boolean, aliases: '--so', default: true, desc: 'from'
39
41
  option :query, type: :string, aliases: '-q', default: nil, desc: 'query'
42
+ option :sort, type: :hash, aliases: '--sort', default: nil, desc: 'ex. --sort @timestamp:desc'
40
43
  def search(query = options['query'])
41
- puts_json(@core.search(options['type'],
44
+ puts_with_format(@core.search(options['type'],
42
45
  query,
43
46
  options['size'],
44
47
  options['from'],
45
48
  options['source_only'],
46
- query,
47
- options['fields']
49
+ nil,
50
+ options['fields'],
51
+ options['sort']
48
52
  ))
49
53
  end
50
54
 
@@ -52,54 +56,56 @@ module Ej
52
56
  option :type, type: :string, aliases: '-t', default: nil, desc: 'type'
53
57
  option :query, type: :string, aliases: '-q', default: nil, desc: 'query'
54
58
  def count(query = options['query'])
55
- puts_json(@core.search(options['type'], query, 0, 0, false))
59
+ puts_with_format(@core.search(options['type'], query, 0, 0, false))
56
60
  end
57
61
 
58
62
  desc 'distinct [lucene query]', 'distinct'
59
63
  option :type, type: :string, aliases: '-t', default: nil, desc: 'type'
60
64
  option :query, type: :string, aliases: '-q', default: nil, desc: 'query'
61
65
  def distinct(term)
62
- puts_json(@core.distinct(term, options['type'], options['query']))
66
+ puts_with_format(@core.distinct(term, options['type'], options['query']))
63
67
  end
64
68
 
65
69
  desc 'copy', 'copy index'
66
70
  option :source, type: :string, aliases: '--source', required: true, desc: 'source host'
67
71
  option :dest, type: :string, aliases: '--dest', required: true, desc: 'dest host'
68
72
  option :query, type: :string, aliases: '-q', default: nil, desc: 'query'
73
+ option :per, type: :numeric, default: nil, desc: 'per'
69
74
  def copy
70
- @core.copy(options['source'], options['dest'], options['query'])
75
+ @core.copy(options['source'], options['dest'], options['query'], options['per'])
71
76
  end
72
77
 
73
78
  desc 'dump', 'dump index'
74
79
  option :query, type: :string, aliases: '-q', default: nil, desc: 'query'
80
+ option :per, type: :numeric, default: nil, desc: 'per'
75
81
  def dump
76
- @core.dump(options['query'])
82
+ @core.dump(options['query'], options['per'])
77
83
  end
78
84
 
79
85
  desc '-f', 'facet'
80
86
  option :query, type: :string, aliases: '-q', default: '*', desc: 'query'
81
87
  option :size, type: :numeric, aliases: '-n', default: 10, desc: 'size'
82
88
  def facet(term)
83
- puts_json(@core.facet(term, options['size'], options['query']))
89
+ puts_with_format(@core.facet(term, options['size'], options['query']))
84
90
  end
85
91
 
86
92
  desc 'aggs', 'aggs'
87
93
  option :query, type: :string, aliases: '-q', default: '*', desc: 'query'
88
94
  option :size, type: :numeric, aliases: '-n', default: 10, desc: 'size'
89
95
  def aggs(term)
90
- puts_json(@core.aggs(term, options['size'], options['query']))
96
+ puts_with_format(@core.aggs(term, options['size'], options['query']))
91
97
  end
92
98
 
93
99
  desc 'min', 'term'
94
100
  option :term, type: :string, aliases: '-k', desc: 'terms'
95
101
  def min
96
- puts_json(@core.min(options['term']))
102
+ puts_with_format(@core.min(options['term']))
97
103
  end
98
104
 
99
105
  desc 'max', 'count record, group by keys'
100
106
  option :term, type: :string, aliases: '-k', desc: 'terms'
101
107
  def max
102
- puts_json(@core.max(options['term']))
108
+ puts_with_format(@core.max(options['term']))
103
109
  end
104
110
 
105
111
  desc '-b', 'bulk import STDIN JSON'
@@ -114,39 +120,39 @@ module Ej
114
120
 
115
121
  desc 'health', 'health'
116
122
  def health
117
- puts_json(@core.health)
123
+ puts_with_format(@core.health)
118
124
  end
119
125
 
120
126
  desc '-a', 'list aliases'
121
127
  def aliases
122
- puts_json(@core.aliases)
128
+ puts_with_format(@core.aliases)
123
129
  end
124
130
 
125
131
  desc 'state', 'state'
126
132
  def state
127
- puts_json(@core.state)
133
+ puts_with_format(@core.state)
128
134
  end
129
135
 
130
136
  desc 'indices', 'show indices summary'
131
137
  def indices
132
- puts_json(@core.indices)
138
+ puts_with_format(@core.indices)
133
139
  end
134
140
 
135
141
  desc 'stats', 'index stats'
136
142
  def stats
137
- puts_json(@core.stats)
143
+ puts_with_format(@core.stats)
138
144
  end
139
145
 
140
146
  desc 'mapping', 'show mapping'
141
147
  def mapping
142
- puts_json(@core.mapping)
148
+ puts_with_format(@core.mapping)
143
149
  end
144
150
 
145
151
  desc 'not_analyzed', 'not analyzed'
146
152
  def not_analyzed
147
153
  json = File.read(File.expand_path('../../../template/not_analyze_template.json', __FILE__))
148
154
  hash = Yajl::Parser.parse(json)
149
- puts_json(@core.put_template('ej_init', hash))
155
+ puts_with_format(@core.put_template('ej_init', hash))
150
156
  end
151
157
 
152
158
  desc 'put_routing', "put routing.\nexsample. ej put_routing -i someindex -t sometype --path somecolumn"
@@ -155,13 +161,13 @@ module Ej
155
161
  option :path, type: :string, default: nil, required: true, desc: 'path'
156
162
  def put_routing
157
163
  body = { options['type'] => {"_routing"=>{"required"=>true, "path"=>options['path']}}}
158
- puts_json(@core.put_mapping(options['index'], options['type'], body))
164
+ puts_with_format(@core.put_mapping(options['index'], options['type'], body))
159
165
  end
160
166
 
161
167
  desc 'put_template', 'put template'
162
168
  def put_template(name)
163
169
  hash = Yajl::Parser.parse(STDIN.read)
164
- puts_json(@core.put_template(name, hash))
170
+ puts_with_format(@core.put_template(name, hash))
165
171
  end
166
172
 
167
173
  desc 'create_aliases', 'create aliases'
@@ -192,32 +198,32 @@ module Ej
192
198
 
193
199
  desc 'template', 'show template'
194
200
  def template
195
- puts_json(@core.template)
201
+ puts_with_format(@core.template)
196
202
  end
197
203
 
198
204
  desc 'settings', 'show template'
199
205
  def settings
200
- puts_json(@core.settings)
206
+ puts_with_format(@core.settings)
201
207
  end
202
208
 
203
209
  desc 'warmer', 'warmer'
204
210
  def warmer
205
- puts_json(@core.warmer)
211
+ puts_with_format(@core.warmer)
206
212
  end
207
213
 
208
214
  desc 'refresh', 'refresh'
209
215
  def refresh
210
- puts_json(@core.refresh)
216
+ puts_with_format(@core.refresh)
211
217
  end
212
218
 
213
219
  desc 'nodes_info', 'view nodes info'
214
220
  def nodes_info
215
- puts_json @core.nodes_info
221
+ puts_with_format @core.nodes_info
216
222
  end
217
223
 
218
224
  desc 'nodes_stats', 'view nodes stats'
219
225
  def nodes_stats
220
- puts_json @core.nodes_stats
226
+ puts_with_format @core.nodes_stats
221
227
  end
222
228
 
223
229
  desc '--j2h', 'json to hash'
@@ -227,9 +233,21 @@ module Ej
227
233
 
228
234
  private
229
235
 
236
+ def puts_with_format(object)
237
+ if options['format'] == 'json'
238
+ puts_json(object)
239
+ elsif options['format'] == 'yaml'
240
+ puts_yaml(object)
241
+ end
242
+ end
243
+
230
244
  def puts_json(object)
231
245
  puts Yajl::Encoder.encode(object, pretty: true)
232
246
  end
233
247
 
248
+ def puts_yaml(object)
249
+ puts YAML.dump(object)
250
+ end
251
+
234
252
  end
235
253
  end
data/lib/ej/core.rb CHANGED
@@ -8,16 +8,23 @@ require 'pp'
8
8
 
9
9
  module Ej
10
10
  class Core
11
- DEFAULT_PER = 30000
11
+ DEFAULT_PER = 1000
12
12
  def initialize(host, index, debug)
13
13
  @logger = debug ? Logger.new($stderr) : nil
14
14
  @index = index
15
15
  @client = Elasticsearch::Client.new hosts: host, logger: @logger, index: @index
16
16
  end
17
17
 
18
- def search(type, query, size, from, source_only, routing = nil, fields = nil)
18
+ def search(type, query, size, from, source_only, routing = nil, fields = nil, sort = nil)
19
19
  body = { from: from }
20
20
  body[:size] = size unless size.nil?
21
+ if sort
22
+ sorts = []
23
+ sort.each do |k, v|
24
+ sorts << { k => v }
25
+ end
26
+ body[:sort] = sorts
27
+ end
21
28
  body[:query] = { query_string: { query: query } } unless query.nil?
22
29
  search_option = { index: @index, type: type, body: body }
23
30
  search_option[:routing] = routing unless routing.nil?
@@ -32,8 +39,8 @@ module Ej
32
39
  @client.search index: @index, type: type, body: body
33
40
  end
34
41
 
35
- def copy(source, dest, query)
36
- per = DEFAULT_PER
42
+ def copy(source, dest, query, per_size)
43
+ per = per_size || DEFAULT_PER
37
44
  num = 0
38
45
  logger = Logger.new($stdout)
39
46
  source_client = Elasticsearch::Client.new hosts: source, index: @index
@@ -59,11 +66,11 @@ module Ej
59
66
  end
60
67
  end
61
68
 
62
- def dump(query)
63
- per = DEFAULT_PER
69
+ def dump(query, per_size)
70
+ per = per_size || DEFAULT_PER
64
71
  num = 0
65
- bulk_message = []
66
72
  while true
73
+ bulk_message = []
67
74
  from = num * per
68
75
  body = { size: per, from: from }
69
76
  body[:query] = { query_string: { query: query } } unless query.nil?
@@ -77,8 +84,8 @@ module Ej
77
84
  bulk_message << Yajl::Encoder.encode(source)
78
85
  end
79
86
  num += 1
87
+ puts bulk_message.join("\n")
80
88
  end
81
- puts bulk_message.join("\n")
82
89
  end
83
90
 
84
91
  def facet(term, size, query)
@@ -106,7 +113,8 @@ module Ej
106
113
  "aggs"=>
107
114
  {"agg_" + term =>
108
115
  {"terms"=>{"field"=>term, "size"=>size, "order"=>{"_count"=>"desc"}}}}}
109
- @client.search index: @index, body: body
116
+ results = @client.search index: @index, body: body
117
+ results['aggregations']["agg_" + term]['buckets']
110
118
  end
111
119
 
112
120
  def min(term)
data/lib/ej/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ej
2
- VERSION = '0.0.10'
2
+ VERSION = '0.0.12'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ej
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - toyama0919
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-10 00:00:00.000000000 Z
11
+ date: 2016-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -196,7 +196,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
196
196
  version: '0'
197
197
  requirements: []
198
198
  rubyforge_project:
199
- rubygems_version: 2.4.5
199
+ rubygems_version: 2.4.8
200
200
  signing_key:
201
201
  specification_version: 4
202
202
  summary: elasticsearch command line utility.