chewy 0.10.1 → 7.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (243) hide show
  1. checksums.yaml +5 -5
  2. data/.github/CODEOWNERS +1 -0
  3. data/.github/ISSUE_TEMPLATE/bug_report.md +39 -0
  4. data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  5. data/.github/PULL_REQUEST_TEMPLATE.md +16 -0
  6. data/.github/workflows/ruby.yml +74 -0
  7. data/.rubocop.yml +28 -23
  8. data/.rubocop_todo.yml +110 -22
  9. data/CHANGELOG.md +480 -298
  10. data/CODE_OF_CONDUCT.md +14 -0
  11. data/CONTRIBUTING.md +63 -0
  12. data/Gemfile +3 -5
  13. data/Guardfile +3 -1
  14. data/LICENSE.txt +1 -1
  15. data/README.md +571 -333
  16. data/chewy.gemspec +12 -15
  17. data/gemfiles/rails.5.2.activerecord.gemfile +11 -0
  18. data/gemfiles/rails.6.0.activerecord.gemfile +11 -0
  19. data/gemfiles/rails.6.1.activerecord.gemfile +13 -0
  20. data/gemfiles/rails.7.0.activerecord.gemfile +13 -0
  21. data/lib/chewy/config.rb +48 -77
  22. data/lib/chewy/errors.rb +4 -10
  23. data/lib/chewy/fields/base.rb +88 -16
  24. data/lib/chewy/fields/root.rb +15 -21
  25. data/lib/chewy/index/actions.rb +67 -38
  26. data/lib/chewy/{type → index}/adapter/active_record.rb +18 -4
  27. data/lib/chewy/{type → index}/adapter/base.rb +11 -12
  28. data/lib/chewy/{type → index}/adapter/object.rb +28 -32
  29. data/lib/chewy/{type → index}/adapter/orm.rb +26 -24
  30. data/lib/chewy/index/aliases.rb +14 -5
  31. data/lib/chewy/index/crutch.rb +40 -0
  32. data/lib/chewy/index/import/bulk_builder.rb +311 -0
  33. data/lib/chewy/{type → index}/import/bulk_request.rb +10 -9
  34. data/lib/chewy/{type → index}/import/journal_builder.rb +11 -12
  35. data/lib/chewy/{type → index}/import/routine.rb +19 -18
  36. data/lib/chewy/{type → index}/import.rb +82 -36
  37. data/lib/chewy/{type → index}/mapping.rb +63 -62
  38. data/lib/chewy/index/observe/active_record_methods.rb +87 -0
  39. data/lib/chewy/index/observe/callback.rb +34 -0
  40. data/lib/chewy/index/observe.rb +17 -0
  41. data/lib/chewy/index/settings.rb +2 -0
  42. data/lib/chewy/index/specification.rb +13 -10
  43. data/lib/chewy/{type → index}/syncer.rb +62 -63
  44. data/lib/chewy/{type → index}/witchcraft.rb +15 -9
  45. data/lib/chewy/{type → index}/wrapper.rb +16 -6
  46. data/lib/chewy/index.rb +68 -93
  47. data/lib/chewy/journal.rb +25 -14
  48. data/lib/chewy/minitest/helpers.rb +91 -18
  49. data/lib/chewy/minitest/search_index_receiver.rb +29 -33
  50. data/lib/chewy/multi_search.rb +62 -0
  51. data/lib/chewy/railtie.rb +8 -24
  52. data/lib/chewy/rake_helper.rb +141 -112
  53. data/lib/chewy/rspec/build_query.rb +12 -0
  54. data/lib/chewy/rspec/helpers.rb +55 -0
  55. data/lib/chewy/rspec/update_index.rb +58 -49
  56. data/lib/chewy/rspec.rb +2 -0
  57. data/lib/chewy/runtime.rb +1 -1
  58. data/lib/chewy/search/loader.rb +19 -41
  59. data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
  60. data/lib/chewy/search/parameters/collapse.rb +16 -0
  61. data/lib/chewy/search/parameters/concerns/query_storage.rb +6 -5
  62. data/lib/chewy/search/parameters/ignore_unavailable.rb +27 -0
  63. data/lib/chewy/search/parameters/indices.rb +78 -0
  64. data/lib/chewy/search/parameters/none.rb +1 -3
  65. data/lib/chewy/search/parameters/order.rb +6 -19
  66. data/lib/chewy/search/parameters/source.rb +5 -1
  67. data/lib/chewy/search/parameters/track_total_hits.rb +16 -0
  68. data/lib/chewy/search/parameters.rb +28 -8
  69. data/lib/chewy/search/query_proxy.rb +9 -2
  70. data/lib/chewy/search/request.rb +207 -157
  71. data/lib/chewy/search/response.rb +5 -5
  72. data/lib/chewy/search/scoping.rb +7 -8
  73. data/lib/chewy/search/scrolling.rb +14 -13
  74. data/lib/chewy/search.rb +7 -26
  75. data/lib/chewy/stash.rb +27 -29
  76. data/lib/chewy/strategy/active_job.rb +2 -2
  77. data/lib/chewy/strategy/atomic.rb +1 -1
  78. data/lib/chewy/strategy/atomic_no_refresh.rb +18 -0
  79. data/lib/chewy/strategy/base.rb +10 -0
  80. data/lib/chewy/strategy/delayed_sidekiq/scheduler.rb +148 -0
  81. data/lib/chewy/strategy/delayed_sidekiq/worker.rb +52 -0
  82. data/lib/chewy/strategy/delayed_sidekiq.rb +17 -0
  83. data/lib/chewy/strategy/lazy_sidekiq.rb +64 -0
  84. data/lib/chewy/strategy/sidekiq.rb +3 -2
  85. data/lib/chewy/strategy.rb +6 -19
  86. data/lib/chewy/version.rb +1 -1
  87. data/lib/chewy.rb +37 -80
  88. data/lib/generators/chewy/install_generator.rb +1 -1
  89. data/lib/tasks/chewy.rake +26 -32
  90. data/migration_guide.md +56 -0
  91. data/spec/chewy/config_spec.rb +27 -57
  92. data/spec/chewy/fields/base_spec.rb +457 -174
  93. data/spec/chewy/fields/root_spec.rb +24 -32
  94. data/spec/chewy/fields/time_fields_spec.rb +5 -5
  95. data/spec/chewy/index/actions_spec.rb +425 -60
  96. data/spec/chewy/{type → index}/adapter/active_record_spec.rb +110 -44
  97. data/spec/chewy/{type → index}/adapter/object_spec.rb +21 -6
  98. data/spec/chewy/index/aliases_spec.rb +3 -3
  99. data/spec/chewy/index/import/bulk_builder_spec.rb +494 -0
  100. data/spec/chewy/{type → index}/import/bulk_request_spec.rb +5 -12
  101. data/spec/chewy/{type → index}/import/journal_builder_spec.rb +22 -30
  102. data/spec/chewy/{type → index}/import/routine_spec.rb +19 -19
  103. data/spec/chewy/{type → index}/import_spec.rb +154 -95
  104. data/spec/chewy/index/mapping_spec.rb +135 -0
  105. data/spec/chewy/index/observe/active_record_methods_spec.rb +68 -0
  106. data/spec/chewy/index/observe/callback_spec.rb +139 -0
  107. data/spec/chewy/index/observe_spec.rb +143 -0
  108. data/spec/chewy/index/settings_spec.rb +3 -1
  109. data/spec/chewy/index/specification_spec.rb +32 -33
  110. data/spec/chewy/{type → index}/syncer_spec.rb +14 -19
  111. data/spec/chewy/{type → index}/witchcraft_spec.rb +34 -21
  112. data/spec/chewy/index/wrapper_spec.rb +100 -0
  113. data/spec/chewy/index_spec.rb +99 -114
  114. data/spec/chewy/journal_spec.rb +56 -101
  115. data/spec/chewy/minitest/helpers_spec.rb +122 -14
  116. data/spec/chewy/minitest/search_index_receiver_spec.rb +24 -26
  117. data/spec/chewy/multi_search_spec.rb +84 -0
  118. data/spec/chewy/rake_helper_spec.rb +325 -101
  119. data/spec/chewy/rspec/build_query_spec.rb +34 -0
  120. data/spec/chewy/rspec/helpers_spec.rb +61 -0
  121. data/spec/chewy/rspec/update_index_spec.rb +106 -102
  122. data/spec/chewy/runtime_spec.rb +2 -2
  123. data/spec/chewy/search/loader_spec.rb +19 -53
  124. data/spec/chewy/search/pagination/kaminari_examples.rb +3 -5
  125. data/spec/chewy/search/pagination/kaminari_spec.rb +1 -1
  126. data/spec/chewy/search/parameters/collapse_spec.rb +5 -0
  127. data/spec/chewy/search/parameters/ignore_unavailable_spec.rb +67 -0
  128. data/spec/chewy/search/parameters/indices_spec.rb +99 -0
  129. data/spec/chewy/search/parameters/none_spec.rb +1 -1
  130. data/spec/chewy/search/parameters/order_spec.rb +18 -11
  131. data/spec/chewy/search/parameters/query_storage_examples.rb +67 -21
  132. data/spec/chewy/search/parameters/search_after_spec.rb +4 -1
  133. data/spec/chewy/search/parameters/source_spec.rb +8 -2
  134. data/spec/chewy/search/parameters/track_total_hits_spec.rb +5 -0
  135. data/spec/chewy/search/parameters_spec.rb +39 -8
  136. data/spec/chewy/search/query_proxy_spec.rb +68 -17
  137. data/spec/chewy/search/request_spec.rb +360 -149
  138. data/spec/chewy/search/response_spec.rb +35 -25
  139. data/spec/chewy/search/scrolling_spec.rb +28 -26
  140. data/spec/chewy/search_spec.rb +73 -53
  141. data/spec/chewy/stash_spec.rb +16 -26
  142. data/spec/chewy/strategy/active_job_spec.rb +23 -10
  143. data/spec/chewy/strategy/atomic_no_refresh_spec.rb +60 -0
  144. data/spec/chewy/strategy/atomic_spec.rb +9 -10
  145. data/spec/chewy/strategy/delayed_sidekiq_spec.rb +190 -0
  146. data/spec/chewy/strategy/lazy_sidekiq_spec.rb +214 -0
  147. data/spec/chewy/strategy/sidekiq_spec.rb +14 -10
  148. data/spec/chewy/strategy_spec.rb +19 -15
  149. data/spec/chewy_spec.rb +17 -110
  150. data/spec/spec_helper.rb +7 -22
  151. data/spec/support/active_record.rb +43 -5
  152. metadata +123 -198
  153. data/.travis.yml +0 -53
  154. data/Appraisals +0 -79
  155. data/LEGACY_DSL.md +0 -497
  156. data/gemfiles/rails.4.0.activerecord.gemfile +0 -14
  157. data/gemfiles/rails.4.1.activerecord.gemfile +0 -14
  158. data/gemfiles/rails.4.2.activerecord.gemfile +0 -15
  159. data/gemfiles/rails.4.2.mongoid.5.1.gemfile +0 -15
  160. data/gemfiles/rails.5.0.activerecord.gemfile +0 -15
  161. data/gemfiles/rails.5.0.mongoid.6.0.gemfile +0 -15
  162. data/gemfiles/rails.5.1.activerecord.gemfile +0 -15
  163. data/gemfiles/rails.5.1.mongoid.6.1.gemfile +0 -15
  164. data/gemfiles/sequel.4.45.gemfile +0 -11
  165. data/lib/chewy/backports/deep_dup.rb +0 -46
  166. data/lib/chewy/backports/duplicable.rb +0 -91
  167. data/lib/chewy/query/compose.rb +0 -68
  168. data/lib/chewy/query/criteria.rb +0 -191
  169. data/lib/chewy/query/filters.rb +0 -227
  170. data/lib/chewy/query/loading.rb +0 -111
  171. data/lib/chewy/query/nodes/and.rb +0 -25
  172. data/lib/chewy/query/nodes/base.rb +0 -17
  173. data/lib/chewy/query/nodes/bool.rb +0 -34
  174. data/lib/chewy/query/nodes/equal.rb +0 -34
  175. data/lib/chewy/query/nodes/exists.rb +0 -20
  176. data/lib/chewy/query/nodes/expr.rb +0 -28
  177. data/lib/chewy/query/nodes/field.rb +0 -110
  178. data/lib/chewy/query/nodes/has_child.rb +0 -15
  179. data/lib/chewy/query/nodes/has_parent.rb +0 -15
  180. data/lib/chewy/query/nodes/has_relation.rb +0 -59
  181. data/lib/chewy/query/nodes/match_all.rb +0 -11
  182. data/lib/chewy/query/nodes/missing.rb +0 -20
  183. data/lib/chewy/query/nodes/not.rb +0 -25
  184. data/lib/chewy/query/nodes/or.rb +0 -25
  185. data/lib/chewy/query/nodes/prefix.rb +0 -19
  186. data/lib/chewy/query/nodes/query.rb +0 -20
  187. data/lib/chewy/query/nodes/range.rb +0 -63
  188. data/lib/chewy/query/nodes/raw.rb +0 -15
  189. data/lib/chewy/query/nodes/regexp.rb +0 -35
  190. data/lib/chewy/query/nodes/script.rb +0 -20
  191. data/lib/chewy/query/pagination.rb +0 -25
  192. data/lib/chewy/query.rb +0 -1098
  193. data/lib/chewy/search/pagination/will_paginate.rb +0 -43
  194. data/lib/chewy/search/parameters/types.rb +0 -20
  195. data/lib/chewy/strategy/resque.rb +0 -27
  196. data/lib/chewy/strategy/shoryuken.rb +0 -40
  197. data/lib/chewy/type/actions.rb +0 -43
  198. data/lib/chewy/type/adapter/mongoid.rb +0 -69
  199. data/lib/chewy/type/adapter/sequel.rb +0 -95
  200. data/lib/chewy/type/crutch.rb +0 -32
  201. data/lib/chewy/type/import/bulk_builder.rb +0 -122
  202. data/lib/chewy/type/observe.rb +0 -78
  203. data/lib/chewy/type.rb +0 -117
  204. data/lib/sequel/plugins/chewy_observe.rb +0 -78
  205. data/spec/chewy/query/criteria_spec.rb +0 -700
  206. data/spec/chewy/query/filters_spec.rb +0 -201
  207. data/spec/chewy/query/loading_spec.rb +0 -124
  208. data/spec/chewy/query/nodes/and_spec.rb +0 -12
  209. data/spec/chewy/query/nodes/bool_spec.rb +0 -14
  210. data/spec/chewy/query/nodes/equal_spec.rb +0 -32
  211. data/spec/chewy/query/nodes/exists_spec.rb +0 -18
  212. data/spec/chewy/query/nodes/has_child_spec.rb +0 -59
  213. data/spec/chewy/query/nodes/has_parent_spec.rb +0 -59
  214. data/spec/chewy/query/nodes/match_all_spec.rb +0 -11
  215. data/spec/chewy/query/nodes/missing_spec.rb +0 -16
  216. data/spec/chewy/query/nodes/not_spec.rb +0 -13
  217. data/spec/chewy/query/nodes/or_spec.rb +0 -12
  218. data/spec/chewy/query/nodes/prefix_spec.rb +0 -16
  219. data/spec/chewy/query/nodes/query_spec.rb +0 -12
  220. data/spec/chewy/query/nodes/range_spec.rb +0 -32
  221. data/spec/chewy/query/nodes/raw_spec.rb +0 -11
  222. data/spec/chewy/query/nodes/regexp_spec.rb +0 -43
  223. data/spec/chewy/query/nodes/script_spec.rb +0 -15
  224. data/spec/chewy/query/pagination/kaminari_spec.rb +0 -5
  225. data/spec/chewy/query/pagination/will_paginate_spec.rb +0 -5
  226. data/spec/chewy/query/pagination_spec.rb +0 -39
  227. data/spec/chewy/query_spec.rb +0 -636
  228. data/spec/chewy/search/pagination/will_paginate_examples.rb +0 -63
  229. data/spec/chewy/search/pagination/will_paginate_spec.rb +0 -23
  230. data/spec/chewy/search/parameters/indices_boost_spec.rb +0 -83
  231. data/spec/chewy/search/parameters/types_spec.rb +0 -5
  232. data/spec/chewy/strategy/resque_spec.rb +0 -46
  233. data/spec/chewy/strategy/shoryuken_spec.rb +0 -64
  234. data/spec/chewy/type/actions_spec.rb +0 -50
  235. data/spec/chewy/type/adapter/mongoid_spec.rb +0 -372
  236. data/spec/chewy/type/adapter/sequel_spec.rb +0 -472
  237. data/spec/chewy/type/import/bulk_builder_spec.rb +0 -279
  238. data/spec/chewy/type/mapping_spec.rb +0 -142
  239. data/spec/chewy/type/observe_spec.rb +0 -137
  240. data/spec/chewy/type/wrapper_spec.rb +0 -98
  241. data/spec/chewy/type_spec.rb +0 -55
  242. data/spec/support/mongoid.rb +0 -93
  243. data/spec/support/sequel.rb +0 -80
