Commit 08fbc1d697df653582313cd7995082928012437a

Authored by Rustam Sharshenov
1 parent 807142da

Fix no records import logging exception

lib/searchkick/index.rb
1 1 module Searchkick
2 2 class Index
3   - attr_reader :name
  3 + attr_reader :name, :default_document_type
4 4  
5   - def initialize(name)
  5 + def initialize(name, default_document_type)
6 6 @name = name
  7 + @default_document_type = default_document_type
7 8 end
8 9  
9 10 def create(options = {})
... ...
lib/searchkick/logging.rb
... ... @@ -39,7 +39,7 @@ module Searchkick
39 39  
40 40 def import_with_instrumentation(records)
41 41 event = {
42   - name: "#{records.first.searchkick_klass.name} Import",
  42 + name: "#{records.any? ? records.first.searchkick_klass.name : default_document_type} Import",
43 43 count: records.size
44 44 }
45 45 ActiveSupport::Notifications.instrument("request.searchkick", event) do
... ...
lib/searchkick/model.rb
... ... @@ -18,7 +18,7 @@ module Searchkick
18 18 def self.searchkick_index
19 19 index = class_variable_get :@@searchkick_index
20 20 index = index.call if index.respond_to? :call
21   - Searchkick::Index.new(index)
  21 + Searchkick::Index.new(index, name)
22 22 end
23 23  
24 24 define_singleton_method(Searchkick.search_method_name) do |term = nil, options={}|
... ...
lib/searchkick/reindex.rb
... ... @@ -8,7 +8,7 @@ module Searchkick
8 8  
9 9 alias_name = searchkick_index.name
10 10 new_name = alias_name + "_" + Time.now.strftime("%Y%m%d%H%M%S%L")
11   - index = Searchkick::Index.new(new_name)
  11 + index = Searchkick::Index.new(new_name, name)
12 12  
13 13 clean_indices
14 14  
... ... @@ -42,7 +42,7 @@ module Searchkick
42 42 all_indices = Searchkick.client.indices.get_aliases
43 43 indices = all_indices.select{|k, v| v["aliases"].empty? && k =~ /\A#{Regexp.escape(searchkick_index.name)}_\d{14,17}\z/ }.keys
44 44 indices.each do |index|
45   - Searchkick::Index.new(index).delete
  45 + Searchkick::Index.new(index, name).delete
46 46 end
47 47 indices
48 48 end
... ...
test/index_test.rb
... ... @@ -3,8 +3,8 @@ require_relative "test_helper"
3 3 class TestIndex < Minitest::Test
4 4  
5 5 def test_clean_indices
6   - old_index = Searchkick::Index.new("products_test_20130801000000000")
7   - different_index = Searchkick::Index.new("items_test_20130801000000000")
  6 + old_index = Searchkick::Index.new("products_test_20130801000000000", 'Product')
  7 + different_index = Searchkick::Index.new("items_test_20130801000000000", 'Item')
8 8  
9 9 old_index.delete if old_index.exists?
10 10 different_index.delete if different_index.exists?
... ... @@ -21,7 +21,7 @@ class TestIndex &lt; Minitest::Test
21 21 end
22 22  
23 23 def test_clean_indices_old_format
24   - old_index = Searchkick::Index.new("products_test_20130801000000")
  24 + old_index = Searchkick::Index.new("products_test_20130801000000", 'Product')
25 25 old_index.create
26 26  
27 27 Product.clean_indices
... ...