chewy 0.8.4 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +34 -0
  3. data/.rubocop_todo.yml +44 -0
  4. data/.travis.yml +20 -60
  5. data/Appraisals +15 -40
  6. data/CHANGELOG.md +42 -0
  7. data/Gemfile +1 -0
  8. data/Guardfile +5 -5
  9. data/README.md +155 -6
  10. data/Rakefile +11 -1
  11. data/chewy.gemspec +5 -7
  12. data/gemfiles/rails.3.2.activerecord.gemfile +1 -0
  13. data/gemfiles/rails.3.2.activerecord.kaminari.gemfile +1 -0
  14. data/gemfiles/rails.3.2.activerecord.will_paginate.gemfile +1 -0
  15. data/gemfiles/rails.4.2.activerecord.gemfile +1 -0
  16. data/gemfiles/rails.4.2.activerecord.kaminari.gemfile +1 -0
  17. data/gemfiles/rails.4.2.activerecord.will_paginate.gemfile +1 -0
  18. data/gemfiles/{rails.4.2.mongoid.4.0.0.gemfile → rails.4.2.mongoid.4.0.gemfile} +2 -1
  19. data/gemfiles/{rails.4.2.mongoid.4.0.0.kaminari.gemfile → rails.4.2.mongoid.4.0.kaminari.gemfile} +2 -1
  20. data/gemfiles/{rails.4.2.mongoid.4.0.0.will_paginate.gemfile → rails.4.2.mongoid.4.0.will_paginate.gemfile} +2 -1
  21. data/gemfiles/{rails.4.2.mongoid.5.1.0.gemfile → rails.4.2.mongoid.5.1.gemfile} +2 -1
  22. data/gemfiles/{rails.4.2.mongoid.5.1.0.kaminari.gemfile → rails.4.2.mongoid.5.1.kaminari.gemfile} +2 -1
  23. data/gemfiles/{rails.4.2.mongoid.5.1.0.will_paginate.gemfile → rails.4.2.mongoid.5.1.will_paginate.gemfile} +2 -1
  24. data/gemfiles/{rails.4.0.activerecord.gemfile → rails.5.0.activerecord.gemfile} +4 -2
  25. data/gemfiles/{rails.4.0.mongoid.4.0.0.kaminari.gemfile → rails.5.0.activerecord.kaminari.gemfile} +4 -2
  26. data/gemfiles/{rails.4.0.mongoid.4.0.0.will_paginate.gemfile → rails.5.0.activerecord.will_paginate.gemfile} +4 -2
  27. data/gemfiles/{sequel.4.31.gemfile → sequel.4.38.gemfile} +3 -2
  28. data/lib/chewy.rb +24 -16
  29. data/lib/chewy/backports/deep_dup.rb +1 -1
  30. data/lib/chewy/backports/duplicable.rb +1 -0
  31. data/lib/chewy/config.rb +13 -7
  32. data/lib/chewy/errors.rb +4 -4
  33. data/lib/chewy/fields/base.rb +19 -14
  34. data/lib/chewy/fields/root.rb +11 -9
  35. data/lib/chewy/index.rb +38 -25
  36. data/lib/chewy/index/actions.rb +17 -17
  37. data/lib/chewy/index/settings.rb +3 -4
  38. data/lib/chewy/journal.rb +107 -0
  39. data/lib/chewy/journal/apply.rb +31 -0
  40. data/lib/chewy/journal/clean.rb +24 -0
  41. data/lib/chewy/journal/entry.rb +83 -0
  42. data/lib/chewy/journal/query.rb +87 -0
  43. data/lib/chewy/log_subscriber.rb +8 -8
  44. data/lib/chewy/minitest.rb +1 -0
  45. data/lib/chewy/minitest/helpers.rb +77 -0
  46. data/lib/chewy/minitest/search_index_receiver.rb +80 -0
  47. data/lib/chewy/query.rb +116 -60
  48. data/lib/chewy/query/compose.rb +5 -6
  49. data/lib/chewy/query/criteria.rb +26 -16
  50. data/lib/chewy/query/filters.rb +9 -9
  51. data/lib/chewy/query/loading.rb +2 -2
  52. data/lib/chewy/query/nodes/and.rb +3 -3
  53. data/lib/chewy/query/nodes/base.rb +1 -1
  54. data/lib/chewy/query/nodes/bool.rb +6 -4
  55. data/lib/chewy/query/nodes/equal.rb +6 -6
  56. data/lib/chewy/query/nodes/exists.rb +2 -2
  57. data/lib/chewy/query/nodes/expr.rb +2 -2
  58. data/lib/chewy/query/nodes/field.rb +35 -31
  59. data/lib/chewy/query/nodes/has_child.rb +1 -0
  60. data/lib/chewy/query/nodes/has_parent.rb +1 -0
  61. data/lib/chewy/query/nodes/has_relation.rb +11 -13
  62. data/lib/chewy/query/nodes/match_all.rb +1 -1
  63. data/lib/chewy/query/nodes/missing.rb +2 -2
  64. data/lib/chewy/query/nodes/not.rb +3 -3
  65. data/lib/chewy/query/nodes/or.rb +3 -3
  66. data/lib/chewy/query/nodes/prefix.rb +4 -3
  67. data/lib/chewy/query/nodes/query.rb +3 -3
  68. data/lib/chewy/query/nodes/range.rb +11 -11
  69. data/lib/chewy/query/nodes/raw.rb +1 -1
  70. data/lib/chewy/query/nodes/regexp.rb +15 -11
  71. data/lib/chewy/query/nodes/script.rb +6 -6
  72. data/lib/chewy/query/pagination/will_paginate.rb +2 -2
  73. data/lib/chewy/railtie.rb +3 -3
  74. data/lib/chewy/rake_helper.rb +51 -30
  75. data/lib/chewy/repository.rb +2 -2
  76. data/lib/chewy/rspec.rb +1 -1
  77. data/lib/chewy/rspec/update_index.rb +46 -47
  78. data/lib/chewy/runtime/version.rb +4 -4
  79. data/lib/chewy/search.rb +7 -5
  80. data/lib/chewy/strategy.rb +10 -8
  81. data/lib/chewy/strategy/atomic.rb +2 -2
  82. data/lib/chewy/strategy/base.rb +4 -4
  83. data/lib/chewy/strategy/bypass.rb +1 -2
  84. data/lib/chewy/strategy/sidekiq.rb +2 -0
  85. data/lib/chewy/strategy/urgent.rb +1 -1
  86. data/lib/chewy/type.rb +51 -45
  87. data/lib/chewy/type/adapter/active_record.rb +23 -12
  88. data/lib/chewy/type/adapter/base.rb +4 -4
  89. data/lib/chewy/type/adapter/mongoid.rb +6 -6
  90. data/lib/chewy/type/adapter/object.rb +15 -12
  91. data/lib/chewy/type/adapter/orm.rb +24 -15
  92. data/lib/chewy/type/adapter/sequel.rb +11 -7
  93. data/lib/chewy/type/crutch.rb +4 -3
  94. data/lib/chewy/type/import.rb +51 -32
  95. data/lib/chewy/type/mapping.rb +17 -17
  96. data/lib/chewy/type/observe.rb +9 -7
  97. data/lib/chewy/type/witchcraft.rb +62 -23
  98. data/lib/chewy/type/wrapper.rb +20 -14
  99. data/lib/chewy/version.rb +1 -1
  100. data/lib/generators/chewy/install_generator.rb +3 -3
  101. data/lib/tasks/chewy.rake +28 -23
  102. data/spec/chewy/config_spec.rb +33 -12
  103. data/spec/chewy/fields/base_spec.rb +143 -154
  104. data/spec/chewy/fields/root_spec.rb +22 -20
  105. data/spec/chewy/fields/time_fields_spec.rb +11 -9
  106. data/spec/chewy/index/actions_spec.rb +27 -4
  107. data/spec/chewy/index/aliases_spec.rb +2 -2
  108. data/spec/chewy/index/settings_spec.rb +72 -50
  109. data/spec/chewy/index_spec.rb +95 -43
  110. data/spec/chewy/journal/apply_spec.rb +120 -0
  111. data/spec/chewy/journal/entry_spec.rb +237 -0
  112. data/spec/chewy/journal_spec.rb +173 -0
  113. data/spec/chewy/minitest/helpers_spec.rb +90 -0
  114. data/spec/chewy/minitest/search_index_receiver_spec.rb +120 -0
  115. data/spec/chewy/query/criteria_spec.rb +504 -237
  116. data/spec/chewy/query/filters_spec.rb +94 -66
  117. data/spec/chewy/query/loading_spec.rb +76 -40
  118. data/spec/chewy/query/nodes/and_spec.rb +3 -7
  119. data/spec/chewy/query/nodes/bool_spec.rb +5 -13
  120. data/spec/chewy/query/nodes/equal_spec.rb +20 -20
  121. data/spec/chewy/query/nodes/exists_spec.rb +7 -7
  122. data/spec/chewy/query/nodes/has_child_spec.rb +42 -23
  123. data/spec/chewy/query/nodes/has_parent_spec.rb +42 -23
  124. data/spec/chewy/query/nodes/match_all_spec.rb +2 -2
  125. data/spec/chewy/query/nodes/missing_spec.rb +6 -5
  126. data/spec/chewy/query/nodes/not_spec.rb +3 -7
  127. data/spec/chewy/query/nodes/or_spec.rb +3 -7
  128. data/spec/chewy/query/nodes/prefix_spec.rb +6 -6
  129. data/spec/chewy/query/nodes/query_spec.rb +3 -3
  130. data/spec/chewy/query/nodes/range_spec.rb +19 -19
  131. data/spec/chewy/query/nodes/raw_spec.rb +2 -2
  132. data/spec/chewy/query/nodes/regexp_spec.rb +31 -19
  133. data/spec/chewy/query/nodes/script_spec.rb +5 -5
  134. data/spec/chewy/query/pagination/kaminari_spec.rb +2 -2
  135. data/spec/chewy/query/pagination/will_paginage_spec.rb +6 -7
  136. data/spec/chewy/query/pagination_spec.rb +2 -3
  137. data/spec/chewy/query_spec.rb +208 -145
  138. data/spec/chewy/repository_spec.rb +8 -8
  139. data/spec/chewy/rspec/update_index_spec.rb +180 -111
  140. data/spec/chewy/search_spec.rb +8 -8
  141. data/spec/chewy/strategy/active_job_spec.rb +2 -2
  142. data/spec/chewy/strategy/atomic_spec.rb +4 -1
  143. data/spec/chewy/strategy/resque_spec.rb +2 -2
  144. data/spec/chewy/strategy/sidekiq_spec.rb +2 -2
  145. data/spec/chewy/type/actions_spec.rb +1 -1
  146. data/spec/chewy/type/adapter/active_record_spec.rb +255 -149
  147. data/spec/chewy/type/adapter/mongoid_spec.rb +169 -108
  148. data/spec/chewy/type/adapter/object_spec.rb +56 -40
  149. data/spec/chewy/type/adapter/sequel_spec.rb +248 -163
  150. data/spec/chewy/type/import_spec.rb +78 -47
  151. data/spec/chewy/type/mapping_spec.rb +6 -6
  152. data/spec/chewy/type/observe_spec.rb +20 -14
  153. data/spec/chewy/type/witchcraft_spec.rb +89 -43
  154. data/spec/chewy/type_spec.rb +4 -3
  155. data/spec/chewy_spec.rb +10 -8
  156. data/spec/spec_helper.rb +3 -0
  157. data/spec/support/active_record.rb +1 -1
  158. data/spec/support/class_helpers.rb +10 -11
  159. data/spec/support/mongoid.rb +2 -2
  160. data/spec/support/sequel.rb +1 -1
  161. metadata +65 -35
  162. data/gemfiles/rails.4.0.activerecord.kaminari.gemfile +0 -14
  163. data/gemfiles/rails.4.0.activerecord.will_paginate.gemfile +0 -14
  164. data/gemfiles/rails.4.0.mongoid.4.0.0.gemfile +0 -15
  165. data/gemfiles/rails.4.0.mongoid.5.1.0.gemfile +0 -15
  166. data/gemfiles/rails.4.0.mongoid.5.1.0.kaminari.gemfile +0 -14
  167. data/gemfiles/rails.4.0.mongoid.5.1.0.will_paginate.gemfile +0 -14
  168. data/gemfiles/rails.4.1.activerecord.gemfile +0 -15
  169. data/gemfiles/rails.4.1.activerecord.kaminari.gemfile +0 -14
  170. data/gemfiles/rails.4.1.activerecord.will_paginate.gemfile +0 -14
  171. data/gemfiles/rails.4.1.mongoid.4.0.0.gemfile +0 -15
  172. data/gemfiles/rails.4.1.mongoid.4.0.0.kaminari.gemfile +0 -14
  173. data/gemfiles/rails.4.1.mongoid.4.0.0.will_paginate.gemfile +0 -14
  174. data/gemfiles/rails.4.1.mongoid.5.1.0.gemfile +0 -15
  175. data/gemfiles/rails.4.1.mongoid.5.1.0.kaminari.gemfile +0 -14
  176. data/gemfiles/rails.4.1.mongoid.5.1.0.will_paginate.gemfile +0 -14
  177. data/gemfiles/rails.5.0.0.beta3.activerecord.gemfile +0 -16
  178. data/gemfiles/rails.5.0.0.beta3.activerecord.kaminari.gemfile +0 -16
  179. data/gemfiles/rails.5.0.0.beta3.activerecord.will_paginate.gemfile +0 -15