@@ -2,21 +2,21 @@ module Chewy
2
2
  class Index
3
3
  # Index specification is a combination of index settings and
4
4
  # mappings. The idea behind this class is that specification
5
- # can be locked in the `Chewy::Stash` between resets, so it is
6
- # possible to track changes. In the future it is planned to
7
- # be way smarter but right now `rake chewy:deploy` checks
8
- # if there were changes and resets the index only if anything
9
- # was changed. Otherwise, the index reset is skipped.
5
+ # can be locked in the `Chewy::Stash::Specification` between
6
+ # resets, so it is possible to track changes. In the future
7
+ # it is planned to be way smarter but right now `rake chewy:deploy`
8
+ # checks if there were changes and resets the index only if
9
+ # anything was changed. Otherwise, the index reset is skipped.
10
10
  #
11
- # @see Chewy::Stash
11
+ # @see Chewy::Stash::Specification
12
12
  class Specification
13
- # @see Chewy::Index.specification
13
+ # @see Chewy::Index::Specification
14
14
  # @param index [Chewy::Index] Just a chewy index
15
15
  def initialize(index)
16
16
  @index = index
17
17
  end
18
18
 
19
- # Stores the current index specification to the `Chewy::Stash`
19
+ # Stores the current index specification to the `Chewy::Stash::Specification`
20
20
  # as json.
