Class: Elastics::Admin::Tasks
- Inherits:
-
Object
- Object
- Elastics::Admin::Tasks
- Defined in:
- lib/elastics/admin.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #default_options ⇒ Object
- #dump_to_file(cli = false) ⇒ Object
-
#initialize(overrides = {}) ⇒ Tasks
constructor
A new instance of Tasks.
- #load_from_file ⇒ Object
Constructor Details
#initialize(overrides = {}) ⇒ Tasks
Returns a new instance of Tasks.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/elastics/admin.rb', line 8 def initialize(overrides={}) = Elastics::Utils. *.keys [:size] = [:size].to_i if [:size] [:timeout] = [:timeout].to_i if [:timeout] [:batch_size] = [:batch_size].to_i if [:batch_size] [:index_map] = Hash[[:index_map].split(',').map{|i|i.split(':')}] if [:index_map] && [:index_map].is_a?(String) @options = .merge().merge(overrides) end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/elastics/admin.rb', line 6 def @options end |
Instance Method Details
#default_options ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/elastics/admin.rb', line 19 def @default_options ||= { :file => './elastics.dump', :index => Conf.variables[:index], :type => Conf.variables[:type], :scroll => '5m', :size => 50, :timeout => 60, :batch_size => 500, :verbose => true, :index_map => nil } end |
#dump_to_file(cli = false) ⇒ Object
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 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/elastics/admin.rb', line 31 def dump_to_file(cli=false) vars = { :index => cli ? [:index] : ([:index] || Conf.indices.keys), :type => [:type] } Prompter.say_title "Dumping indices: #{vars[:index].inspect}" if [:verbose] if [:verbose] total_hits = Elastics.count(vars)['count'].to_i total_count = 0 = ProgBar.new(total_hits) dump_stats = Hash.new { |hash, key| hash[key] = Hash.new { |h, k| h[k] = 0 } } file_size = 0 end vars.merge! :params => { :scroll => [:scroll], :size => [:size], :fields => '_source,*' } file = [:file].is_a?(String) ? File.open([:file], 'wb') : [:file] path = file.path Elastics.dump_all(vars) do |batch| bulk_string = '' batch.each do |document| dump_stats[document['_index']][document['_type']] += 1 if [:verbose] bulk_string << Elastics.build_bulk_string(document) end file.puts bulk_string if [:verbose] total_count += batch.size ..progress += batch.size end end file_size = file.size if [:verbose] file.close if [:verbose] formatted_file_size = file_size.to_s.reverse.gsub(/...(?=.)/, '\&,').reverse ..finish unless ..finished? Prompter.say_warning "\n***** WARNING: Expected document to dump: #{total_hits}, dumped: #{total_count}. *****" \ unless total_hits == total_count Prompter.say_notice "\nDumped #{total_count} documents to #{path} (size: #{formatted_file_size} bytes)" Prompter.say_log dump_stats.to_yaml end end |
#load_from_file ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/elastics/admin.rb', line 74 def load_from_file Configuration.http_client.[:timeout] = [:timeout] chunk_size = [:batch_size] * 2 # 2 lines per doc bulk_string = '' file = [:file].is_a?(String) ? File.open([:file]) : [:file] path = file.path if [:verbose] line_count = 0 file.lines { line_count += 1 } file.rewind Prompter.say_title "Loading: #{path}" = ProgBar.new(line_count / 2, [:batch_size]) end file.lines do |line| bulk_string << ([:index_map] ? map_index(line) : line) if (file.lineno % chunk_size) == 0 result = Elastics.post_bulk_string :bulk_string => bulk_string bulk_string = '' .process_result(result, [:batch_size]) if [:verbose] end end # last chunk unless bulk_string == '' result = Elastics.post_bulk_string :bulk_string => bulk_string .process_result(result, (file.lineno % chunk_size) / 2) if [:verbose] end file.close .finish if [:verbose] end |