@@ -5,6 +5,7 @@ module Chewy
5
5
  module Nodes
6
6
  class HasChild < HasRelation
7
7
  private
8
+
8
9
  def _relation
9
10
  :has_child
10
11
  end
@@ -5,6 +5,7 @@ module Chewy
5
5
  module Nodes
6
6
  class HasParent < HasRelation
7
7
  private
8
+
8
9
  def _relation
9
10
  :has_parent
10
11
  end
@@ -6,7 +6,7 @@ module Chewy
6
6
  class HasRelation < Expr
7
7
  include Compose
8
8
 
9
- def initialize type, outer = nil
9
+ def initialize(type, outer = nil)
10
10
  @type = type.to_s
11
11
  @outer = outer
12
12
  @query_mode = :must
@@ -15,26 +15,23 @@ module Chewy
15
15
  @filters = []
16
16
  end
17
17
 
18
- def query_mode mode
18
+ def query_mode(mode)
19
19
  @query_mode = mode
20
20
  self
21
21
  end
22
22
 
23
- def filter_mode mode
23
+ def filter_mode(mode)
24
24
  @filter_mode = mode
25
25
  self
26
26
  end
27
27
 
28
- def query params = nil, &block
29
- if block
30
- raise 'Query DLS is not supported yet'
31
- else
32
- @queries.push(params)
33
- end
28
+ def query(params = nil, &block)
29
+ raise 'Query DLS is not supported yet' if block
30
+ @queries.push(params)
34
31
  self