21
21
  #
22
22
  # @raise [Chewy::ImportFailed] if something went wrong
@@ -24,7 +24,7 @@ module Chewy
24
24
  def lock!
25
25
  Chewy::Stash::Specification.import!([
26
26
  id: @index.derivable_name,
27
- value: current.to_json
27
+ specification: Base64.encode64(current.to_json)
28
28
  ], journal: false)
29
29
  end
30
30
 
@@ -34,7 +34,10 @@ module Chewy
34
34
  # @return [Hash] hash produced with JSON parser
35
35
  def locked
36
36
  filter = {ids: {values: [@index.derivable_name]}}
37
- JSON.parse(Chewy::Stash::Specification.filter(filter).first.try!(:value) || '{}')
37
+ document = Chewy::Stash::Specification.filter(filter).first
38
+ return {} unless document
39
+
40
+ JSON.load(Base64.decode64(document.specification)) # rubocop:disable Security/JSONLoad
38
41
  end
39
42
 
40
43
  # Simply returns `Chewy::Index.specification_hash`, but
@@ -1,18 +1,18 @@
1
1
  module Chewy
2
- class Type
2
+ class Index
3
3
  # This class is able to find missing and outdated documents in the ES
4
4
  # comparing ids from the data source and the ES index. Also, if `outdated_sync_field`
