chewy 0.10.0 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (157) hide show
  1. checksums.yaml +5 -5
  2. data/.circleci/config.yml +240 -0
  3. data/.rubocop.yml +25 -25
  4. data/Appraisals +12 -10
  5. data/CHANGELOG.md +252 -263
  6. data/Gemfile +5 -1
  7. data/LICENSE.txt +1 -1
  8. data/README.md +142 -78
  9. data/chewy.gemspec +10 -12
  10. data/gemfiles/{rails.4.2.mongoid.5.1.gemfile → rails.5.2.activerecord.gemfile} +6 -4
  11. data/gemfiles/{rails.4.2.activerecord.gemfile → rails.5.2.mongoid.6.4.gemfile} +6 -4
  12. data/gemfiles/{rails.4.0.activerecord.gemfile → rails.6.0.activerecord.gemfile} +6 -3
  13. data/gemfiles/rails.6.1.activerecord.gemfile +19 -0
  14. data/gemfiles/sequel.4.45.gemfile +2 -2
  15. data/lib/chewy.rb +2 -1
  16. data/lib/chewy/backports/duplicable.rb +1 -1
  17. data/lib/chewy/config.rb +10 -39
  18. data/lib/chewy/fields/base.rb +40 -28
  19. data/lib/chewy/fields/root.rb +18 -11
  20. data/lib/chewy/index.rb +3 -1
  21. data/lib/chewy/index/actions.rb +27 -15
  22. data/lib/chewy/index/settings.rb +2 -0
  23. data/lib/chewy/index/specification.rb +12 -10
  24. data/lib/chewy/minitest/helpers.rb +6 -6
  25. data/lib/chewy/minitest/search_index_receiver.rb +17 -17
  26. data/lib/chewy/multi_search.rb +62 -0
  27. data/lib/chewy/railtie.rb +4 -4
  28. data/lib/chewy/rake_helper.rb +5 -5
  29. data/lib/chewy/rspec/update_index.rb +3 -5
  30. data/lib/chewy/search.rb +4 -11
  31. data/lib/chewy/search/loader.rb +1 -1
  32. data/lib/chewy/search/pagination/will_paginate.rb +4 -2
  33. data/lib/chewy/search/parameters.rb +24 -6
  34. data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
  35. data/lib/chewy/search/parameters/concerns/query_storage.rb +4 -3
  36. data/lib/chewy/search/parameters/indices.rb +123 -0
  37. data/lib/chewy/search/parameters/none.rb +1 -3
  38. data/lib/chewy/search/request.rb +100 -74
  39. data/lib/chewy/search/scrolling.rb +7 -6
  40. data/lib/chewy/stash.rb +30 -21
  41. data/lib/chewy/strategy/active_job.rb +1 -1
  42. data/lib/chewy/strategy/atomic.rb +1 -1
  43. data/lib/chewy/strategy/sidekiq.rb +1 -1
  44. data/lib/chewy/type.rb +5 -2
  45. data/lib/chewy/type/adapter/active_record.rb +1 -1
  46. data/lib/chewy/type/adapter/base.rb +9 -9
  47. data/lib/chewy/type/adapter/mongoid.rb +2 -4
  48. data/lib/chewy/type/adapter/orm.rb +7 -4
  49. data/lib/chewy/type/adapter/sequel.rb +5 -7
  50. data/lib/chewy/type/crutch.rb +1 -1
  51. data/lib/chewy/type/import.rb +13 -11
  52. data/lib/chewy/type/import/bulk_builder.rb +1 -1
  53. data/lib/chewy/type/import/bulk_request.rb +4 -2
  54. data/lib/chewy/type/import/journal_builder.rb +3 -3
  55. data/lib/chewy/type/import/routine.rb +3 -3
  56. data/lib/chewy/type/mapping.rb +42 -36
  57. data/lib/chewy/type/observe.rb +16 -12
  58. data/lib/chewy/type/syncer.rb +15 -14
  59. data/lib/chewy/type/witchcraft.rb +11 -7
  60. data/lib/chewy/type/wrapper.rb +14 -4
  61. data/lib/chewy/version.rb +1 -1
  62. data/lib/sequel/plugins/chewy_observe.rb +4 -19
  63. data/migration_guide.md +18 -0
  64. data/spec/chewy/config_spec.rb +16 -21
  65. data/spec/chewy/fields/base_spec.rb +70 -70
  66. data/spec/chewy/fields/root_spec.rb +56 -9
  67. data/spec/chewy/index/actions_spec.rb +63 -7
  68. data/spec/chewy/index/specification_spec.rb +25 -16
  69. data/spec/chewy/index_spec.rb +75 -45
  70. data/spec/chewy/journal_spec.rb +33 -29
  71. data/spec/chewy/minitest/search_index_receiver_spec.rb +11 -9
  72. data/spec/chewy/multi_search_spec.rb +85 -0
  73. data/spec/chewy/rake_helper_spec.rb +123 -95
  74. data/spec/chewy/rspec/update_index_spec.rb +47 -46
  75. data/spec/chewy/runtime_spec.rb +2 -2
  76. data/spec/chewy/search/pagination/kaminari_spec.rb +7 -3
  77. data/spec/chewy/search/pagination/will_paginate_spec.rb +9 -3
  78. data/spec/chewy/search/parameters/indices_spec.rb +190 -0
  79. data/spec/chewy/search/parameters/none_spec.rb +1 -1
  80. data/spec/chewy/search/parameters_spec.rb +21 -4
  81. data/spec/chewy/search/request_spec.rb +101 -70
  82. data/spec/chewy/search/response_spec.rb +27 -17
  83. data/spec/chewy/search/scrolling_spec.rb +25 -16
  84. data/spec/chewy/search_spec.rb +49 -35
  85. data/spec/chewy/stash_spec.rb +15 -13
  86. data/spec/chewy/strategy/active_job_spec.rb +15 -2
  87. data/spec/chewy/strategy/shoryuken_spec.rb +8 -2
  88. data/spec/chewy/strategy/sidekiq_spec.rb +6 -2
  89. data/spec/chewy/type/adapter/active_record_spec.rb +16 -4
  90. data/spec/chewy/type/import/bulk_builder_spec.rb +9 -94
  91. data/spec/chewy/type/import/journal_builder_spec.rb +17 -15
  92. data/spec/chewy/type/import_spec.rb +6 -0
  93. data/spec/chewy/type/mapping_spec.rb +51 -18
  94. data/spec/chewy/type/observe_spec.rb +4 -4
  95. data/spec/chewy/type/witchcraft_spec.rb +31 -0
  96. data/spec/chewy/type/wrapper_spec.rb +3 -1
  97. data/spec/chewy_spec.rb +0 -7
  98. data/spec/spec_helper.rb +5 -1
  99. data/spec/support/active_record.rb +20 -0
  100. metadata +46 -116
  101. data/.travis.yml +0 -53
  102. data/LEGACY_DSL.md +0 -497
  103. data/gemfiles/rails.4.1.activerecord.gemfile +0 -14
  104. data/gemfiles/rails.5.0.activerecord.gemfile +0 -15
  105. data/gemfiles/rails.5.0.mongoid.6.0.gemfile +0 -15
  106. data/gemfiles/rails.5.1.activerecord.gemfile +0 -15
  107. data/gemfiles/rails.5.1.mongoid.6.1.gemfile +0 -15
  108. data/lib/chewy/query.rb +0 -1098
  109. data/lib/chewy/query/compose.rb +0 -68
  110. data/lib/chewy/query/criteria.rb +0 -191
  111. data/lib/chewy/query/filters.rb +0 -227
  112. data/lib/chewy/query/loading.rb +0 -111
  113. data/lib/chewy/query/nodes/and.rb +0 -25
  114. data/lib/chewy/query/nodes/base.rb +0 -17
  115. data/lib/chewy/query/nodes/bool.rb +0 -34
  116. data/lib/chewy/query/nodes/equal.rb +0 -34
  117. data/lib/chewy/query/nodes/exists.rb +0 -20
  118. data/lib/chewy/query/nodes/expr.rb +0 -28
  119. data/lib/chewy/query/nodes/field.rb +0 -110
  120. data/lib/chewy/query/nodes/has_child.rb +0 -15
  121. data/lib/chewy/query/nodes/has_parent.rb +0 -15
  122. data/lib/chewy/query/nodes/has_relation.rb +0 -59
  123. data/lib/chewy/query/nodes/match_all.rb +0 -11
  124. data/lib/chewy/query/nodes/missing.rb +0 -20
  125. data/lib/chewy/query/nodes/not.rb +0 -25
  126. data/lib/chewy/query/nodes/or.rb +0 -25
  127. data/lib/chewy/query/nodes/prefix.rb +0 -19
  128. data/lib/chewy/query/nodes/query.rb +0 -20
  129. data/lib/chewy/query/nodes/range.rb +0 -63
  130. data/lib/chewy/query/nodes/raw.rb +0 -15
  131. data/lib/chewy/query/nodes/regexp.rb +0 -35
  132. data/lib/chewy/query/nodes/script.rb +0 -20
  133. data/lib/chewy/query/pagination.rb +0 -25
  134. data/spec/chewy/query/criteria_spec.rb +0 -700
  135. data/spec/chewy/query/filters_spec.rb +0 -201
  136. data/spec/chewy/query/loading_spec.rb +0 -124
  137. data/spec/chewy/query/nodes/and_spec.rb +0 -12
  138. data/spec/chewy/query/nodes/bool_spec.rb +0 -14
  139. data/spec/chewy/query/nodes/equal_spec.rb +0 -32
  140. data/spec/chewy/query/nodes/exists_spec.rb +0 -18
  141. data/spec/chewy/query/nodes/has_child_spec.rb +0 -59
  142. data/spec/chewy/query/nodes/has_parent_spec.rb +0 -59
  143. data/spec/chewy/query/nodes/match_all_spec.rb +0 -11
  144. data/spec/chewy/query/nodes/missing_spec.rb +0 -16
  145. data/spec/chewy/query/nodes/not_spec.rb +0 -13
  146. data/spec/chewy/query/nodes/or_spec.rb +0 -12
  147. data/spec/chewy/query/nodes/prefix_spec.rb +0 -16
  148. data/spec/chewy/query/nodes/query_spec.rb +0 -12
  149. data/spec/chewy/query/nodes/range_spec.rb +0 -32
  150. data/spec/chewy/query/nodes/raw_spec.rb +0 -11
  151. data/spec/chewy/query/nodes/regexp_spec.rb +0 -43
  152. data/spec/chewy/query/nodes/script_spec.rb +0 -15
  153. data/spec/chewy/query/pagination/kaminari_spec.rb +0 -5
  154. data/spec/chewy/query/pagination/will_paginate_spec.rb +0 -5
  155. data/spec/chewy/query/pagination_spec.rb +0 -39
  156. data/spec/chewy/query_spec.rb +0 -636
  157. data/spec/chewy/search/parameters/indices_boost_spec.rb +0 -83