35
32
  end
36
33
 
37
- def filter params = nil, &block
34
+ def filter(params = nil, &block)
38
35
  if block
39
36
  @filters.push(Chewy::Query::Filters.new(@outer, &block).__render__)
40
37
  else
@@ -48,12 +45,13 @@ module Chewy
48
45
  filters = _filters_join @filters, @filter_mode
49
46
 
50
47
  body = if filters && !queries
51
- {filter: filters}
48
+ { filter: filters }
52
49
  else
53
50
  _filtered_query(queries, filters)
54
- end || {}
51
+ end
52
+ body ||= {}
55
53
 
56
- {_relation => body.merge(type: @type)} if body
54
+ { _relation => body.merge(type: @type) }
57
55
  end
58
56
  end
59
57
  end
@@ -3,7 +3,7 @@ module Chewy
3
3
  module Nodes
4
4
  class MatchAll < Expr
5
5
  def __render__
6
- {match_all: {}}
6
+ { match_all: {} }
7
7
  end
8
8
  end
9
9
  end
@@ -2,7 +2,7 @@ module Chewy
2
2
  class Query
3
3
  module Nodes
4
4
  class Missing < Expr
5
- def initialize name, options = {}
5
+ def initialize(name, options = {})
6
6
  @name = name.to_s
