flex 0.4.2 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. data/LICENSE +1 -1
  2. data/README.md +46 -7
  3. data/VERSION +1 -1
  4. data/flex.gemspec +13 -19
  5. data/lib/flex.rb +66 -392
  6. data/lib/flex/api_stubs.rb +1268 -0
  7. data/lib/flex/api_templates/cluster_api.yml +94 -0
  8. data/lib/flex/api_templates/core_api.yml +202 -0
  9. data/lib/flex/api_templates/indices_api.yml +304 -0
  10. data/lib/flex/class_proxy/base.rb +20 -6
  11. data/lib/flex/class_proxy/templates.rb +97 -0
  12. data/lib/flex/class_proxy/templates/doc.rb +91 -0
  13. data/lib/flex/class_proxy/templates/search.rb +72 -0
  14. data/lib/flex/configuration.rb +17 -49
  15. data/lib/flex/deprecation.rb +153 -0
  16. data/lib/flex/errors.rb +2 -1
  17. data/lib/flex/http_clients/base.rb +15 -0
  18. data/lib/flex/http_clients/loader.rb +51 -0
  19. data/lib/flex/http_clients/patron.rb +9 -7
  20. data/lib/flex/http_clients/rest_client.rb +6 -8
  21. data/lib/flex/logger.rb +24 -3
  22. data/lib/flex/prog_bar.rb +11 -6
  23. data/lib/flex/rails.rb +1 -13
  24. data/lib/flex/result.rb +8 -2
  25. data/lib/flex/result/document.rb +42 -13
  26. data/lib/flex/result/multi_get.rb +24 -0
  27. data/lib/flex/result/search.rb +1 -24
  28. data/lib/flex/struct/array.rb +25 -0
  29. data/lib/flex/struct/hash.rb +105 -0
  30. data/lib/flex/{result/collection.rb → struct/paginable.rb} +16 -9
  31. data/lib/flex/struct/prunable.rb +60 -0
  32. data/lib/flex/struct/symbolize.rb +27 -0
  33. data/lib/flex/tasks.rb +26 -86
  34. data/lib/flex/template.rb +60 -120
  35. data/lib/flex/template/common.rb +42 -0
  36. data/lib/flex/template/logger.rb +66 -0
  37. data/lib/flex/template/partial.rb +12 -15
  38. data/lib/flex/template/search.rb +6 -6
  39. data/lib/flex/template/slim_search.rb +1 -1
  40. data/lib/flex/template/tags.rb +19 -9
  41. data/lib/flex/templates.rb +20 -0
  42. data/lib/flex/utility_methods.rb +80 -89
  43. data/lib/flex/utils.rb +68 -25
  44. data/lib/flex/variables.rb +55 -4
  45. data/lib/tasks.rake +28 -0
  46. metadata +61 -85
  47. data/bin/flexes +0 -174
  48. data/lib/flex/api_methods.yml +0 -108
  49. data/lib/flex/class_proxy/loader.rb +0 -102
  50. data/lib/flex/class_proxy/model.rb +0 -45
  51. data/lib/flex/class_proxy/model_sync.rb +0 -23
  52. data/lib/flex/class_proxy/related_model.rb +0 -23
  53. data/lib/flex/instance_proxy/base.rb +0 -29
  54. data/lib/flex/instance_proxy/model.rb +0 -102
  55. data/lib/flex/instance_proxy/related_model.rb +0 -7
  56. data/lib/flex/loader.rb +0 -18
  57. data/lib/flex/manager.rb +0 -61
  58. data/lib/flex/model.rb +0 -24
  59. data/lib/flex/rails/engine.rb +0 -23
  60. data/lib/flex/rails/helper.rb +0 -16
  61. data/lib/flex/related_model.rb +0 -16
  62. data/lib/flex/result/indifferent_access.rb +0 -11
  63. data/lib/flex/result/source_document.rb +0 -63
  64. data/lib/flex/result/source_search.rb +0 -32
  65. data/lib/flex/structure/indifferent_access.rb +0 -44
  66. data/lib/flex/structure/mergeable.rb +0 -21
  67. data/lib/flex/template/base.rb +0 -41
  68. data/lib/flex/template/info.rb +0 -68
  69. data/lib/generators/flex/setup/setup_generator.rb +0 -48
  70. data/lib/generators/flex/setup/templates/flex_config.yml +0 -16
  71. data/lib/generators/flex/setup/templates/flex_dir/es.rb.erb +0 -18
  72. data/lib/generators/flex/setup/templates/flex_dir/es.yml.erb +0 -19
  73. data/lib/generators/flex/setup/templates/flex_dir/es_extender.rb.erb +0 -17
  74. data/lib/generators/flex/setup/templates/flex_initializer.rb.erb +0 -44
  75. data/lib/tasks/index.rake +0 -17
  76. data/test/flex.irt +0 -143
  77. data/test/flex/configuration.irt +0 -53
  78. data/test/irt_helper.rb +0 -12