@@ -13,7 +13,7 @@ module Chewy
13
13
  # * performs the bulk request;
14
14
  # * composes new leftovers bulk for the next iteration basing on the response errors if `update_failover` is true;
15
15
  # * appends the rest of unfixable errors to the instance level errors array.
16
- # 4. Perform the request for the last leftovers bulk if present using {#process_leftovers}.
16
+ # 4. Perform the request for the last leftovers bulk if present using {#extract_leftovers}.
17
17
  # 3. Return the result errors array.
18
18
  #
19
19
  # At the moment, it tries to restore only from the partial document update errors in cases
@@ -64,9 +64,9 @@ module Chewy
64
64
  # Creates the journal index and the type corresponding index if necessary.
65
65
  # @return [Object] whatever
66
66
  def create_indexes!
67
- Chewy::Stash.create if @options[:journal]
67
+ Chewy::Stash::Journal.create if @options[:journal]
68
68
  return if Chewy.configuration[:skip_index_creation_on_import]
69
- @type.index.create!(@bulk_options.slice(:suffix)) unless @type.index.exists?
69
+ @type.index.create!(**@bulk_options.slice(:suffix)) unless @type.index.exists?
70
70
  end
71
71
 
72
72
  # The main process method. Converts passed objects to thr bulk request body,