5
- # existss in the index definition, it performs comparison of this field
5
+ # exists in the index definition, it performs comparison of this field
6
6
  # values for each source object and corresponding ES document. Usually,
7
7
  # this field is `updated_at` and if its value in the source is not equal
8
8
  # to the value in the index - this means that this document outdated and
9
9
  # should be reindexed.
10
10
  #
11
11
  # To fetch necessary data from the source it uses adapter method
12
- # {Chewy::Type::Adapter::Base#import_fields}, in case when the Object
12
+ # {Chewy::Index::Adapter::Base#import_fields}, in case when the Object
13
13
  # adapter is used it makes sense to read corresponding documentation.
14
14
  #
15
- # If `parallel` option is passed to the initializer - it will fetch surce and
15
+ # If `parallel` option is passed to the initializer - it will fetch source and
16
16
  # index data in parallel and then perform outdated objects calculation in
17
17
  # parallel processes. Also, further import (if required) will be performed
18
18
  # in parallel as well.
@@ -24,17 +24,17 @@ module Chewy
24
24
  # ATTENTION: synchronization may be slow in case when synchronized tables
25
25
  # are missing compound index on primary key and `outdated_sync_field`.
26
26
  #
27
- # @see Chewy::Type::Actions::ClassMethods#sync
27
+ # @see Chewy::Index::Actions::ClassMethods#sync
28
28
  class Syncer