@@ -2,23 +2,17 @@ module Flex
2
2
  module Utils
3
3
  extend self
4
4
 
5
- def data_from_source(source)
5
+ def parse_source(source)
6
6
  return unless source
7
- data = case source
8
- when Hash then stringified_hash(source)
9
- when /^\s*\{.+\}\s*$/m then source
10
- when String then YAML.load(source)
11
- else raise ArgumentError, "expected a String or Hash instance (got #{source.inspect})"
12
- end
13
- raise ArgumentError, "the source does not decode to a Hash or String (got #{data.inspect})" \
14
- unless data.is_a?(Hash) || data.is_a?(String)
15
- data
16
- end
17
-
18
- def deep_merge_hashes(h1, *hashes)
19
- merged = h1.dup
20
- hashes.each {|h2| merged.replace(deep_merge_hash(merged,h2))}
21
- merged
7
+ parsed = case source
8
+ when Hash then keyfy(:to_s, source)
9
+ when /^\s*\{.+\}\s*$/m then source
10
+ when String then YAML.load(source)
11
+ else raise ArgumentError, "expected a String or Hash instance, got #{source.inspect}"
12
+ end
13
+ raise ArgumentError, "the source does not decode to an Array, Hash or String, got #{parsed.inspect}" \
14
+ unless parsed.is_a?(Hash) || parsed.is_a?(Array) || parsed.is_a?(String)
15
+ parsed
22
16
  end
23
17
 
24
18
  def erb_process(source)
@@ -39,22 +33,71 @@ module Flex
39
33
  h
40
34
  end
41
35
 
42
- def stringified_hash(hash)
43
- h = {}
44
- hash.each do |k,v|
45
- h[k.to_s] = v.is_a?(Hash) ? stringified_hash(v) : v
36
+ def delete_allcaps_keys(hash)
37
+ hash.keys.each { |k| hash.delete(k) if k =~ /^[A-Z_]+$/ }
38
+ hash
39
+ end
40
+
41
+ def keyfy(to_what, obj)
42
+ case obj
43
+ when Hash
44
+ h = {}
45
+ obj.each do |k,v|
46
+ h[k.send(to_what)] = keyfy(to_what, v)
47
+ end
48
+ h
49
+ when Array
50
+ obj.map{|i| keyfy(to_what, i)}
51
+ else
52
+ obj
46
53
  end
54
+ end
55
+
56
+ def slice_hash(hash, *keys)
57
+ h = {}
58
+ keys.each{|k| h[k] = hash[k] if hash.has_key?(k)}
47
59
  h
48
60
  end
49
61
 
50
- private
62
+ def env2options(*keys)
63
+ options = {}
64
+ ENV.keys.map do |k|
65
+ key = k.downcase.to_sym
66
+ options[key] = ENV[k] if keys.include?(key)
67
+ end
68
+ options
69
+ end
70
+
71
+ def define_delegation(opts)
72
+ file, line = caller.first.split(':', 2)
73
+ line = line.to_i
74
+
75
+ obj, meth, methods, to = opts[:in], opts[:by], opts[:for], opts[:to]
51
76
 
