Module: Flex::LiveReindex

Defined in:
lib/flex/live_reindex_model.rb

Overview

private module

Instance Method Summary collapse

Instance Method Details

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

Yields:

  • (_self)

Yield Parameters:

Raises:

  • (NotImplementedError)


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
# File 'lib/flex/live_reindex_model.rb', line 33

def reindex_active_models(opts={})

  raise NotImplementedError, 'Flex::LiveReindex.reindex_models requires the "flex-admin" gem. PLease, install it.' \
        unless defined?(Flex::Admin)

  yield self if block_given?

  opts[:verbose]  = true unless opts.has_key?(:verbose)
  opts[:models] ||= Conf.flex_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 Flex::ActiveModel model" \
            unless model.include?(Flex::ActiveModel)

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

      model.find_in_batches({:raw_result => true, :params => {:fields => '*,_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
# File 'lib/flex/live_reindex_model.rb', line 5

def reindex_models(opts={})

  raise NotImplementedError, 'Flex::LiveReindex.reindex_models requires the "flex-admin" gem. Please, install it.' \
        unless defined?(Flex::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?

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

  perform(opts)
end