29
29
  DEFAULT_SYNC_BATCH_SIZE = 20_000
30
- ISO_DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?\z/
31
- OUTDATED_IDS_WORKER = lambda do |outdated_sync_field_type, source_data_hash, type, total, index_data|
32
- ::Process.setproctitle("chewy [#{type}]: sync outdated calculation (#{::Parallel.worker_number + 1}/#{total})") if type
30
+ ISO_DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?\z/.freeze
31
+ OUTDATED_IDS_WORKER = lambda do |outdated_sync_field_type, source_data_hash, index, total, index_data|
32
+ ::Process.setproctitle("chewy [#{index}]: sync outdated calculation (#{::Parallel.worker_number + 1}/#{total})") if index
33
33
  index_data.each_with_object([]) do |(id, index_sync_value), result|
34
34
  next unless source_data_hash[id]
35
35
 
36
36
  outdated = if outdated_sync_field_type == 'date'
37
- !Chewy::Type::Syncer.dates_equal(typecast_date(source_data_hash[id]), DateTime.iso8601(index_sync_value))
37
+ !Chewy::Index::Syncer.dates_equal(typecast_date(source_data_hash[id]), Time.iso8601(index_sync_value))
38
38
  else
39
39
  source_data_hash[id] != index_sync_value
40
40
  end
@@ -42,8 +42,8 @@ module Chewy
42
42
  result.push(id) if outdated
43
43
  end
44
44
  end
45
- SOURCE_OR_INDEX_DATA_WORKER = lambda do |syncer, type, kind|
46
- ::Process.setproctitle("chewy [#{type}]: sync fetching data (#{kind})")
45
+ SOURCE_OR_INDEX_DATA_WORKER = lambda do |syncer, index, kind|
46
+ ::Process.setproctitle("chewy [#{index}]: sync fetching data (#{kind})")
47
47
  result = case kind
48
48
  when :source
49
49
  syncer.send(:fetch_source_data)
@@ -56,8 +56,11 @@ module Chewy
56
56
  def self.typecast_date(string)
57
57
  if string.is_a?(String) && (match = ISO_DATETIME.match(string))
58
58
  microsec = (match[7].to_r * 1_000_000).to_i
59
- date = "#{match[1]}-#{match[2]}-#{match[3]}T#{match[4]}:#{match[5]}:#{match[6]}.#{format('%06d', microsec)}+00:00"
60
- DateTime.iso8601(date)
59
+ day = "#{match[1]}-#{match[2]}-#{match[3]}"
60
+ time_with_seconds = "#{match[4]}:#{match[5]}:#{match[6]}"
61
+ microseconds = format('%06d', microsec)
62
+ date = "#{day}T#{time_with_seconds}.#{microseconds}+00:00"
63
+ Time.iso8601(date)
61
64
  else
62
65
  string
63
66
  end
@@ -68,18 +71,10 @@ module Chewy
68
71
  [one.to_i, one.usec / 1000] == [two.to_i, two.usec / 1000]