52
- def deep_merge_hash(h1, h2)
53
- h2 ||= {}
54
- h1.merge(h2) do |key, oldval, newval|
55
- oldval.is_a?(Hash) && newval.is_a?(Hash) ? deep_merge_hash(oldval, newval) : newval
77
+ methods.each do |method|
78
+ obj.send meth, <<-method, file, line - 1
79
+ def #{method}(*args, &block) # def method_name(*args, &block)
80
+ if #{to} || #{to}.respond_to?(:#{method}) # if client || client.respond_to?(:name)
81
+ #{to}.__send__(:#{method}, *args, &block) # client.__send__(:name, *args, &block)
82
+ end # end
83
+ end # end
84
+ method
56
85
  end
86
+
57
87
  end
58
88
 
89
+ def class_name_to_type(class_name)
90
+ type = class_name.tr(':', '_')
91
+ type.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
92
+ type.gsub!(/([a-z\d])([A-Z])/,'\1_\2')
93
+ type.downcase!
94
+ type
95
+ end
96
+
97
+ def type_to_class_name(type)
98
+ type.gsub(/__(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
99
+ end
100
+
101
+
59
102
  end
60
103
  end
@@ -1,11 +1,62 @@
1
1
  module Flex
2
- class Variables < Hash
2
+ class Variables < Struct::Hash
3
3
 
4
- include Structure::Mergeable
4
+ def initialize(*hashes)
5
+ deep_merge! super(), *hashes
6
+ end
7
+
8
+ def finalize
9
+ self[:index] = self[:index].uniq.join(',') if self[:index].is_a?(Array)
10
+ self[:type] = self[:type].uniq.join(',') if self[:type].is_a?(Array)
11
+ # so you can pass :fields => [:field_one, :field_two]
12
+ self[:params!].each{|k,v| self[:params][k] = v.uniq.join(',') if v.is_a?(Array)}
13
+ if self[:page]
14
+ self[:page] = self[:page].to_i
15
+ self[:page] = 1 unless self[:page] > 0
16
+ self[:params][:from] ||= ((self[:page] - 1) * (self[:params][:size] || 10)).ceil unless self[:page] == 1
17
+ else
18
+ self[:page] = 1
19
+ end
20
+ self
21
+ end
22
+
23
+ # returns Prunable::Value if the value is in VALUES (called from stringified)
24
+ def get_prunable(key)
25
+ val = fetch_nested(key)
26
+ return val if self[:no_pruning].include?(key)
27
+ Prunable::VALUES.include?(val) ? Prunable::Value : val
28
+ end
29
+
30
+ # allows to store keys like 'a.3.c' into vars[:a][3][:c]
31
+ def store_nested(key, value)
32
+ var = unnest(key).reverse.inject(value) do |memo,k|
33
+ if k.is_a?(Symbol)
34
+ {k => memo}
35
+ else
36
+ ar = []
37
+ ar[k] = memo
38
+ ar
39
+ end
40
+ end
41
+ deep_merge! var
42
+ end
43
+
44
+ # allows to fetch values for tag names like 'a.3.c' fetching vars[:a][3][:c]
45
+ def fetch_nested(key)
46
+ unnest(key).inject(self, :fetch)
47
+ rescue NoMethodError, KeyError
48
+ raise MissingVariableError, "the required #{key.inspect} variable is missing."
49
+ end
5
50
 
6
- def initialize(hash={})
7
- replace hash
51
+ private
52
+
53
+ def unnest(key)
54
+ key.to_s.split('.').map{|s| s =~ /^\d+$/ ? s.to_i : s.to_sym}
8
55
  end
9
56
 
10
57
  end
58
+ # shorter alias
59
+ Vars = Variables
11
60
  end
61
+
62
+
@@ -0,0 +1,28 @@
1
+ require 'flex'
2
+
3
+ env = defined?(Rails) ? :environment : []
4
+
5
+ namespace :flex do
6
+
7
+ # deprecated tasks
8
+ task(:create_indices => env) do
9
+ Flex::Deprecation.warn 'flex:create_indices', 'flex:index:create', nil
10
+ Flex::Tasks.new.create_indices
11
+ end
12
+ task(:delete_indices => env) do
13
+ Flex::Deprecation.warn 'flex:delete_indices', 'flex:index:delete', nil
14
+ Flex::Tasks.new.delete_indices
15
+ end
16
+
17
+ namespace :index do
18
+
19
+ desc 'creates index/indices from the Flex::Configuration.config_file file'
20
+ task(:create => env) { Flex::Tasks.new.create_indices }
21
+
22
+ desc 'destroys index/indices in the Flex::Configuration.config_file file'
23
+ task(:delete => env) { Flex::Tasks.new.delete_indices }
24
+
25
+ end
26
+
27
+
28
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,83 +9,78 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-23 00:00:00.000000000 Z
12
+ date: 2013-08-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multi_json
16
- requirement: &70261802510500 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
- - - ~>
19
+ - - ! '>='
20
20
  - !ruby/object:Gem::Version
21
21
  version: 1.3.4
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70261802510500
25
- - !ruby/object:Gem::Dependency
26
- name: progressbar
27
- requirement: &70261802508900 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
28
25
  none: false
29
26
  requirements:
30
- - - ~>
27
+ - - ! '>='
31
28
  - !ruby/object:Gem::Version
32
- version: 0.11.0
33
- type: :runtime
34
- prerelease: false
35
- version_requirements: *70261802508900
29
+ version: 1.3.4
36
30
  - !ruby/object:Gem::Dependency
37
- name: prompter
38
- requirement: &70261802507520 !ruby/object:Gem::Requirement
31
+ name: progressbar
32
+ requirement: !ruby/object:Gem::Requirement
39
33
  none: false
40
34
  requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 0.11.0
41
38
  - - ~>
42
39
  - !ruby/object:Gem::Version
43
- version: 0.1.5
40
+ version: 0.12.0
44
41
  type: :runtime
45
42
  prerelease: false
46
- version_requirements: *70261802507520
47
- - !ruby/object:Gem::Dependency
48
- name: irt
49
- requirement: &70261802506900 !ruby/object:Gem::Requirement
43
+ version_requirements: !ruby/object:Gem::Requirement
50
44
  none: false
51
45
  requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: 0.11.0
52
49
  - - ~>
53
50
  - !ruby/object:Gem::Version
54
- version: 1.2.10
55
- type: :development
56
- prerelease: false
57
- version_requirements: *70261802506900
51
+ version: 0.12.0
58
52
  - !ruby/object:Gem::Dependency
59
- name: patron
60
- requirement: &70261802506120 !ruby/object:Gem::Requirement
53
+ name: dye
54
+ requirement: !ruby/object:Gem::Requirement
61
55
  none: false
62
56
  requirements:
63
57
  - - ~>
64
58
  - !ruby/object:Gem::Version
65
- version: 0.4.18
66
- type: :development
59
+ version: 0.1.4
60
+ type: :runtime
67
61
  prerelease: false
68
- version_requirements: *70261802506120
69
- - !ruby/object:Gem::Dependency
70
- name: rest-client
71
- requirement: &70261802505360 !ruby/object:Gem::Requirement
62
+ version_requirements: !ruby/object:Gem::Requirement
72
63
  none: false
73
64
  requirements:
74
65
  - - ~>
75
66
  - !ruby/object:Gem::Version
76
- version: 1.6.7
77
- type: :development
78
- prerelease: false
79
- version_requirements: *70261802505360
80
- description: ! 'Flex is an innovative ruby client for ElasticSearch. With Flex your
81
- code will be clean, easy to write and read, and very short: "poetic-short". Flex
82
- is fast and efficient, easy to use and customize, and offers ActiveRecord, Mongoid
83
- and Rails integration.
67
+ version: 0.1.4
68
+ description: ! 'Flex is the ultimate ruby client for elasticsearch. It is powerful,
69
+ fast and efficient, easy to use and customize.
70
+
71
+
72
+ It covers ALL the elasticsearch API, and transparently integrates it with your app
73
+ and its components, like Rails, ActiveRecord, Mongoid, ActiveModel, will_paginate,
74
+ kaminari, elasticsearch-mapper-attachments, ...
75
+
76
+
77
+ It also implements and integrates very advanced features like chainable scopes,
78
+ live-reindex, cross-model syncing, query fragment reuse, parent/child relationships,
79
+ templating, self-documenting tools, detailed debugging, ...
84
80
 
85
81
  '
86
82
  email: dd.nexus@gmail.com
87
- executables:
88
- - flexes
83
+ executables: []
89
84
  extensions: []
90
85
  extra_rdoc_files:
91
86
  - README.md
@@ -93,72 +88,54 @@ files:
93
88
  - LICENSE
94
89
  - README.md
95
90
  - VERSION
96
- - bin/flexes
97
91
  - flex.gemspec
98
92
  - lib/flex.rb
99
- - lib/flex/api_methods.yml
93
+ - lib/flex/api_stubs.rb
94
+ - lib/flex/api_templates/cluster_api.yml
95
+ - lib/flex/api_templates/core_api.yml
96
+ - lib/flex/api_templates/indices_api.yml
100
97
  - lib/flex/class_proxy/base.rb
101
- - lib/flex/class_proxy/loader.rb
102
- - lib/flex/class_proxy/model.rb
103
- - lib/flex/class_proxy/model_sync.rb
104
- - lib/flex/class_proxy/related_model.rb
98
+ - lib/flex/class_proxy/templates.rb
99
+ - lib/flex/class_proxy/templates/doc.rb
100
+ - lib/flex/class_proxy/templates/search.rb
105
101
  - lib/flex/configuration.rb
102
+ - lib/flex/deprecation.rb
106
103
  - lib/flex/errors.rb
104
+ - lib/flex/http_clients/base.rb
105
+ - lib/flex/http_clients/loader.rb
107
106
  - lib/flex/http_clients/patron.rb
108
107
  - lib/flex/http_clients/rest_client.rb
109
- - lib/flex/instance_proxy/base.rb
110
- - lib/flex/instance_proxy/model.rb
111
- - lib/flex/instance_proxy/related_model.rb
112
- - lib/flex/loader.rb
113
108
  - lib/flex/logger.rb
114
- - lib/flex/manager.rb
115
- - lib/flex/model.rb
116
109
  - lib/flex/prog_bar.rb
117
110
  - lib/flex/rails.rb
118
- - lib/flex/rails/engine.rb
119
- - lib/flex/rails/helper.rb
120
- - lib/flex/related_model.rb
121
111
  - lib/flex/result.rb
122
112
  - lib/flex/result/bulk.rb
123
- - lib/flex/result/collection.rb
124
113
  - lib/flex/result/document.rb
125
- - lib/flex/result/indifferent_access.rb
114
+ - lib/flex/result/multi_get.rb
126
115
  - lib/flex/result/search.rb
127
- - lib/flex/result/source_document.rb
128
- - lib/flex/result/source_search.rb
129
- - lib/flex/structure/indifferent_access.rb
130
- - lib/flex/structure/mergeable.rb
116
+ - lib/flex/struct/array.rb
117
+ - lib/flex/struct/hash.rb
118
+ - lib/flex/struct/paginable.rb
119
+ - lib/flex/struct/prunable.rb
120
+ - lib/flex/struct/symbolize.rb
131
121
  - lib/flex/tasks.rb
132
122
  - lib/flex/template.rb
133
- - lib/flex/template/base.rb
134
- - lib/flex/template/info.rb
123
+ - lib/flex/template/common.rb
124
+ - lib/flex/template/logger.rb
135
125
  - lib/flex/template/partial.rb
136
126
  - lib/flex/template/search.rb
137
127
  - lib/flex/template/slim_search.rb
138
128
  - lib/flex/template/tags.rb
129
+ - lib/flex/templates.rb
139
130
  - lib/flex/utility_methods.rb
140
131
  - lib/flex/utils.rb
141
132
  - lib/flex/variables.rb
142
- - lib/generators/flex/setup/setup_generator.rb
143
- - lib/generators/flex/setup/templates/flex_config.yml
144
- - lib/generators/flex/setup/templates/flex_dir/es.rb.erb
145
- - lib/generators/flex/setup/templates/flex_dir/es.yml.erb
146
- - lib/generators/flex/setup/templates/flex_dir/es_extender.rb.erb
147
- - lib/generators/flex/setup/templates/flex_initializer.rb.erb
148
- - lib/tasks/index.rake
149
- - test/flex.irt
150
- - test/flex/configuration.irt
151
- - test/irt_helper.rb
133
+ - lib/tasks.rake
152
134
  homepage: http://github.com/ddnexus/flex
153
135
  licenses: []
154
136
  post_install_message: ! "________________________________________________________________________________\n\n
155
- \ FLEX INSTALLATION NOTES\n________________________________________________________________________________\n\nIn
156
- order to use Flex, a supported http-client must be installed on this system.\n\nThe
157
- suported http-client gems are \"patron\" and \"rest-client\".\n\nYou should install
158
- \"patron\" (a libcurl based gem developed in C) for best\nperformances, or install
159
- \"rest-client\" if you cannot use libcurl on your system.\n\nAs an alternative you
160
- could eventually develop your own http-client interface\nand set the Flex::Configuration.http_client
161
- option.\n\n________________________________________________________________________________\n"
137
+ \ FLEX INSTALLATION NOTES\n________________________________________________________________________________\n\nNew
138
+ Documentation: http://ddnexus.github.io/flex\n\nUpgrading Tutorial: http://ddnexus.github.io/flex/doc/7-Tutorials/2-Migrate-from-0.x.html\n\n________________________________________________________________________________\n"
162
139
  rdoc_options:
163
140
  - --charset=UTF-8
164
141
  require_paths:
@@ -177,9 +154,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
177
154
  version: 1.3.6
178
155
  requirements: []
179
156
  rubyforge_project:
180
- rubygems_version: 1.8.10
157
+ rubygems_version: 1.8.25
181
158
  signing_key:
182
159
  specification_version: 3
183
- summary: Ruby Client for ElasticSearch
160
+ summary: The ultimate ruby client for elasticsearch
184
161
  test_files: []
185
- has_rdoc:
data/bin/flexes DELETED
@@ -1,174 +0,0 @@
1
- #! /usr/bin/env ruby
2
-
3
- require 'optparse'
4
- require 'flex'
5
-
6
- options = { }
7
- version = File.read(File.expand_path('../../VERSION', __FILE__)).strip
8
- copy = "flexes #{version} (c) 2012 Domizio Demichelis"
9
- optparse = OptionParser.new do |opts|
10
-
11
- opts.banner = <<-banner
12
-
13
- FlexEs:
14
- Flex tool to dump/load data from/to ElasticSearch.
15
- Usage:
16
- flexes <command> [options]
17
- <command>:
18
- dump dumps the data from one or more ElasticSearch indices
19
- load loads a dumpfile
20
- stats prints the full ElasticSearch stats
21
-
22
- Notice: The load command will load the dump-file into ElasticSearch without removing any pre-existent data.
23
- If you need fresh indices, use the flex:delete_indices and flex:create_indices rake tasks from your
24
- application, which will also recreate the mapping.
25
- banner
26
-
27
-
28
- opts.separator ''
29
- opts.separator 'Common options:'
30
-
31
- options[:file] = './flexes.dump'
32
- opts.on( '-f', '--file [FILE]', "The path of the dumpfile (default: '#{options[:file]}')" ) do |f|
33
- options[:file] = f
34
- end
35
-
36
- opts.separator ''
37
- opts.separator 'Dump options:'
38
-
39
- options[:index] = nil
40
- opts.on( '-i', '--index [INDEX_OR_INDICES]', Array, 'The index or comma separated indices to dump (default: all indices)' ) do |i|
41
- options[:index] = i
42
- end
43
-
44
- options[:type] = nil
45
- opts.on( '-t', '--type [TYPE_OR_TYPES]', Array, 'The type or comma separated types to dump (default: all types)' ) do |t|
46
- options[:type] = t
47
- end
48
-
49
- options[:scroll] = '5m'
50
- opts.on( '-s', '--scroll [TIME]', "The ElasticSearch scroll time (default: #{options[:scroll]})" ) do |s|
51
- options[:scroll] = s
52
- end
53
-
54
- options[:size] = 50
55
- opts.on( '-z', '--size [SIZE]', Integer, "The chunk size to dump per shard (default: #{options[:size]} * number of shards)" ) do |z|
56
- options[:size] = z
57
- end
58
-
59
- opts.separator ''
60
- opts.separator 'Load options:'
61
-
62
- options[:timeout] = 20
63
- opts.on( '-o', '--timeout [SECONDS]', Integer, "The http_client timeout for bulk loading (default: #{options[:timeout]} seconds)" ) do |o|
64
- options[:timeout] = o
65
- end
66
-
67
- options[:batch_size] = 1000
68
- opts.on( '-b', '--batch_size [BATCH_SIZE]', Integer, "The batch size to load (default: #{options[:batch_size]})" ) do |z|
69
- options[:size] = z
70
- end
71
-
72
- opts.separator ''
73
- opts.separator 'Other options:'
74
-
75
- opts.on( '-v', '--version', 'Shows the version and exits' ) do
76
- puts version
77
- exit
78
- end
79
-
80
- opts.on_tail( '-h', '--help', 'Displays this screen' ) do
81
- puts copy
82
- puts opts
83
- exit
84
- end
85
-
86
- end
87
-
88
- optparse.parse!
89
- command = ARGV.first
90
- exec "#{$0} -h" if command.nil?
91
- puts copy
92
-
93
- case command
94
-
95
- when 'dump'
96
- search_t = Flex::Template::Search.new( {:query => {:match_all => {}}},
97
- :params => { :search_type => 'scan',
98
- :scroll => options[:scroll],
99
- :size => options[:size],
100
- :fields => '_source,*' } )
101
- scroll_t = Flex::Template.new( :get,
102
- '/_search/scroll',
103
- nil,
104
- :params => { :scroll => options[:scroll] } )
105
- search_res = search_t.render options
106
- scroll_id = search_res['_scroll_id']
107
- total_hits = search_res['hits']['total']
108
- total_count = 0
109
- pbar = Flex::ProgBar.new(total_hits)
110
- dump_stats = Hash.new { |hash, key| hash[key] = Hash.new{|h,k| h[k] = 0} }
111
- file_size = 0
112
- File.open(options[:file], 'wb') do |file|
113
- while (result = scroll_t.render(:data => scroll_id)) do
114
- break if result['hits']['hits'] == []
115
- lines = result['hits']['hits'].map do |h|
116
- dump_stats[h['_index']][h['_type']] += 1
117
- meta = { :_index => h['_index'],
118
- :_type => h['_type'],
119
- :_id => h['_id'] }
120
- if h.has_key?('fields')
121
- h['fields'].each do |k,v|
122
- meta[k] = v if k[0] == '_'
123
- end
124
- end
125
- [ MultiJson.encode({'index' => meta}),
126
- MultiJson.encode(h['_source']) ].join("\n")
127
- end
128
- file.puts lines.join("\n")
129
- scroll_id = result['_scroll_id']
130
- total_count += lines.size
131
- pbar.pbar.inc(lines.size)
132
- end
133
- file_size = file.size
134
- end
135
- formatted_file_size = file_size.to_s.reverse.gsub(/...(?=.)/,'\&,').reverse
136
- pbar.pbar.finish
137
- puts "\n***** WARNING: Expected document to dump: #{total_hits}, dumped: #{total_count}. *****" \
138
- unless total_hits == total_count
139
- puts "\nDumped #{total_count} documents to #{options[:file]} (size: #{formatted_file_size} bytes)"
140
- puts dump_stats.to_yaml
141
-
142
- when 'load'
143
- Flex::Configuration.http_client_options[:timeout] = options[:timeout]
144
- chunk_size = options[:batch_size] * 2 # 2 lines per doc
145
- lines = ''
146
- line_count = 0
147
- file = File.open(options[:file])
148
- file.lines{ line_count += 1 }
149
- file.rewind
150
- pbar = Flex::ProgBar.new(line_count / 2, options[:batch_size])
151
- file.lines do |line|
152
- lines << line
153
- if file.lineno % chunk_size == 0
154
- result = Flex.bulk :lines => lines
155
- lines = ''
156
- pbar.process_result(result, options[:batch_size])
157
- end
158
- end
159
- # last chunk
160
- unless lines == ''
161
- result = Flex.bulk :lines => lines
162
- pbar.process_result(result, (file.lineno % chunk_size) / 2)
163
- end
164
- file.close
165
- pbar.finish
166
-
167
- when 'stats'
168
- puts '>> puts Flex.stats.to_yaml'
169
- puts Flex.stats.to_yaml
170
-
171
- else
172
- puts "unknown command: #{command.inspect}"
173
-
174
- end