7
7
  @options = options.reverse_merge(existence: true, null_value: false)
8
8
  end
@@ -12,7 +12,7 @@ module Chewy
12
12
  end
13
13
 
14
14
  def __render__
15
- {missing: {field: @name}.merge(@options.slice(:existence, :null_value))}
15
+ { missing: { field: @name }.merge(@options.slice(:existence, :null_value)) }
16
16
  end
17
17
  end
18
18
  end
@@ -2,7 +2,7 @@ module Chewy
2
2
  class Query
3
3
  module Nodes
4
4
  class Not < Expr
5
- def initialize expr, options = {}
5
+ def initialize(expr, options = {})
6
6
  @expr = expr
7
7
  @options = options
8
8
  end
@@ -14,9 +14,9 @@ module Chewy
14
14
  def __render__
15
15
  expr = @expr.__render__
16
16
  if @options.key?(:cache)
17
- {not: {filter: expr, _cache: !!@options[:cache]}}
17
+ { not: { filter: expr, _cache: !!@options[:cache] } }
18
18
  else
19
- {not: expr}
19
+ { not: expr }
20
20
  end
21
21
  end
22
22
  end
@@ -2,7 +2,7 @@ module Chewy
2
2
  class Query
3
3
  module Nodes
4
4
  class Or < Expr