69
72
  end
70
73
 
71
- # In ActiveSupport ~> 4.0 json dumpled times without any
72
- # milliseconds, so ES stored time with the seconds precision.
73
- if ActiveSupport::VERSION::STRING < '4.1.0'
74
- def self.dates_equal(one, two)
75
- one.to_i == two.to_i
76
- end
77
- end
78
-
79
- # @param type [Chewy::Type] chewy type
74
+ # @param index [Chewy::Index] chewy index
80
75
  # @param parallel [true, Integer, Hash] options for parallel execution or the number of processes
81
- def initialize(type, parallel: nil)
82
- @type = type
76
+ def initialize(index, parallel: nil)
77
+ @index = index
83
78
  @parallel = if !parallel || parallel.is_a?(Hash)
84
79
  parallel
85
80
  elsif parallel.is_a?(Integer)
@@ -95,7 +90,8 @@ module Chewy
95
90
  def perform
96
91
  ids = missing_ids | outdated_ids
97
92
  return 0 if ids.blank?
98
- @type.import(ids, parallel: @parallel) && ids.count
93
+
94
+ @index.import(ids, parallel: @parallel) && ids.count
99
95
  end
100
96
 
101
97
  # Finds ids of all the objects that are not indexed yet or deleted
@@ -113,21 +109,19 @@ module Chewy
113
109
  end
114
110
  end
115
111
 
116
- # If type supports outdated sync, it compares for the values of the
117
- # type `outdated_sync_field` for each object and document in the source
118
- # and index and returns the ids of entities which which are having
119
- # different values there.
112
+ # If index supports outdated sync, it compares the values of the
113
+ # `outdated_sync_field` for each object and document in the source
114
+ # and index and returns the ids of entities which differ.
120
115
  #
121
- # @see Chewy::Type::Mapping::ClassMethods#supports_outdated_sync?
116
+ # @see Chewy::Index::Mapping::ClassMethods#supports_outdated_sync?
122
117
  # @return [Array<String>] an array of outdated ids
123
118
  def outdated_ids
124
- return [] if source_data.blank? || index_data.blank? || !@type.supports_outdated_sync?
125
- @outdated_ids ||= begin
126
- if @parallel
127
- parallel_outdated_ids
128
- else
129
- linear_outdated_ids
130
- end
119
+ return [] if source_data.blank? || index_data.blank? || !@index.supports_outdated_sync?
120
+
121
+ @outdated_ids ||= if @parallel
122
+ parallel_outdated_ids
123
+ else
124
+ linear_outdated_ids
131
125
  end
132
126
  end
133
127
 
@@ -142,44 +136,48 @@ module Chewy
142
136
  end
143
137
 
144
138
  def source_and_index_data
145
- @source_and_index_data ||= begin
146
- if @parallel
147
- ::ActiveRecord::Base.connection.close if defined?(::ActiveRecord::Base)
148
- result = ::Parallel.map(%i[source index], @parallel, &SOURCE_OR_INDEX_DATA_WORKER.curry[self, @type])
149
- ::ActiveRecord::Base.connection.reconnect! if defined?(::ActiveRecord::Base)
150
- if result.first.keys.first == :source
151
- [result.first.values.first, result.second.values.first]
152
- else
153
- [result.second.values.first, result.first.values.first]
154
- end
139
+ @source_and_index_data ||= if @parallel
140
+ ::ActiveRecord::Base.connection.close if defined?(::ActiveRecord::Base)
141
+ result = ::Parallel.map(%i[source index], @parallel, &SOURCE_OR_INDEX_DATA_WORKER.curry[self, @index])
142
+ ::ActiveRecord::Base.connection.reconnect! if defined?(::ActiveRecord::Base)
143
+ if result.first.keys.first == :source
144
+ [result.first.values.first, result.second.values.first]
155
145
  else
156
- [fetch_source_data, fetch_index_data]
146
+ [result.second.values.first, result.first.values.first]
157
147
  end
148
+ else
149
+ [fetch_source_data, fetch_index_data]
158
150
  end
159
151
  end
160
152
 
161
153
  def fetch_source_data
162
- if @type.supports_outdated_sync?
163
- @type.adapter.import_fields(fields: [@type.outdated_sync_field], batch_size: DEFAULT_SYNC_BATCH_SIZE, typecast: false).to_a.flatten(1).each do |data|
154
+ if @index.supports_outdated_sync?
155
+ import_fields_args = {
156
+ fields: [@index.outdated_sync_field],
157
+ batch_size: DEFAULT_SYNC_BATCH_SIZE,
158
+ typecast: false
159
+ }
160
+ @index.adapter.import_fields(import_fields_args).to_a.flatten(1).each do |data|
164
161
  data[0] = data[0].to_s
165
162
  end
166
163
  else