@@ -17,10 +17,11 @@ module Chewy
17
17
  # definition. Use it only if you need to pass options for root
18
18
  # object mapping, such as `date_detection` or `dynamic_date_formats`
19
19
  #
20
+ # @example
20
21
  # class UsersIndex < Chewy::Index
21
22
  # define_type User do
22
23
  # # root object defined implicitly and optionless for current type
23
- # field :full_name, type: 'string'
24
+ # field :full_name, type: 'keyword'
24
25
  # end
25
26
  # end
26
27
  #
@@ -28,32 +29,37 @@ module Chewy
28
29
  # define_type Car do
29
30
  # # explicit root definition with additional options
30
31
  # root dynamic_date_formats: ['yyyy-MM-dd'] do
31
- # field :model_name, type: 'string'
32
+ # field :model_name, type: 'keyword'
32
33
  # end
33
34
  # end
34
35
  # end
35
36
  #
36
- def root(options = {}, &block)
37
- raise 'Root is already defined' if root_object
38
- build_root(options, &block)
37
+ def root(**options)
38
+ self.root_object ||= Chewy::Fields::Root.new(type_name, **Chewy.default_root_options.merge(options))
39
+ root_object.update_options!(**options)
40
+ yield if block_given?
41
+ root_object
39
42
  end