5
- def initialize *nodes
5
+ def initialize(*nodes)
6
6
  @options = nodes.extract_options!
7
7
  @nodes = nodes.flatten.map { |node| node.is_a?(self.class) ? node.__nodes__ : node }.flatten
8
8
  end
@@ -14,9 +14,9 @@ module Chewy
14
14
  def __render__
15
15
  nodes = @nodes.map(&:__render__)
16
16
  if @options.key?(:cache)
17
- {or: {filters: nodes, _cache: !!@options[:cache]}}
17
+ { or: { filters: nodes, _cache: !!@options[:cache] } }
18
18
  else
19
- {or: nodes}
19
+ { or: nodes }
20
20
  end
21
21
  end
22
22
  end
@@ -2,13 +2,14 @@ module Chewy
2
2
  class Query
3
3
  module Nodes
4
4
  class Prefix < Expr
5
- def initialize name, value, options = {}
5
+ def initialize(name, value, options = {})
6
6
  @name = name.to_s
7
- @value, @options = value, options
7
+ @value = value
8
+ @options = options
8
9
  end
9
10
 
10
11
  def __render__
11
- filter = {prefix: {@name => @value}}
12
+ filter = { prefix: { @name => @value } }
12
13
  filter[:prefix][:_cache] = !!@options[:cache] if @options.key?(:cache)
13
14
  filter
14
15
  end
@@ -2,16 +2,16 @@ module Chewy
2
2
  class Query
3
3
  module Nodes
4
4
  class Query < Expr
5
- def initialize query, options = {}
5
+ def initialize(query, options = {})
6
6
  @query = query
7
7
  @options = options
8
8
  end
9
9
 
10
10
  def __render__
11
11
  if @options.key?(:cache)
12
- {fquery: {query: @query, _cache: !!@options[:cache]}}
12
+ { fquery: { query: @query, _cache: !!@options[:cache] } }
13
13
  else