167
- @type.adapter.import_fields(batch_size: DEFAULT_SYNC_BATCH_SIZE, typecast: false).to_a.flatten(1).map(&:to_s)
164
+ @index.adapter.import_fields(batch_size: DEFAULT_SYNC_BATCH_SIZE, typecast: false).to_a.flatten(1).map(&:to_s)
168
165
  end
169
166
  end
170
167
 
171
168
  def fetch_index_data
172
- if @type.supports_outdated_sync?
173
- @type.pluck(:_id, @type.outdated_sync_field).each do |data|
169
+ if @index.supports_outdated_sync?
170
+ @index.pluck(:_id, @index.outdated_sync_field).each do |data|
174
171
  data[0] = data[0].to_s
175
172
  end
176
173
  else
177
- @type.pluck(:_id).map(&:to_s)
174
+ @index.pluck(:_id).map(&:to_s)
178
175
  end
179
176
  end
180
177
 
181
178
  def data_ids(data)
182
- return data unless @type.supports_outdated_sync?
179
+ return data unless @index.supports_outdated_sync?
180
+
183
181
  data.map(&:first)
184
182
  end
185
183
 
@@ -192,7 +190,12 @@ module Chewy
192
190
  batches = index_data.each_slice(size)
193
191
 
194
192
  ::ActiveRecord::Base.connection.close if defined?(::ActiveRecord::Base)
195
- result = ::Parallel.map(batches, @parallel, &OUTDATED_IDS_WORKER.curry[outdated_sync_field_type, source_data.to_h, @type, batches.size]).flatten(1)
193
+ curried_outdated_ids_worker = OUTDATED_IDS_WORKER.curry[outdated_sync_field_type, source_data.to_h, @index, batches.size]
194
+ result = ::Parallel.map(
195
+ batches,
196
+ @parallel,
197
+ &curried_outdated_ids_worker
198
+ ).flatten(1)
196
199
  ::ActiveRecord::Base.connection.reconnect! if defined?(::ActiveRecord::Base)
197
200
  result
198
201
  end
@@ -203,17 +206,13 @@ module Chewy
203
206
 
204
207
  def outdated_sync_field_type
205
208
  return @outdated_sync_field_type if instance_variable_defined?(:@outdated_sync_field_type)
206
- return unless @type.outdated_sync_field
209
+ return unless @index.outdated_sync_field
207
210
 
208
- mappings = @type.client.indices.get_mapping(
209
- index: @type.index_name,
210
- type: @type.type_name
211
- ).values.first.fetch('mappings', {})
211
+ mappings = @index.client.indices.get_mapping(index: @index.index_name).values.first.fetch('mappings', {})
212
212
 
213
213
  @outdated_sync_field_type = mappings
214
- .fetch(@type.type_name, {})
215
214
  .fetch('properties', {})
216
- .fetch(@type.outdated_sync_field.to_s, {})['type']
215
+ .fetch(@index.outdated_sync_field.to_s, {})['type']
217
216
  rescue Elasticsearch::Transport::Transport::Errors::NotFound
218
217
  nil
219
218
  end
@@ -7,7 +7,7 @@ rescue LoadError
7
7
  end
8
8
 
9
9
  module Chewy
10
- class Type
10
+ class Index
11
11
  module Witchcraft
12
12
  extend ActiveSupport::Concern
13
13
 
@@ -43,10 +43,10 @@ module Chewy
43
43
  class Cauldron
44
44
  attr_reader :locals
45
45
 
46
- # @param type [Chewy::Type] type for composition
46
+ # @param index [Chewy::Index] index for composition
47
47
  # @param fields [Array<Symbol>] restricts the fields for composition
48
- def initialize(type, fields: [])
49
- @type = type
48
+ def initialize(index, fields: [])
49
+ @index = index
50
50
  @locals = []
51
51
  @fields = fields
52
52
  end
@@ -58,9 +58,9 @@ module Chewy
58
58
  private
59
59
 
60
60
  def alicorn
61
- @alicorn ||= class_eval <<-RUBY
61
+ @alicorn ||= singleton_class.class_eval <<-RUBY, __FILE__, __LINE__ + 1
62
62
  -> (locals, object0, crutches) do
63
- #{composed_values(@type.root_object, 0)}
63
+ #{composed_values(@index.root, 0)}
64
64
  end
65
65
  RUBY
66
66
  end
@@ -79,7 +79,9 @@ module Chewy
79
79
  if field.children.present? && !field.multi_field?
80
80
  <<-RUBY
