Module: Elastics::LiveReindex

Defined in:
lib/elastics/models_live_reindex.rb

Overview

private module

Instance Method Summary collapse

Instance Method Details

#reindex_active_models(opts = {}) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

Raises:

  • (NotImplementedError)


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
# File 'lib/elastics/models_live_reindex.rb', line 34

def reindex_active_models(opts={})
  Conf.http_client.options[:timeout] = opts[:timeout] || 60

  raise NotImplementedError, 'Elastics::LiveReindex.reindex_models requires the "elastics-admin" gem. Please, install it.' \
        unless defined?(Elastics::Admin)

  yield self if block_given?

  opts[:verbose]  = true unless opts.has_key?(:verbose)
  opts[:models] ||= Conf.elastics_active_models

  # we override the on_reindex eventually set
  on_reindex do
    opts[:models].each do |model|
      model = eval("::#{model}") if model.is_a?(String)
      raise ArgumentError, "The model #{model.name} is not a standard Elastics::ActiveModel model" \
            unless model.include?(Elastics::ActiveModel)

      pbar = ProgBar.new(model.count, nil, "Model #{model}: ") if opts[:verbose]

      model.find_in_batches({:raw_result => true, :params => {:_source => '*'}}, opts) do |result|
        batch  = result['hits']['hits']
        result = process_and_post_batch(batch)
        pbar.process_result(result, batch.size) if opts[:verbose]
      end

      pbar.finish if opts[:verbose]

    end
  end

  perform(opts)
end

#reindex_models(opts = {}) {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

Raises:

  • (NotImplementedError)


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
# File 'lib/elastics/models_live_reindex.rb', line 5

def reindex_models(opts={})
  raise NotImplementedError, 'Elastics::LiveReindex.reindex_models requires the "elastics-admin" gem. Please, install it.' \
        unless defined?(Elastics::Admin)

  on_each_change do |action, document|
    if action == 'index'
      begin
        { action => document.load! }
      rescue Mongoid::Errors::DocumentNotFound, ActiveRecord::RecordNotFound
        nil # record already deleted
      end
    else
      { action => document }
    end
  end

  yield self if block_given?

  opts[:verbose]  = true unless opts.has_key?(:verbose)

  # we override the on_reindex eventually set
  on_reindex do
    opts = opts.merge(:force => false)
    ModelTasks.new(opts).import_models
  end

  perform(opts)
end