14
- {query: @query}
14
+ { query: @query }
15
15
  end
16
16
  end
17
17
  end
@@ -3,22 +3,22 @@ module Chewy
3
3
  module Nodes
4
4
  class Range < Expr
5
5
  EXECUTION = {
6
- :i => :index,
7
- :index => :index,
8
- :f => :fielddata,
9
- :fielddata => :fielddata,
10
- }
6
+ i: :index,
7
+ index: :index,
8
+ f: :fielddata,
9
+ fielddata: :fielddata
10
+ }.freeze
11
11
 
12
- def initialize name, *args
12
+ def initialize(name, *args)
13
13
  @name = name.to_s
14
14
  @options = args.extract_options!
15
- @range = @options.reject { |k, v| ![:gt, :lt].include?(k) }
16
- @bounds = @options.reject { |k, v| ![:left_closed, :right_closed].include?(k) }
15
+ @range = @options.reject { |k, _v| ![:gt, :lt].include?(k) }
16
+ @bounds = @options.reject { |k, _v| ![:left_closed, :right_closed].include?(k) }
17
17
  execution = EXECUTION[args.first.to_sym] if args.first
18
18
  @options[:execution] = execution if execution
19
19
  end
20
20
 
21
- def & other
21
+ def &(other)
22
22
  if other.is_a?(self.class) && other.__name__ == @name
23
23
  state = __state__.merge(other.__state__)
24
24
 
@@ -51,11 +51,11 @@ module Chewy
51
51
  body[@bounds[:left_closed] ? :gte : :gt] = @range[:gt] if @range.key?(:gt)
52
52
  body[@bounds[:right_closed] ? :lte : :lt] = @range[:lt] if @range.key?(:lt)
53
53
 
54
- filter = {@name => body}
54
+ filter = { @name => body }
55
55
  filter[:_cache] = !!@options[:cache] if @options.key?(:cache)
56
56
  filter.merge!(@options.slice(:execution))
57
57
 
58
- {range: filter}
58
+ { range: filter }
59
59
  end
60
60
  end
61
61
  end
@@ -2,7 +2,7 @@ module Chewy
2
2
  class Query
3
3
  module Nodes
4
4
  class Raw < Expr
5
- def initialize raw
5
+ def initialize(raw)
6
6
  @raw = raw
7
7
  end
8
8
 
@@ -2,28 +2,32 @@ module Chewy
2
2
  class Query
3
3
  module Nodes
4
4
  class Regexp < Expr
5
- FLAGS = %w(all anystring automaton complement empty intersection interval none)
5
+ FLAGS = %w(all anystring automaton complement empty intersection interval none).freeze
6
6
 
7
- def initialize name, regexp, *args
7
+ def initialize(name, regexp, *args)
8
8
  @name = name.to_s
9
9
  @regexp = regexp.respond_to?(:source) ? regexp.source : regexp.to_s
10
10
  @options = args.extract_options!
11
- if args.present? || @options[:flags].present?
12
- @options[:flags] = FLAGS & (args.present? ? args.flatten : @options[:flags]).map(&:to_s).map(&:downcase)
13
- end
11
+ return if args.blank? && @options[:flags].blank?
12
+ @options[:flags] = FLAGS & (args.present? ? args.flatten : @options[:flags]).map(&:to_s).map(&:downcase)
14
13
  end
15
14
 
16
15
  def __render__
17
- body = @options[:flags] ?
18
- {value: @regexp, flags: @options[:flags].map(&:to_s).map(&:upcase).uniq.join('|')} :
16
+ body = if @options[:flags]
17
+ { value: @regexp, flags: @options[:flags].map(&:to_s).map(&:upcase).uniq.join('|') }
18
+ else
19
19
  @regexp
20
- filter = {@name => body}
20
+ end
21
+ filter = { @name => body }
21
22
  if @options.key?(:cache)
22
23
  filter[:_cache] = !!@options[:cache]