40
43
 
41
44
  # Defines mapping field for current type
42
45
  #
46
+ # @example
43
47
  # class UsersIndex < Chewy::Index
44
48
  # define_type User do
45
49
  # # passing all the options to field definition:
46
- # field :full_name, type: 'string', analyzer: 'special'
50
+ # field :full_name, analyzer: 'special'
47
51
  # end
48
52
  # end
49
53
  #
50
54
  # The `type` is optional and defaults to `string` if not defined:
51
55
  #
56
+ # @example
52
57
  # field :full_name
53
58
  #
54
59
  # Also, multiple fields might be defined with one call and
55
60
  # with the same options:
56
61
  #
62
+ # @example
57
63
  # field :first_name, :last_name, analyzer: 'special'
58
64
  #
59
65
  # The only special option in the field definition
@@ -61,31 +67,35 @@ module Chewy
61
67
  # method will be called for the indexed object. Also
62
68
  # `:value` might be a proc or indexed object method name:
63
69
  #
70
+ # @example
64
71
  # class User < ActiveRecord::Base
65
72
  # def user_full_name
66
73
  # [first_name, last_name].join(' ')
67
74
  # end
68
75
  # end
69
76
  #
70
- # field :full_name, type: 'string', value: :user_full_name
77
+ # field :full_name, type: 'keyword', value: :user_full_name
71
78
  #
72
79
  # The proc evaluates inside the indexed object context if
73
80
  # its arity is 0 and in present contexts if there is an argument:
74
81
  #
75
- # field :full_name, type: 'string', value: -> { [first_name, last_name].join(' ') }
82
+ # @example
83
+ # field :full_name, type: 'keyword', value: -> { [first_name, last_name].join(' ') }
76
84
  #
77
85
  # separator = ' '
78
- # field :full_name, type: 'string', value: ->(user) { [user.first_name, user.last_name].join(separator) }
86
+ # field :full_name, type: 'keyword', value: ->(user) { [user.first_name, user.last_name].join(separator) }
79
87
  #
80
88
  # If array was returned as value - it will be put in index as well.
81
89
  #
82
- # field :tags, type: 'string', value: -> { tags.map(&:name) }
90
+ # @example
91
+ # field :tags, type: 'keyword', value: -> { tags.map(&:name) }
83
92
  #
84
93
  # Fields supports nesting in case of `object` field type. If
85
94
  # `user.quiz` will return an array of objects, then result index content
86
95
  # will be an array of hashes, if `user.quiz` is not a collection association
87
96
  # then just values hash will be put in the index.
88
97
  #
98
+ # @example
89
99
  # field :quiz do
90
100
  # field :question, :answer
91
101
  # field :score, type: 'integer'
@@ -93,6 +103,7 @@ module Chewy
93
103
  #
94
104
  # Nested fields are composed from nested objects:
95
105
  #
106
+ # @example
96
107
  # field :name, value: -> { name_translations } do
97
108
  # field :ru, value: ->(name) { name['ru'] }
98
109
  # field :en, value: ->(name) { name['en'] }
@@ -101,32 +112,31 @@ module Chewy
101
112
  # Of course it is possible to define object fields contents dynamically
102
113
  # but make sure evaluation proc returns hash:
103
114
  #
115
+ # @example
104
116
  # field :name, type: 'object', value: -> { name_translations }
105
117
  #
106
118
  # The special case is multi_field. If type options and block are
107
119
  # both present field is treated as a multi-field. In that case field
108
120
  # composition changes satisfy elasticsearch rules:
109
121
  #
110
- # field :full_name, type: 'string', analyzer: 'name', value: ->{ full_name.try(:strip) } do
122
+ # @example
123
+ # field :full_name, type: 'text', analyzer: 'name', value: ->{ full_name.try(:strip) } do
111
124
  # field :sorted, analyzer: 'sorted'
