Sha256: 2b105f5ffa0f59e73b785380616f0938c9c7e0161e75904d3f8d5a1a0e062f3d

Contents?: true

Size: 1.45 KB

Versions: 3

Compression:

Stored size: 1.45 KB

Contents

module Searchkick
  class RecordIndexer
    attr_reader :record, :index

    def initialize(record)
      @record = record
      @index = record.class.searchkick_index
    end

    def reindex(method_name = nil, refresh: false, mode: nil)
      unless [true, nil, :async, :queue].include?(mode)
        raise ArgumentError, "Invalid value for mode"
      end

      mode ||= Searchkick.callbacks_value || index.options[:callbacks] || true

      case mode
      when :queue
        if method_name
          raise Searchkick::Error, "Partial reindex not supported with queue option"
        end

        index.reindex_queue.push(record.id.to_s)
      when :async
        unless defined?(ActiveJob)
          raise Searchkick::Error, "Active Job not found"
        end

        Searchkick::ReindexV2Job.perform_later(
          record.class.name,
          record.id.to_s,
          method_name ? method_name.to_s : nil
        )
      else # bulk, true
        reindex_record(method_name)

        index.refresh if refresh
      end
    end

    private

    def reindex_record(method_name)
      if record.destroyed? || !record.persisted? || !record.should_index?
        begin
          index.remove(record)
        rescue Elasticsearch::Transport::Transport::Errors::NotFound
          # do nothing
        end
      else
        if method_name
          index.update_record(record, method_name)
        else
          index.store(record)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
searchkick-3.0.3 lib/searchkick/record_indexer.rb
searchkick_evichat-0.0.2 lib/searchkick/record_indexer.rb
searchkick-3.0.2 lib/searchkick/record_indexer.rb