23
- filter[:_cache_key] = @options[:cache].is_a?(TrueClass) || @options[:cache].is_a?(FalseClass) ?
24
- @regexp.underscore : @options[:cache]
24
+ filter[:_cache_key] = if @options[:cache].is_a?(TrueClass) || @options[:cache].is_a?(FalseClass)
25
+ @regexp.underscore
26
+ else
27
+ @options[:cache]
28
+ end
25
29
  end
26
- {regexp: filter}
30
+ { regexp: filter }
27
31
  end
28
32
  end
29
33
  end
@@ -2,17 +2,17 @@ module Chewy
2
2
  class Query
3
3
  module Nodes
4
4
  class Script < Expr
5
- def initialize script, params = {}
5
+ def initialize(script, params = {})
6
6
  @script = script
7
7
  @params = params
8
- @options = params.reject { |k, v| ![:cache].include?(k) }
8
+ @options = params.reject { |k, _v| ![:cache].include?(k) }
9
9
  end
10
10
 
11
11
  def __render__
12
- script = {script: @script}
13
- script.merge!(params: @params) if @params.present?
14
- script.merge!(_cache: !!@options[:cache]) if @options.key?(:cache)
15
- {script: script}
12
+ script = { script: @script }
13
+ script[:params] = @params if @params.present?
14
+ script[:_cache] = !!@options[:cache] if @options.key?(:cache)
15
+ { script: script }
16
16
  end
17
17
  end
18
18
  end
@@ -7,12 +7,12 @@ module Chewy
7
7
 
8
8
  attr_reader :current_page, :per_page
9
9
 
10
- def paginate(options={})
10
+ def paginate(options = {})
11
11
  @current_page = ::WillPaginate::PageNumber(options[:page] || @current_page || 1)
12
12
  @page_multiplier = @current_page - 1
13
13
  @per_page = (options[:per_page] || @per_page || ::WillPaginate.per_page).to_i
14
14
 
15
- #call Chewy::Query methods to limit results
15
+ # call Chewy::Query methods to limit results
16
16
  limit(@per_page).offset(@page_multiplier * @per_page)
17
17
  end
18
18
 
@@ -51,7 +51,7 @@ module Chewy
51
51
  end
52
52
 
53
53
  initializer 'chewy.logger', after: 'active_record.logger' do
54
- ActiveSupport.on_load(:active_record) { Chewy.logger ||= ActiveRecord::Base.logger }
54
+ ActiveSupport.on_load(:active_record) { Chewy.logger ||= ActiveRecord::Base.logger }
55
55
  end
56
56
 
57
57
  initializer 'chewy.migration_strategy' do
@@ -70,9 +70,9 @@ module Chewy
70
70
  app.config.middleware.insert_after(Rails::Rack::Logger, RequestStrategy)
71
71
  end
72
72
 
73
- initializer 'chewy.add_app_chewy_path' do |app|
73
+ initializer 'chewy.add_indices_path' do |_app|
74
74
  Chewy::Railtie.all_engines.each do |engine|
75
- engine.paths.add 'app/chewy'
75
+ engine.paths.add Chewy.configuration[:indices_path]
76
76
  end
77
77
  end
78
78
  end
@@ -1,26 +1,32 @@
1
1
  module Chewy
2
2
  module RakeHelper
3
3
  class << self
4
-
5
- def subscribed_task_stats(&block)
6
- callback = ->(name, start, finish, id, payload) do
4
+ def subscribed_task_stats
5
+ import_callback = lambda do |_name, start, finish, _id, payload|
7
6
  duration = (finish - start).round(2)
8
7
  puts " Imported #{payload[:type]} for #{duration}s, documents total: #{payload[:import].try(:[], :index).to_i}"