112
125
  # end
113
126
  #
114
- def field(*args, &block)
115
- options = args.extract_options!
116
- build_root
117
-
127
+ def field(*args, **options, &block)
118
128
  if args.size > 1
119
- args.map { |name| field(name, options) }
129
+ args.map { |name| field(name, **options) }
120
130
  else
121
- expand_nested(Chewy::Fields::Base.new(args.first, options), &block)
131
+ expand_nested(Chewy::Fields::Base.new(args.first, **options), &block)
122
132
  end
123
133
  end
124
134
 
125
135
  # Defines an aggregation that can be bound to a query or filter
126
136
  #
127
- # Suppose that a user has posts and each post has ratings
128
- # avg_post_rating is the mean of all ratings
129
- #
137
+ # @example
138
+ # # Suppose that a user has posts and each post has ratings
139
+ # # avg_post_rating is the mean of all ratings
130
140
  # class UsersIndex < Chewy::Index
131
141
  # define_type User do
132
142
  # field :posts do
@@ -139,72 +149,68 @@ module Chewy
139
149
  # end
140
150
  # end
141
151
  def agg(name, &block)
142
- build_root
143
152
  self._agg_defs = _agg_defs.merge(name => block)
144
153
  end
145
154
  alias_method :aggregation, :agg
146
155
 
147
156
  # Defines dynamic template in mapping root objects
148
157
  #
158
+ # @example
149
159
  # class CarsIndex < Chewy::Index
150
160
  # define_type Car do
151
- # template 'model.*', type: 'string', analyzer: 'special'
161
+ # template 'model.*', type: 'text', analyzer: 'special'
152
162
  # field 'model', type: 'object' # here we can put { de: 'Der Mercedes', en: 'Mercedes' }
153
163
  # # and template will be applyed to this field
154
164
  # end
155
165
  # end
156
166
  #
157
167
  # Name for each template is generated with the following
158
- # rule: "template_#{dynamic_templates.size + 1}".
168
+ # rule: `template_#!{dynamic_templates.size + 1}`.
159
169
  #
170
+ # @example Templates
160
171
  # template 'tit*', mapping_hash
161
172
  # template 'title.*', mapping_hash # dot in template causes "path_match" using
162
173
  # template /tit.+/, mapping_hash # using "match_pattern": "regexp"
163
174
  # template /title\..+/, mapping_hash # "\." - escaped dot causes "path_match" using
164
- # template /tit.+/, 'string', mapping_hash # "match_mapping_type" as the optionsl second argument
175
+ # template /tit.+/, type: 'text', mapping_hash # "match_mapping_type" as the optionsl second argument
165
176
  # template template42: {match: 'hello*', mapping: {type: 'object'}} # or even pass a template as is
166
177
  #
167
178
  def template(*args)
168
- build_root.dynamic_template(*args)
179
+ root.dynamic_template(*args)
169
180
  end
170
181
  alias_method :dynamic_template, :template
171
182
 
172
183
  # Returns compiled mappings hash for current type
173
184
  #
174
185
  def mappings_hash
175
- root_object ? root_object.mappings_hash : {}
186
+ root.mappings_hash[type_name.to_sym].present? ? root.mappings_hash : {}
176
187
  end
177
188
 
178
189
  # Check whether the type has outdated_sync_field defined with a simple value.
179
190
  #
180
191
  # @return [true, false]
181
192
  def supports_outdated_sync?
182
- updated_at_field = root_object.child_hash[outdated_sync_field] if root_object && outdated_sync_field
193
+ updated_at_field = root.child_hash[outdated_sync_field] if outdated_sync_field
183
194
  !!updated_at_field && updated_at_field.value.nil?
184
195
  end
185
196
 
186
197
  private
187
198
 
188
- def expand_nested(field, &block)
199
+ def expand_nested(field)
200
+ @_current_field ||= root
201
+
189
202
  if @_current_field
190
203
  field.parent = @_current_field
191
204
  @_current_field.children.push(field)
192
205
  end
193
206
 
194
- return unless block
207
+ return unless block_given?
195
208
 
196
209
  previous_field = @_current_field
197
210
  @_current_field = field
198
211
  yield
199
212
  @_current_field = previous_field
200
213
  end
201
-
202
- def build_root(options = {}, &block)
203
- return root_object if root_object
204
- self.root_object = Chewy::Fields::Root.new(type_name, Chewy.default_root_options.merge(options))
205
- expand_nested(root_object, &block)
206
- @_current_field = root_object
207
- end
208
214
  end