81
81
  (result#{nesting} = #{fetcher}
82
- if result#{nesting}.respond_to?(:to_ary)
82
+ if result#{nesting}.nil?
83
+ nil
84
+ elsif result#{nesting}.respond_to?(:to_ary)
83
85
  result#{nesting}.map do |object#{nesting}|
84
86
  #{composed_values(field, nesting)}
85
87
  end
@@ -139,6 +141,7 @@ module Chewy
139
141
 
140
142
  def non_proc_fields_for(parent, nesting)
141
143
  return [] unless parent
144
+
142
145
  fields = (parent.children || []).reject { |field| field.value.is_a?(Proc) }
143
146
 
144
147
  if nesting.zero? && @fields.present?
@@ -150,6 +153,7 @@ module Chewy
150
153
 
151
154
  def proc_fields_for(parent, nesting)
152
155
  return [] unless parent
156
+
153
157
  fields = (parent.children || []).select { |field| field.value.is_a?(Proc) }
154
158
 
155
159
  if nesting.zero? && @fields.present?
@@ -171,7 +175,7 @@ module Chewy
171
175
  if proc.arity.zero?
172
176
  source = replace_self(source, :"object#{nesting}")
173
177
  source = replace_send(source, :"object#{nesting}")
174
- elsif proc.arity < 0
178
+ elsif proc.arity.negative?
175
179
  raise "Splat arguments are unsupported by witchcraft:\n`#{proc.source}"
176
180
  else
177
181
  (nesting + 1).times do |n|
@@ -190,6 +194,7 @@ module Chewy
190
194
 
191
195
  def exctract_lambdas(node)
192
196
  return unless node.is_a?(Parser::AST::Node)
197
+
193
198
  if node.type == :block && node.children[0].type == :send && node.children[0].to_a == [nil, :lambda]
194
199
  [node.children[2]]
195
200
  else
@@ -212,7 +217,7 @@ module Chewy
212
217
  def replace_send(node, variable)
213
218
  if node.is_a?(Parser::AST::Node)
214
219
  if node.type == :send && node.children[0].nil?
215
- node.updated(nil, [Parser::AST::Node.new(:lvar, [variable]), *node.children[1..-1]])
220
+ node.updated(nil, [Parser::AST::Node.new(:lvar, [variable]), *node.children[1..]])
216
221
  else
217
222
  node.updated(nil, node.children.map { |child| replace_send(child, variable) })
218
223
  end
@@ -251,6 +256,7 @@ module Chewy
251
256
 
252
257
  def binding_variable_list(node)
253
258
  return unless node.is_a?(Parser::AST::Node)
259
+
254
260
  if node.type == :send && node.children[0].nil?
255
261
  node.children[1]
256
262
  else
@@ -1,5 +1,5 @@
1
1
  module Chewy
2
- class Type
2
+ class Index
3
3
  module Wrapper
4
4
  extend ActiveSupport::Concern
5
5
 
@@ -28,7 +28,7 @@ module Chewy
28
28
  def ==(other)
29
29
  return true if super
30
30
 
31
- if other.is_a?(Chewy::Type)
31
+ if other.is_a?(Chewy::Index)
32
32
  self.class == other.class && (respond_to?(:id) ? id == other.id : attributes == other.attributes)
33
33
  elsif other.respond_to?(:id)
34
34
  self.class.adapter.target.is_a?(Class) &&
@@ -41,7 +41,7 @@ module Chewy
41
41
 
42
42
  %w[_id _type _index].each do |name|
43
43
  define_method name do
44
- data[name]
44
+ _data[name]
45
45
  end
46
46
  end
47
47
 
@@ -49,6 +49,8 @@ module Chewy
49
49
  m = method.to_s
50
50
  if (name = highlight_name(m))
51
51
  highlight(name)
52
+ elsif (name = highlight_names(m))
53
+ highlights(name)
52
54
  elsif @attributes.key?(m)
53
55
  @attributes[m]
54
56
  elsif attribute_defined?(m)
@@ -60,7 +62,7 @@ module Chewy
60
62
 
61
63
  def respond_to_missing?(method, include_private = false)
62
64
  m = method.to_s
63
- highlight_name(m) || @attributes.key?(m) || attribute_defined?(m) || super
65
+ highlight_name(m) || highlight_names(m) || @attributes.key?(m) || attribute_defined?(m) || super
64
66
  end
65
67
 
66
68
  private
@@ -69,12 +71,20 @@ module Chewy
69
71
  method.sub(/_highlight\z/, '') if method.end_with?('_highlight')
70
72
  end
71
73
 
74
+ def highlight_names(method)
75
+ method.sub(/_highlights\z/, '') if method.end_with?('_highlights')
76
+ end
77
+
72
78
  def attribute_defined?(attribute)
73
- self.class.root_object && self.class.root_object.children.find { |a| a.name.to_s == attribute }.present?
79
+ self.class.root && self.class.root.children.find { |a| a.name.to_s == attribute }.present?
74
80
  end
75
81
 
76
82
  def highlight(attribute)
77
- _data['highlight'][attribute].first
83
+ _data['highlight'][attribute].first if highlight?(attribute)
84
+ end
85
+
86
+ def highlights(attribute)
87
+ _data['highlight'][attribute] if highlight?(attribute)
78
88
  end
79
89
 
80
90
  def highlight?(attribute)