9
- payload[:errors].each do |action, errors|
10
- puts " #{action.to_s.humanize} errors:"
11
- errors.each do |error, documents|
12
- puts " `#{error}`"
13
- puts " on #{documents.count} documents: #{documents}"
8
+ if payload[:errors]
9
+ payload[:errors].each do |action, errors|
10
+ puts " #{action.to_s.humanize} errors:"
11
+ errors.each do |error, documents|
12
+ puts " `#{error}`"
13
+ puts " on #{documents.count} documents: #{documents}"
14
+ end
14
15
  end
15
- end if payload[:errors]
16
+ end
16
17
  end
17
- ActiveSupport::Notifications.subscribed(callback, 'import_objects.chewy') do
18
- yield
18
+ journal_callback = lambda do |_, _, _, _, payload|
19
+ puts "Applying journal. Stage #{payload[:stage]}"
20
+ end
21
+ ActiveSupport::Notifications.subscribed(journal_callback, 'apply_journal.chewy') do
22
+ ActiveSupport::Notifications.subscribed(import_callback, 'import_objects.chewy') do
23
+ yield
24
+ end
19
25
  end
20
26
  end
21
27
 
22
28
  def eager_load_chewy!
23
- dirs = Chewy::Railtie.all_engines.map { |engine| engine.paths['app/chewy'] }.compact.map(&:existent).flatten.uniq
29
+ dirs = Chewy::Railtie.all_engines.map { |engine| engine.paths[Chewy.configuration[:indices_path]] }.compact.map(&:existent).flatten.uniq
24
30
 
25
31
  dirs.each do |dir|
26
32
  Dir.glob(File.join(dir, '**/*.rb')).each do |file|
@@ -29,36 +35,51 @@ module Chewy
29
35
  end
30
36
  end
31
37
 
32
- def normalize_index index
38
+ def all_indexes
39
+ eager_load_chewy!
40
+ Chewy::Index.descendants
41
+ end
42
+
43
+ def normalize_index(index)
44
+ return index if index.is_a?(Chewy::Index)
33
45
  "#{index.to_s.gsub(/index\z/i, '').camelize}Index".constantize
34
46
  end
35
47
 
48
+ def normalize_indexes(*indexes)
49
+ indexes.flatten.map { |index| normalize_index(index) }
50
+ end
51
+
36
52
  # Performs zero downtime reindexing of all documents in the specified index.
37
- def reset_index index
38
- index = normalize_index(index)
39
- puts "Resetting #{index}"
40
- index.reset! (Time.now.to_f * 1000).round
53
+ def reset_index(*indexes)
54
+ normalize_indexes(indexes).each do |index|
55
+ puts "Resetting #{index}"
56
+ time = Time.now
57
+ index.reset!((time.to_f * 1000).round)
58
+ if index.journal?
59
+ Chewy::Journal.create
60
+ Chewy::Journal::Apply.since(time, only: [index])
61
+ end
62
+ end
41
63
  end
42
64
 
43
65
  # Performs zero downtime reindexing of all documents across all indices.
44
- def reset_all
45
- eager_load_chewy!
46
- Chewy::Index.descendants.each { |index| reset_index index }
66
+ def reset_all(*except)
67
+ reset_index(all_indexes - normalize_indexes(except))
47
68
  end
48
69
 
49
- def update_index index
50
- index = normalize_index(index)
51
- puts "Updating #{index}"
52
- if index.exists?
53
- index.import
54
- else
55
- puts "Index `#{index.index_name}` does not exists. Use rake chewy:reset[#{index.index_name}] to create and update it."
70
+ def update_index(*indexes)
71
+ normalize_indexes(indexes).each do |index|
72
+ puts "Updating #{index}"
73
+ if index.exists?
74
+ index.import
75
+ else
76
+ puts "Index `#{index.index_name}` does not exists. Use rake chewy:reset[#{index.index_name}] to create and update it."
77
+ end
56
78
  end
57
79
  end
58
80
 
59
- def update_all
60
- eager_load_chewy!
61
- Chewy::Index.descendants.each { |index| update_index index }
81
+ def update_all(*except)
82
+ update_index(all_indexes - normalize_indexes(except))
62
83
  end
63
84
  end
64
85
  end