209
215
  end
210
216
  end
@@ -9,14 +9,6 @@ module Chewy
9
9
  method = args.first
10
10
 
11
11
  proc do
12
- backreference = if method && method.to_s == 'self'
13
- self
14
- elsif method
15
- send(method)
16
- else
17
- instance_eval(&block)
18
- end
19
-
20
12
  reference = if type_name.is_a?(Proc)
21
13
  if type_name.arity.zero?
22
14
  instance_exec(&type_name)
@@ -27,7 +19,19 @@ module Chewy
27
19
  type_name
28
20
  end
29
21
 
30
- Chewy.derive_type(reference).update_index(backreference, options)
22
+ type = Chewy.derive_type(reference)
23
+
24
+ next if Chewy.strategy.current.name == :bypass
25
+
26
+ backreference = if method && method.to_s == 'self'
27
+ self
28
+ elsif method
29
+ send(method)
30
+ else
31
+ instance_eval(&block)
32
+ end
33
+
34
+ type.update_index(backreference, options)
31
35
  end
32
36
  end
33
37
 
@@ -59,10 +63,10 @@ module Chewy
59
63
  update_proc = Observe.update_proc(type_name, *args, &block)
60
64
 
61
65
  if Chewy.use_after_commit_callbacks
62
- after_commit(callback_options, &update_proc)
66
+ after_commit(**callback_options, &update_proc)
63
67
  else
64
- after_save(callback_options, &update_proc)
65
- after_destroy(callback_options, &update_proc)
68
+ after_save(**callback_options, &update_proc)
69
+ after_destroy(**callback_options, &update_proc)
66
70
  end
67
71
  end
68
72
  end
@@ -2,7 +2,7 @@ module Chewy
2
2
  class Type
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
@@ -28,12 +28,13 @@ module Chewy
28
28
  class Syncer
29
29
  DEFAULT_SYNC_BATCH_SIZE = 20_000
30
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, index_data|
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
32
33
  index_data.each_with_object([]) do |(id, index_sync_value), result|
33
34
  next unless source_data_hash[id]
34
35
 
35
36
  outdated = if outdated_sync_field_type == 'date'
36
- !Chewy::Type::Syncer.dates_equal(typecast_date(source_data_hash[id]), DateTime.iso8601(index_sync_value))
37
+ !Chewy::Type::Syncer.dates_equal(typecast_date(source_data_hash[id]), Time.iso8601(index_sync_value))
37
38
  else
38
39
  source_data_hash[id] != index_sync_value
39
40
  end
@@ -41,21 +42,22 @@ module Chewy
41
42
  result.push(id) if outdated
42
43
  end
43
44
  end
44
- SOURCE_OR_INDEX_DATA_WORKER = lambda do |syncer, type|
45
- result = case type
45
+ SOURCE_OR_INDEX_DATA_WORKER = lambda do |syncer, type, kind|
46
+ ::Process.setproctitle("chewy [#{type}]: sync fetching data (#{kind})")
47
+ result = case kind
46
48
  when :source
47
49
  syncer.send(:fetch_source_data)
48
50
  when :index
49
51
  syncer.send(:fetch_index_data)
50
52
  end
51
- {type => result}
53
+ {kind => result}
52
54
  end
53
55
 
54
56
  def self.typecast_date(string)
55
57
  if string.is_a?(String) && (match = ISO_DATETIME.match(string))
56
58
  microsec = (match[7].to_r * 1_000_000).to_i
57
59
  date = "#{match[1]}-#{match[2]}-#{match[3]}T#{match[4]}:#{match[5]}:#{match[6]}.#{format('%06d', microsec)}+00:00"
58
- DateTime.iso8601(date)
60
+ Time.iso8601(date)
59
61
  else
60
62
  string
61
63
  end
@@ -143,7 +145,7 @@ module Chewy
143
145
  @source_and_index_data ||= begin
144
146
  if @parallel
145
147
  ::ActiveRecord::Base.connection.close if defined?(::ActiveRecord::Base)
146
- result = ::Parallel.map(%i[source index], @parallel, &SOURCE_OR_INDEX_DATA_WORKER.curry[self])
148
+ result = ::Parallel.map(%i[source index], @parallel, &SOURCE_OR_INDEX_DATA_WORKER.curry[self, @type])
147
149
  ::ActiveRecord::Base.connection.reconnect! if defined?(::ActiveRecord::Base)
148
150
  if result.first.keys.first == :source
149
151
  [result.first.values.first, result.second.values.first]
@@ -182,7 +184,7 @@ module Chewy
182
184
  end
183
185
 
184
186
  def linear_outdated_ids
185
- OUTDATED_IDS_WORKER.call(outdated_sync_field_type, source_data.to_h, index_data)
187
+ OUTDATED_IDS_WORKER.call(outdated_sync_field_type, source_data.to_h, nil, nil, index_data)
186
188
  end
187
189
 
188
190
  def parallel_outdated_ids
@@ -190,7 +192,7 @@ module Chewy
190
192
  batches = index_data.each_slice(size)
191
193
 
192
194
  ::ActiveRecord::Base.connection.close if defined?(::ActiveRecord::Base)
193
- result = ::Parallel.map(batches, @parallel, &OUTDATED_IDS_WORKER.curry[outdated_sync_field_type, source_data.to_h]).flatten(1)
195
+ result = ::Parallel.map(batches, @parallel, &OUTDATED_IDS_WORKER.curry[outdated_sync_field_type, source_data.to_h, @type, batches.size]).flatten(1)
194
196
  ::ActiveRecord::Base.connection.reconnect! if defined?(::ActiveRecord::Base)
195
197
  result
196
198
  end
@@ -203,10 +205,9 @@ module Chewy
203
205
  return @outdated_sync_field_type if instance_variable_defined?(:@outdated_sync_field_type)
204
206
  return unless @type.outdated_sync_field
205
207
 
206
- mappings = @type.client.indices.get_mapping(
207
- index: @type.index_name,
208
- type: @type.type_name
209
- ).values.first.fetch('mappings', {})
208
+ args = {index: @type.index_name, type: @type.type_name}
209
+ args[:include_type_name] = true if Runtime.version >= '6.7.0'
210
+ mappings = @type.client.indices.get_mapping(**args).values.first.fetch('mappings', {})
210
211
 
211
212
  @outdated_sync_field_type = mappings
212
213
  .fetch(@type.type_name, {})
@@ -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(@type.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
@@ -102,14 +104,16 @@ module Chewy
102
104
  (if #{object}.is_a?(Hash)
103
105
  {
104
106
  #{non_proc_fields.map do |f|
105
- fetcher = "#{object}.has_key?(:#{f.name}) ? #{object}[:#{f.name}] : #{object}['#{f.name}']"
107
+ key_name = f.value.is_a?(Symbol) || f.value.is_a?(String) ? f.value : f.name
108
+ fetcher = "#{object}.has_key?(:#{key_name}) ? #{object}[:#{key_name}] : #{object}['#{key_name}']"
106
109
  "'#{f.name}'.freeze => #{composed_value(f, fetcher, nesting)}"
107
110
  end.join(', ')}
108
111
  }
109
112
  else
110
113
  {
111
114
  #{non_proc_fields.map do |f|
112
- "'#{f.name}'.freeze => #{composed_value(f, "#{object}.#{f.name}", nesting)}"
115
+ method_name = f.value.is_a?(Symbol) || f.value.is_a?(String) ? f.value : f.name
116
+ "'#{f.name}'.freeze => #{composed_value(f, "#{object}.#{method_name}", nesting)}"
113
117
  end.join(', ')}
114
118
  }
115
119
  end)
@@ -137,7 +141,7 @@ module Chewy
137
141
 
138
142
  def non_proc_fields_for(parent, nesting)
139
143
  return [] unless parent
140
- fields = (parent.children || []).reject { |field| field.value && field.value.is_a?(Proc) }
144
+ fields = (parent.children || []).reject { |field| field.value.is_a?(Proc) }
141
145
 
142
146
  if nesting.zero? && @fields.present?
143
147
  fields.select { |f| @fields.include?(f.name) }
@@ -148,7 +152,7 @@ module Chewy
148
152
 
149
153
  def proc_fields_for(parent, nesting)
150
154
  return [] unless parent
151
- fields = (parent.children || []).select { |field| field.value && field.value.is_a?(Proc) }
155
+ fields = (parent.children || []).select { |field| field.value.is_a?(Proc) }
152
156
 
153
157
  if nesting.zero? && @fields.present?
154
158
  fields.select { |f| @fields.include?(f.name) }
@@ -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)