chewy 0.9.0 → 0.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (265) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +24 -2
  4. data/.rubocop_todo.yml +2 -2
  5. data/.travis.yml +38 -21
  6. data/.yardopts +5 -0
  7. data/Appraisals +55 -27
  8. data/CHANGELOG.md +57 -12
  9. data/Gemfile +14 -10
  10. data/LEGACY_DSL.md +497 -0
  11. data/README.md +249 -515
  12. data/chewy.gemspec +5 -4
  13. data/gemfiles/rails.4.0.activerecord.gemfile +14 -0
  14. data/gemfiles/rails.4.1.activerecord.gemfile +14 -0
  15. data/gemfiles/rails.4.2.activerecord.gemfile +8 -10
  16. data/gemfiles/rails.4.2.mongoid.5.1.gemfile +9 -10
  17. data/gemfiles/rails.5.0.activerecord.gemfile +8 -10
  18. data/gemfiles/rails.5.0.mongoid.6.0.gemfile +15 -0
  19. data/gemfiles/rails.5.1.activerecord.gemfile +15 -0
  20. data/gemfiles/rails.5.1.mongoid.6.1.gemfile +15 -0
  21. data/gemfiles/sequel.4.45.gemfile +11 -0
  22. data/lib/chewy.rb +77 -43
  23. data/lib/chewy/config.rb +44 -7
  24. data/lib/chewy/errors.rb +2 -2
  25. data/lib/chewy/fields/base.rb +39 -32
  26. data/lib/chewy/fields/root.rb +33 -7
  27. data/lib/chewy/index.rb +237 -149
  28. data/lib/chewy/index/actions.rb +85 -28
  29. data/lib/chewy/index/aliases.rb +2 -1
  30. data/lib/chewy/index/settings.rb +9 -5
  31. data/lib/chewy/index/specification.rb +58 -0
  32. data/lib/chewy/journal.rb +40 -92
  33. data/lib/chewy/query.rb +43 -27
  34. data/lib/chewy/query/compose.rb +13 -13
  35. data/lib/chewy/query/criteria.rb +13 -13
  36. data/lib/chewy/query/filters.rb +1 -1
  37. data/lib/chewy/query/loading.rb +1 -1
  38. data/lib/chewy/query/nodes/and.rb +2 -2
  39. data/lib/chewy/query/nodes/bool.rb +1 -1
  40. data/lib/chewy/query/nodes/equal.rb +2 -2
  41. data/lib/chewy/query/nodes/exists.rb +1 -1
  42. data/lib/chewy/query/nodes/has_relation.rb +2 -2
  43. data/lib/chewy/query/nodes/match_all.rb +1 -1
  44. data/lib/chewy/query/nodes/missing.rb +1 -1
  45. data/lib/chewy/query/nodes/not.rb +2 -2
  46. data/lib/chewy/query/nodes/or.rb +2 -2
  47. data/lib/chewy/query/nodes/prefix.rb +1 -1
  48. data/lib/chewy/query/nodes/query.rb +2 -2
  49. data/lib/chewy/query/nodes/range.rb +4 -4
  50. data/lib/chewy/query/nodes/regexp.rb +4 -4
  51. data/lib/chewy/query/nodes/script.rb +3 -3
  52. data/lib/chewy/query/pagination.rb +10 -1
  53. data/lib/chewy/railtie.rb +1 -0
  54. data/lib/chewy/rake_helper.rb +265 -48
  55. data/lib/chewy/rspec/update_index.rb +30 -22
  56. data/lib/chewy/search.rb +78 -21
  57. data/lib/chewy/search/loader.rb +83 -0
  58. data/lib/chewy/{query → search}/pagination/kaminari.rb +13 -5
  59. data/lib/chewy/search/pagination/will_paginate.rb +41 -0
  60. data/lib/chewy/search/parameters.rb +150 -0
  61. data/lib/chewy/search/parameters/aggs.rb +16 -0
  62. data/lib/chewy/search/parameters/concerns/bool_storage.rb +24 -0
  63. data/lib/chewy/search/parameters/concerns/hash_storage.rb +23 -0
  64. data/lib/chewy/search/parameters/concerns/integer_storage.rb +14 -0
  65. data/lib/chewy/search/parameters/concerns/query_storage.rb +237 -0
  66. data/lib/chewy/search/parameters/concerns/string_array_storage.rb +23 -0
  67. data/lib/chewy/search/parameters/concerns/string_storage.rb +14 -0
  68. data/lib/chewy/search/parameters/docvalue_fields.rb +12 -0
  69. data/lib/chewy/search/parameters/explain.rb +16 -0
  70. data/lib/chewy/search/parameters/filter.rb +47 -0
  71. data/lib/chewy/search/parameters/highlight.rb +16 -0
  72. data/lib/chewy/search/parameters/indices_boost.rb +52 -0
  73. data/lib/chewy/search/parameters/limit.rb +17 -0
  74. data/lib/chewy/search/parameters/load.rb +32 -0
  75. data/lib/chewy/search/parameters/min_score.rb +16 -0
  76. data/lib/chewy/search/parameters/none.rb +27 -0
  77. data/lib/chewy/search/parameters/offset.rb +17 -0
  78. data/lib/chewy/search/parameters/order.rb +64 -0
  79. data/lib/chewy/search/parameters/post_filter.rb +19 -0
  80. data/lib/chewy/search/parameters/preference.rb +16 -0
  81. data/lib/chewy/search/parameters/profile.rb +16 -0
  82. data/lib/chewy/search/parameters/query.rb +19 -0
  83. data/lib/chewy/search/parameters/request_cache.rb +27 -0
  84. data/lib/chewy/search/parameters/rescore.rb +29 -0
  85. data/lib/chewy/search/parameters/script_fields.rb +16 -0
  86. data/lib/chewy/search/parameters/search_after.rb +20 -0
  87. data/lib/chewy/search/parameters/search_type.rb +16 -0
  88. data/lib/chewy/search/parameters/source.rb +73 -0
  89. data/lib/chewy/search/parameters/storage.rb +95 -0
  90. data/lib/chewy/search/parameters/stored_fields.rb +63 -0
  91. data/lib/chewy/search/parameters/suggest.rb +16 -0
  92. data/lib/chewy/search/parameters/terminate_after.rb +16 -0
  93. data/lib/chewy/search/parameters/timeout.rb +16 -0
  94. data/lib/chewy/search/parameters/track_scores.rb +16 -0
  95. data/lib/chewy/search/parameters/types.rb +20 -0
  96. data/lib/chewy/search/parameters/version.rb +16 -0
  97. data/lib/chewy/search/query_proxy.rb +257 -0
  98. data/lib/chewy/search/request.rb +1021 -0
  99. data/lib/chewy/search/response.rb +119 -0
  100. data/lib/chewy/search/scoping.rb +50 -0
  101. data/lib/chewy/search/scrolling.rb +136 -0
  102. data/lib/chewy/stash.rb +70 -0
  103. data/lib/chewy/strategy.rb +10 -3
  104. data/lib/chewy/strategy/active_job.rb +1 -0
  105. data/lib/chewy/strategy/atomic.rb +1 -3
  106. data/lib/chewy/strategy/bypass.rb +1 -1
  107. data/lib/chewy/strategy/resque.rb +1 -0
  108. data/lib/chewy/strategy/shoryuken.rb +40 -0
  109. data/lib/chewy/strategy/sidekiq.rb +13 -3
  110. data/lib/chewy/type.rb +29 -7
  111. data/lib/chewy/type/actions.rb +26 -2
  112. data/lib/chewy/type/adapter/active_record.rb +44 -29
  113. data/lib/chewy/type/adapter/base.rb +27 -7
  114. data/lib/chewy/type/adapter/mongoid.rb +18 -7
  115. data/lib/chewy/type/adapter/object.rb +187 -26
  116. data/lib/chewy/type/adapter/orm.rb +59 -32
  117. data/lib/chewy/type/adapter/sequel.rb +32 -16
  118. data/lib/chewy/type/import.rb +145 -191
  119. data/lib/chewy/type/import/bulk_builder.rb +122 -0
  120. data/lib/chewy/type/import/bulk_request.rb +76 -0
  121. data/lib/chewy/type/import/journal_builder.rb +45 -0
  122. data/lib/chewy/type/import/routine.rb +138 -0
  123. data/lib/chewy/type/mapping.rb +11 -1
  124. data/lib/chewy/type/observe.rb +1 -1
  125. data/lib/chewy/type/syncer.rb +220 -0
  126. data/lib/chewy/type/witchcraft.rb +27 -13
  127. data/lib/chewy/type/wrapper.rb +28 -2
  128. data/lib/chewy/version.rb +1 -1
  129. data/lib/tasks/chewy.rake +84 -26
  130. data/spec/chewy/config_spec.rb +82 -1
  131. data/spec/chewy/fields/base_spec.rb +147 -112
  132. data/spec/chewy/fields/root_spec.rb +75 -18
  133. data/spec/chewy/fields/time_fields_spec.rb +2 -3
  134. data/spec/chewy/index/actions_spec.rb +180 -50
  135. data/spec/chewy/index/aliases_spec.rb +2 -2
  136. data/spec/chewy/index/settings_spec.rb +67 -38
  137. data/spec/chewy/index/specification_spec.rb +160 -0
  138. data/spec/chewy/index_spec.rb +57 -66
  139. data/spec/chewy/journal_spec.rb +149 -54
  140. data/spec/chewy/minitest/helpers_spec.rb +4 -4
  141. data/spec/chewy/minitest/search_index_receiver_spec.rb +1 -1
  142. data/spec/chewy/query/criteria_spec.rb +179 -179
  143. data/spec/chewy/query/filters_spec.rb +15 -15
  144. data/spec/chewy/query/loading_spec.rb +22 -20
  145. data/spec/chewy/query/nodes/and_spec.rb +2 -2
  146. data/spec/chewy/query/nodes/bool_spec.rb +4 -4
  147. data/spec/chewy/query/nodes/equal_spec.rb +19 -19
  148. data/spec/chewy/query/nodes/exists_spec.rb +6 -6
  149. data/spec/chewy/query/nodes/has_child_spec.rb +19 -19
  150. data/spec/chewy/query/nodes/has_parent_spec.rb +19 -19
  151. data/spec/chewy/query/nodes/missing_spec.rb +5 -5
  152. data/spec/chewy/query/nodes/not_spec.rb +3 -2
  153. data/spec/chewy/query/nodes/or_spec.rb +2 -2
  154. data/spec/chewy/query/nodes/prefix_spec.rb +5 -5
  155. data/spec/chewy/query/nodes/query_spec.rb +2 -2
  156. data/spec/chewy/query/nodes/range_spec.rb +18 -18
  157. data/spec/chewy/query/nodes/raw_spec.rb +1 -1
  158. data/spec/chewy/query/nodes/regexp_spec.rb +14 -14
  159. data/spec/chewy/query/nodes/script_spec.rb +4 -4
  160. data/spec/chewy/query/pagination/kaminari_spec.rb +3 -55
  161. data/spec/chewy/query/pagination/will_paginate_spec.rb +5 -0
  162. data/spec/chewy/query/pagination_spec.rb +25 -21
  163. data/spec/chewy/query_spec.rb +501 -560
  164. data/spec/chewy/rake_helper_spec.rb +368 -0
  165. data/spec/chewy/repository_spec.rb +4 -4
  166. data/spec/chewy/rspec/update_index_spec.rb +89 -56
  167. data/spec/chewy/runtime_spec.rb +2 -2
  168. data/spec/chewy/search/loader_spec.rb +117 -0
  169. data/spec/chewy/search/pagination/kaminari_examples.rb +71 -0
  170. data/spec/chewy/search/pagination/kaminari_spec.rb +17 -0
  171. data/spec/chewy/search/pagination/will_paginate_examples.rb +63 -0
  172. data/spec/chewy/search/pagination/will_paginate_spec.rb +17 -0
  173. data/spec/chewy/search/parameters/aggs_spec.rb +5 -0
  174. data/spec/chewy/search/parameters/bool_storage_examples.rb +53 -0
  175. data/spec/chewy/search/parameters/docvalue_fields_spec.rb +5 -0
  176. data/spec/chewy/search/parameters/explain_spec.rb +5 -0
  177. data/spec/chewy/search/parameters/filter_spec.rb +5 -0
  178. data/spec/chewy/search/parameters/hash_storage_examples.rb +59 -0
  179. data/spec/chewy/search/parameters/highlight_spec.rb +5 -0
  180. data/spec/chewy/search/parameters/indices_boost_spec.rb +83 -0
  181. data/spec/chewy/search/parameters/integer_storage_examples.rb +32 -0
  182. data/spec/chewy/search/parameters/limit_spec.rb +5 -0
  183. data/spec/chewy/search/parameters/load_spec.rb +60 -0
  184. data/spec/chewy/search/parameters/min_score_spec.rb +32 -0
  185. data/spec/chewy/search/parameters/none_spec.rb +5 -0
  186. data/spec/chewy/search/parameters/offset_spec.rb +5 -0
  187. data/spec/chewy/search/parameters/order_spec.rb +65 -0
  188. data/spec/chewy/search/parameters/post_filter_spec.rb +5 -0
  189. data/spec/chewy/search/parameters/preference_spec.rb +5 -0
  190. data/spec/chewy/search/parameters/profile_spec.rb +5 -0
  191. data/spec/chewy/search/parameters/query_spec.rb +5 -0
  192. data/spec/chewy/search/parameters/query_storage_examples.rb +388 -0
  193. data/spec/chewy/search/parameters/request_cache_spec.rb +67 -0
  194. data/spec/chewy/search/parameters/rescore_spec.rb +62 -0
  195. data/spec/chewy/search/parameters/script_fields_spec.rb +5 -0
  196. data/spec/chewy/search/parameters/search_after_spec.rb +32 -0
  197. data/spec/chewy/search/parameters/search_type_spec.rb +5 -0
  198. data/spec/chewy/search/parameters/source_spec.rb +156 -0
  199. data/spec/chewy/search/parameters/storage_spec.rb +60 -0
  200. data/spec/chewy/search/parameters/stored_fields_spec.rb +126 -0
  201. data/spec/chewy/search/parameters/string_array_storage_examples.rb +63 -0
  202. data/spec/chewy/search/parameters/string_storage_examples.rb +32 -0
  203. data/spec/chewy/search/parameters/suggest_spec.rb +5 -0
  204. data/spec/chewy/search/parameters/terminate_after_spec.rb +5 -0
  205. data/spec/chewy/search/parameters/timeout_spec.rb +5 -0
  206. data/spec/chewy/search/parameters/track_scores_spec.rb +5 -0
  207. data/spec/chewy/search/parameters/types_spec.rb +5 -0
  208. data/spec/chewy/search/parameters/version_spec.rb +5 -0
  209. data/spec/chewy/search/parameters_spec.rb +130 -0
  210. data/spec/chewy/search/query_proxy_spec.rb +68 -0
  211. data/spec/chewy/search/request_spec.rb +669 -0
  212. data/spec/chewy/search/response_spec.rb +192 -0
  213. data/spec/chewy/search/scrolling_spec.rb +169 -0
  214. data/spec/chewy/search_spec.rb +13 -6
  215. data/spec/chewy/stash_spec.rb +95 -0
  216. data/spec/chewy/strategy/active_job_spec.rb +6 -0
  217. data/spec/chewy/strategy/resque_spec.rb +6 -0
  218. data/spec/chewy/strategy/shoryuken_spec.rb +64 -0
  219. data/spec/chewy/strategy/sidekiq_spec.rb +8 -0
  220. data/spec/chewy/strategy_spec.rb +6 -6
  221. data/spec/chewy/type/actions_spec.rb +29 -10
  222. data/spec/chewy/type/adapter/active_record_spec.rb +203 -91
  223. data/spec/chewy/type/adapter/mongoid_spec.rb +112 -54
  224. data/spec/chewy/type/adapter/object_spec.rb +101 -28
  225. data/spec/chewy/type/adapter/sequel_spec.rb +149 -82
  226. data/spec/chewy/type/import/bulk_builder_spec.rb +279 -0
  227. data/spec/chewy/type/import/bulk_request_spec.rb +102 -0
  228. data/spec/chewy/type/import/journal_builder_spec.rb +95 -0
  229. data/spec/chewy/type/import/routine_spec.rb +110 -0
  230. data/spec/chewy/type/import_spec.rb +350 -271
  231. data/spec/chewy/type/mapping_spec.rb +54 -18
  232. data/spec/chewy/type/observe_spec.rb +5 -1
  233. data/spec/chewy/type/syncer_spec.rb +123 -0
  234. data/spec/chewy/type/witchcraft_spec.rb +45 -29
  235. data/spec/chewy/type/wrapper_spec.rb +63 -23
  236. data/spec/chewy/type_spec.rb +28 -7
  237. data/spec/chewy_spec.rb +75 -7
  238. data/spec/spec_helper.rb +5 -2
  239. data/spec/support/active_record.rb +5 -1
  240. data/spec/support/class_helpers.rb +0 -14
  241. data/spec/support/mongoid.rb +15 -3
  242. data/spec/support/sequel.rb +6 -1
  243. metadata +198 -37
  244. data/gemfiles/rails.3.2.activerecord.gemfile +0 -16
  245. data/gemfiles/rails.3.2.activerecord.kaminari.gemfile +0 -15
  246. data/gemfiles/rails.3.2.activerecord.will_paginate.gemfile +0 -15
  247. data/gemfiles/rails.4.2.activerecord.kaminari.gemfile +0 -16
  248. data/gemfiles/rails.4.2.activerecord.will_paginate.gemfile +0 -16
  249. data/gemfiles/rails.4.2.mongoid.4.0.gemfile +0 -16
  250. data/gemfiles/rails.4.2.mongoid.4.0.kaminari.gemfile +0 -15
  251. data/gemfiles/rails.4.2.mongoid.4.0.will_paginate.gemfile +0 -15
  252. data/gemfiles/rails.4.2.mongoid.5.1.kaminari.gemfile +0 -15
  253. data/gemfiles/rails.4.2.mongoid.5.1.will_paginate.gemfile +0 -15
  254. data/gemfiles/rails.5.0.activerecord.kaminari.gemfile +0 -16
  255. data/gemfiles/rails.5.0.activerecord.will_paginate.gemfile +0 -16
  256. data/gemfiles/sequel.4.38.gemfile +0 -14
  257. data/lib/chewy/journal/apply.rb +0 -31
  258. data/lib/chewy/journal/clean.rb +0 -24
  259. data/lib/chewy/journal/entry.rb +0 -83
  260. data/lib/chewy/journal/query.rb +0 -87
  261. data/lib/chewy/query/pagination/will_paginate.rb +0 -27
  262. data/lib/chewy/query/scoping.rb +0 -20
  263. data/spec/chewy/journal/apply_spec.rb +0 -120
  264. data/spec/chewy/journal/entry_spec.rb +0 -237
  265. data/spec/chewy/query/pagination/will_paginage_spec.rb +0 -59
@@ -52,17 +52,18 @@ module Chewy
52
52
  # Suffixed index names might be used for zero-downtime mapping change, for example.
53
53
  # Description: (http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/).
54
54
  #
55
- def create!(*args)
56
- options = args.extract_options!.reverse_merge!(alias: true)
57
- name = build_index_name(suffix: args.first)
55
+ def create!(suffix = nil, **options)
56
+ options.reverse_merge!(alias: true)
57
+ general_name = index_name
58
+ suffixed_name = index_name(suffix: suffix)
58
59
 
59
60
  if Chewy::Runtime.version >= 1.1
60
- body = index_params
61
- body[:aliases] = { index_name => {} } if options[:alias] && name != index_name
62
- result = client.indices.create(index: name, body: body)
61
+ body = specification_hash
62
+ body[:aliases] = {general_name => {}} if options[:alias] && suffixed_name != general_name
63
+ result = client.indices.create(index: suffixed_name, body: body)
63
64
  else
64
- result = client.indices.create(index: name, body: index_params)
65
- result &&= client.indices.put_alias(index: name, name: index_name) if options[:alias] && name != index_name
65
+ result = client.indices.create(index: suffixed_name, body: specification_hash)
66
+ result &&= client.indices.put_alias(index: suffixed_name, name: general_name) if options[:alias] && name != index_name
66
67
  end
67
68
 
68
69
  Chewy.wait_for_status if result
@@ -78,7 +79,7 @@ module Chewy
78
79
  # UsersIndex.delete '01-2014' # deletes `users_01-2014` index
79
80
  #
80
81
  def delete(suffix = nil)
81
- result = client.indices.delete index: build_index_name(suffix: suffix)
82
+ result = client.indices.delete index: index_name(suffix: suffix)
82
83
  Chewy.wait_for_status if result
83
84
  result
84
85
  # es-ruby >= 1.0.10 handles Elasticsearch::Transport::Transport::Errors::NotFound
@@ -137,7 +138,7 @@ module Chewy
137
138
  #
138
139
  # See [import.rb](lib/chewy/type/import.rb) for more details.
139
140
  #
140
- [:import, :import!].each do |method|
141
+ %i[import import!].each do |method|
141
142
  class_eval <<-METHOD, __FILE__, __LINE__ + 1
142
143
  def #{method} options = {}
143
144
  objects = options.reject { |k, v| !type_names.map(&:to_sym).include?(k) }
@@ -149,33 +150,89 @@ module Chewy
149
150
  METHOD
150
151
  end
151
152
 
152
- # Deletes, creates and imports data to the index.
153
- # Returns import result
153
+ # Deletes, creates and imports data to the index. Returns the
154
+ # import result. If index name suffix is passed as the first
155
+ # argument - performs zero-downtime index resetting.
154
156
  #
155
- # UsersIndex.reset!
156
- #
157
- # If index name suffix passed as the first argument - performs
158
- # zero-downtime index resetting (described here:
159
- # http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/).
160
- #
161
- # UsersIndex.reset! Time.now.to_i, journal: true
157
+ # It also applies journal if anything was journaled during the
158
+ # reset.
162
159
  #
163
- def reset!(suffix = nil, journal: false)
164
- if suffix.present? && (indexes = self.indexes).present?
160
+ # @example
161
+ # UsersIndex.reset!
162
+ # UsersIndex.reset! Time.now.to_i
163
+ #
164
+ # @see http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime
165
+ # @param suffix [String] a suffix for the newly created index
166
+ # @param apply_journal [true, false] if true, journal is applied after the import is completed
167
+ # @param journal [true, false] journalig is switched off for import during reset by default
168
+ # @param import_options [Hash] options, passed to the import call
169
+ # @return [true, false] false in case of errors
170
+ def reset!(suffix = nil, apply_journal: true, journal: false, **import_options)
171
+ result = if suffix.present?
172
+ start_time = Time.now
173
+ indexes = self.indexes
165
174
  create! suffix, alias: false
166
- result = import suffix: suffix, journal: journal
167
- client.indices.update_aliases body: { actions: [
175
+
176
+ general_name = index_name
177
+ suffixed_name = index_name(suffix: suffix)
178
+
179
+ optimize_index_settings suffixed_name
180
+ result = import import_options.merge(suffix: suffix, journal: journal, refresh: !Chewy.reset_disable_refresh_interval)
181
+ original_index_settings suffixed_name
182
+
183
+ delete if indexes.blank?
184
+ client.indices.update_aliases body: {actions: [
168
185
  *indexes.map do |index|
169
- { remove: { index: index, alias: index_name } }
186
+ {remove: {index: index, alias: general_name}}
170
187
  end,
171
- { add: { index: build_index_name(suffix: suffix), alias: index_name } }
172
- ] }
188
+ {add: {index: suffixed_name, alias: general_name}}
189
+ ]}
173
190
  client.indices.delete index: indexes if indexes.present?
191
+
192
+ self.journal.apply(start_time, **import_options) if apply_journal
174
193
  result
175
194
  else
176
- purge! suffix
177
- import journal: journal
195
+ purge!
196
+ import import_options.merge(journal: journal)
178
197
  end
198
+
199
+ specification.lock!
200
+ result
201
+ end
202
+
203
+ # A {Chewy::Journal} instance for the particular index
204
+ #
205
+ # @return [Chewy::Journal] journal instance
206
+ def journal
207
+ @journal ||= Chewy::Journal.new(self)
208
+ end
209
+
210
+ private
211
+
212
+ def optimize_index_settings(index_name)
213
+ settings = {}
214
+ settings[:refresh_interval] = -1 if Chewy.reset_disable_refresh_interval
215
+ settings[:number_of_replicas] = 0 if Chewy.reset_no_replicas
216
+ update_settings index_name, settings: settings if settings.any?
217
+ end
218
+
219
+ def original_index_settings(index_name)
220
+ settings = {}
221
+ if Chewy.reset_disable_refresh_interval
222
+ settings.merge! index_settings(:refresh_interval)
223
+ settings[:refresh_interval] = '1s' if settings.empty?
224
+ end
225
+ settings.merge! index_settings(:number_of_replicas) if Chewy.reset_no_replicas
226
+ update_settings index_name, settings: settings if settings.any?
227
+ end
228
+
229
+ def update_settings(index_name, **options)
230
+ client.indices.put_settings index: index_name, body: {index: options[:settings]}
231
+ end
232
+
233
+ def index_settings(setting_name)
234
+ return {} unless settings_hash.key?(:settings) || settings_hash[:settings].key?(:index)
235
+ settings_hash[:settings][:index].slice(setting_name)
179
236
  end
180
237
  end
181
238
  end
@@ -11,7 +11,8 @@ module Chewy
11
11
  end
12
12
 
13
13
  def aliases
14
- client.indices.get_alias(index: index_name, name: '*')[index_name].try(:[], 'aliases').try(:keys) || []
14
+ name = index_name
15
+ client.indices.get_alias(index: name, name: '*')[name].try(:[], 'aliases').try(:keys) || []
15
16
  rescue Elasticsearch::Transport::Transport::Errors::NotFound
16
17
  []
17
18
  end
@@ -23,20 +23,23 @@ module Chewy
23
23
  # might be used as well.
24
24
  #
25
25
  class Settings
26
- def initialize(params = {})
26
+ def initialize(params = {}, &block)
27
27
  @params = params
28
+ @proc_params = block
28
29
  end
29
30
 
30
31
  def to_hash
31
32
  settings = @params.deep_symbolize_keys
33
+ settings.merge!((@proc_params.call || {}).deep_symbolize_keys) if @proc_params
32
34
 
33
35
  settings[:analysis] = resolve_analysis(settings[:analysis]) if settings[:analysis]
36
+
34
37
  if settings[:index] || Chewy.configuration[:index]
35
38
  settings[:index] = (Chewy.configuration[:index] || {})
36
39
  .deep_merge((settings[:index] || {}).deep_symbolize_keys)
37
40
  end
38
41
 
39
- settings.present? ? { settings: settings } : {}
42
+ settings.present? ? {settings: settings} : {}
40
43
  end
41
44
 
42
45
  private
@@ -44,14 +47,15 @@ module Chewy
44
47
  def resolve_analysis(analysis)
45
48
  analyzer = resolve(analysis[:analyzer], Chewy.analyzers)
46
49
 
47
- options = [:tokenizer, :filter, :char_filter].each.with_object({}) do |type, result|
50
+ options = %i[tokenizer filter char_filter].each.with_object({}) do |type, result|
48
51
  dependencies = collect_dependencies(type, analyzer)
49
52
  resolved = resolve(dependencies.push(analysis[type]), Chewy.send(type.to_s.pluralize))
50
53
  result.merge!(type => resolved) if resolved.present?
51
54
  end
52
55
 
53
56
  options[:analyzer] = analyzer if analyzer.present?
54
- options
57
+ analysis = analysis.except(:analyzer, :tokenizer, :filter, :char_filter)
58
+ analysis.merge(options)
55
59
  end
56
60
 
57
61
  def collect_dependencies(type, analyzer)
@@ -66,7 +70,7 @@ module Chewy
66
70
  else
67
71
  name_or_hash = name_or_hash.to_sym
68
72
  resolved = repository[name_or_hash]
69
- resolved ? { name_or_hash => resolved } : {}
73
+ resolved ? {name_or_hash => resolved} : {}
70
74
  end
71
75
  result.merge!(options)
72
76
  end
@@ -0,0 +1,58 @@
1
+ module Chewy
2
+ class Index
3
+ # Index specification is a combination of index settings and
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.
10
+ #
11
+ # @see Chewy::Stash
12
+ class Specification
13
+ # @see Chewy::Index.specification
14
+ # @param index [Chewy::Index] Just a chewy index
15
+ def initialize(index)
16
+ @index = index
17
+ end
18
+
19
+ # Stores the current index specification to the `Chewy::Stash`
20
+ # as json.
21
+ #
22
+ # @raise [Chewy::ImportFailed] if something went wrong
23
+ # @return [true] if everything is fine
24
+ def lock!
25
+ Chewy::Stash::Specification.import!([
26
+ id: @index.derivable_name,
27
+ value: current.to_json
28
+ ], journal: false)
29
+ end
30
+
31
+ # Returns the last locked specification as ruby hash. Returns
32
+ # empty hash if nothing is stored yet.
33
+ #
34
+ # @return [Hash] hash produced with JSON parser
35
+ def locked
36
+ filter = {ids: {values: [@index.derivable_name]}}
37
+ JSON.parse(Chewy::Stash::Specification.filter(filter).first.try!(:value) || '{}')
38
+ end
39
+
40
+ # Simply returns `Chewy::Index.specification_hash`, but
41
+ # prepared for JSON with `as_json` method. This means all the
42
+ # keys are strings and there are only values of types handled in JSON.
43
+ #
44
+ # @see Chewy::Index.specification_hash
45
+ # @return [Hash] a JSON-ready hash
46
+ def current
47
+ @index.specification_hash.as_json
48
+ end
49
+
50
+ # Compares previously locked and current specifications.
51
+ #
52
+ # @return [true, false] the result of comparison
53
+ def changed?
54
+ current != locked
55
+ end
56
+ end
57
+ end
58
+ end
@@ -1,107 +1,55 @@
1
- require 'chewy/journal/entry'
2
- require 'chewy/journal/query'
3
- require 'chewy/journal/apply'
4
- require 'chewy/journal/clean'
5
-
6
1
  module Chewy
2
+ # A class to perform journal-related actions for the specified indexes/types.
3
+ #
4
+ # @example
5
+ # journal = Chewy::Journal.new('places#city', UsersIndex)
6
+ # journal.apply(20.minutes.ago)
7
+ # journal.clean
8
+ #
7
9
  class Journal
8
- JOURNAL_MAPPING = {
9
- journal: {
10
- properties: {
11
- index_name: { type: 'string', index: 'not_analyzed' },
12
- type_name: { type: 'string', index: 'not_analyzed' },
13
- action: { type: 'string', index: 'not_analyzed' },
14
- object_ids: { type: 'string', index: 'not_analyzed' },
15
- created_at: { type: 'date' }
16
- }
17
- }
18
- }.freeze
19
-
20
- def initialize(index)
21
- @records = []
22
- @index = index
10
+ # @param only [Array<String, Chewy::Index, Chewy::Type>] indexes/types or even string references to perform actions on
11
+ def initialize(*only)
12
+ @only = only
23
13
  end
24
14
 
25
- def add(action_objects)
26
- @records +=
27
- action_objects.map do |action, objects|
28
- {
29
- index_name: @index.derivable_index_name,
30
- type_name: @index.type_name,
31
- action: action,
32
- object_ids: identify(objects),
33
- created_at: Time.now.to_i
34
- }
15
+ # Applies all changes that were done since the specified time to the
16
+ # specified indexes/types.
17
+ #
18
+ # @param since_time [Time, DateTime] timestamp from which changes will be applied
19
+ # @param retries [Integer] maximum number of attempts to make journal empty, 10 by default
20
+ # @return [Integer] the amount of journal entries found
21
+ def apply(since_time, retries: 10, **import_options)
22
+ stage = 1
23
+ since_time -= 1
24
+ count = 0
25
+ while stage <= retries
26
+ entries = Chewy::Stash::Journal.entries(since_time, only: @only).to_a.presence or break
27
+ count += entries.size
28
+ groups = reference_groups(entries)
29
+ ActiveSupport::Notifications.instrument 'apply_journal.chewy', stage: stage, groups: groups
30
+ groups.each do |type, references|
31
+ type.import(references, import_options.merge(journal: false))
35
32
  end
36
- end
37
-
38
- def bulk_body
39
- @records.map do |record|
40
- {
41
- create: {
42
- _index: self.class.index_name,
43
- _type: self.class.type_name,
44
- data: record
45
- }
46
- }
33
+ stage += 1
34
+ since_time = entries.map(&:created_at).max
47
35
  end
36
+ count
48
37
  end
49
38
 
50
- def any_records?
51
- @records.any?
39
+ # Cleans journal for the specified indexes/types.
40
+ #
41
+ # @param until_time [Time, DateTime] time to clean up until it
42
+ # @return [Hash] delete_by_query ES API call result
43
+ def clean(until_time = nil)
44
+ Chewy::Stash::Journal.clean(until_time, only: @only)
52
45
  end
53
46
 
54
47
  private
55
48
 
56
- def identify(objects)
57
- @index.adapter.identify(objects)
58
- end
59
-
60
- class << self
61
- def exists?
62
- Chewy.client.indices.exists? index: index_name
63
- end
64
-
65
- def index_name
66
- [
67
- Chewy.configuration[:prefix],
68
- Chewy.configuration[:journal_name] || 'chewy_journal'
69
- ].reject(&:blank?).join('_')
70
- end
71
-
72
- def type_name
73
- JOURNAL_MAPPING.keys.first
74
- end
75
-
76
- def create
77
- return if exists?
78
- Chewy.client.indices.create index: index_name, body: { settings: { index: Chewy.configuration[:index] }, mappings: JOURNAL_MAPPING }
79
- Chewy.wait_for_status
80
- end
81
-
82
- def delete!
83
- delete or raise Elasticsearch::Transport::Transport::Errors::NotFound
84
- end
85
-
86
- def delete
87
- result = Chewy.client.indices.delete index: index_name
88
- Chewy.wait_for_status if result
89
- result
90
- rescue Elasticsearch::Transport::Transport::Errors::NotFound
91
- false
92
- end
93
-
94
- def apply_changes_from(*args)
95
- Apply.since(*args)
96
- end
97
-
98
- def entries_from(*args)
99
- Entry.since(*args)
100
- end
101
-
102
- def clean_until(*args)
103
- Clean.until(*args)
104
- end
49
+ def reference_groups(entries)
50
+ entries.group_by(&:type).map do |type, grouped_entries|
51
+ [type, grouped_entries.map(&:references).inject(:|)]
52
+ end.to_h
105
53
  end
106
54
  end
107
55
  end
@@ -1,6 +1,5 @@
1
1
  require 'chewy/query/criteria'
2
2
  require 'chewy/query/filters'
3
- require 'chewy/query/scoping'
4
3
  require 'chewy/query/loading'
5
4
  require 'chewy/query/pagination'
6
5
 
@@ -14,9 +13,18 @@ module Chewy
14
13
  #
15
14
  class Query
16
15
  include Enumerable
17
- include Scoping
18
16
  include Loading
19
17
  include Pagination
18
+ include Chewy::Search::Scoping
19
+
20
+ DELEGATED_METHODS = %i[
21
+ explain query_mode filter_mode post_filter_mode
22
+ timeout limit offset highlight min_score rescore facets script_score
23
+ boost_factor weight random_score field_value_factor decay aggregations
24
+ suggest none strategy query filter post_filter boost_mode
25
+ score_mode order reorder only types delete_all find total
26
+ total_count total_entries unlimited script_fields track_scores preference
27
+ ].to_set.freeze
20
28
 
21
29
  delegate :each, :count, :size, to: :_collection
22
30
  alias_method :to_ary, :to_a
@@ -373,7 +381,7 @@ module Chewy
373
381
  # }]
374
382
  # } } }
375
383
  def script_score(script, options = {})
376
- scoring = { script_score: { script: script }.merge(options) }
384
+ scoring = {script_score: {script: script}.merge(options)}
377
385
  chain { criteria.update_scores scoring }
378
386
  end
379
387
 
@@ -441,7 +449,7 @@ module Chewy
441
449
  # }]
442
450
  # } } }
443
451
  def random_score(seed = Time.now, options = {})
444
- scoring = options.merge(random_score: { seed: seed.to_i })
452
+ scoring = options.merge(random_score: {seed: seed.to_i})
445
453
  chain { criteria.update_scores scoring }
446
454
  end
447
455
 
@@ -510,8 +518,8 @@ module Chewy
510
518
  def decay(function, field, options = {})
511
519
  field_options = options.extract!(:origin, :scale, :offset, :decay).delete_if { |_, v| v.nil? }
512
520
  scoring = options.merge(function => {
513
- field => field_options
514
- })
521
+ field => field_options
522
+ })
515
523
  chain { criteria.update_scores scoring }
516
524
  end
517
525
 
@@ -540,8 +548,8 @@ module Chewy
540
548
  @_named_aggs ||= _build_named_aggs
541
549
  @_fully_qualified_named_aggs ||= _build_fqn_aggs
542
550
  if params
543
- params = { params => @_named_aggs[params] } if params.is_a?(Symbol)
544
- params = { params => _get_fully_qualified_named_agg(params) } if params.is_a?(String) && params =~ /\A\S+#\S+\.\S+\z/
551
+ params = {params => @_named_aggs[params]} if params.is_a?(Symbol)
552
+ params = {params => _get_fully_qualified_named_agg(params)} if params.is_a?(String) && params =~ /\A\S+#\S+\.\S+\z/
545
553
  chain { criteria.update_aggregations params }
546
554
  else
547
555
  _response['aggregations'] || {}
@@ -605,7 +613,7 @@ module Chewy
605
613
  end
606
614
  end
607
615
 
608
- # Marks the criteria as having zero records. This scope always returns empty array
616
+ # Marks the criteria as having zero documents. This scope always returns empty array
609
617
  # without touching the elasticsearch server.
610
618
  # All the chained calls of methods don't affect the result
611
619
  #
@@ -898,10 +906,10 @@ module Chewy
898
906
  end
899
907
 
900
908
  # Sets <tt>search_type</tt> for request.
901
- # For instance, one can use <tt>search_type=count</tt> to fetch only total count of records or to fetch only aggregations without fetching records.
909
+ # For instance, one can use <tt>search_type=count</tt> to fetch only total count of documents or to fetch only aggregations without fetching documents.
902
910
  #
903
911
  # scope = UsersIndex.search_type(:count)
904
- # scope.count == 0 # no records actually fetched
912
+ # scope.count == 0 # no documents actually fetched
905
913
  # scope.total == 10 # but we know a total count of them
906
914
  #
907
915
  # scope = UsersIndex.aggs(max_age: { max: { field: 'age' } }).search_type(:count)
@@ -924,7 +932,7 @@ module Chewy
924
932
  chain { criteria.merge!(other.criteria) }
925
933
  end
926
934
 
927
- # Deletes all records matching a query.
935
+ # Deletes all documents matching a query.
928
936
  #
929
937
  # UsersIndex.delete_all
930
938
  # UsersIndex.filter{ age <= 42 }.delete_all
@@ -932,20 +940,31 @@ module Chewy
932
940
  # UsersIndex::User.filter{ age <= 42 }.delete_all
933
941
  #
934
942
  def delete_all
935
- if Runtime.version > '2.0'
943
+ if Runtime.version >= '2.0'
936
944
  plugins = Chewy.client.nodes.info(plugins: true)['nodes'].values.map { |item| item['plugins'] }.flatten
937
945
  raise PluginMissing, 'install delete-by-query plugin' unless plugins.find { |item| item['name'] == 'delete-by-query' }
938
946
  end
947
+
939
948
  request = chain { criteria.update_options simple: true }.send(:_request)
949
+
940
950
  ActiveSupport::Notifications.instrument 'delete_query.chewy',
941
951
  request: request, indexes: _indexes, types: _types,
942
952
  index: _indexes.one? ? _indexes.first : _indexes,
943
953
  type: _types.one? ? _types.first : _types do
944
- Chewy.client.delete_by_query(request)
945
- end
954
+ if Runtime.version >= '2.0'
955
+ path = Elasticsearch::API::Utils.__pathify(
956
+ Elasticsearch::API::Utils.__listify(request[:index]),
957
+ Elasticsearch::API::Utils.__listify(request[:type]),
958
+ '/_query'
959
+ )
960
+ Chewy.client.perform_request(Elasticsearch::API::HTTP_DELETE, path, {}, request[:body]).body
961
+ else
962
+ Chewy.client.delete_by_query(request)
963
+ end
964
+ end
946
965
  end
947
966
 
948
- # Find all records matching a query.
967
+ # Find all documents matching a query.
949
968
  #
950
969
  # UsersIndex.find(42)
951
970
  # UsersIndex.filter{ age <= 42 }.find(42)
@@ -1008,8 +1027,8 @@ module Chewy
1008
1027
 
1009
1028
  protected
1010
1029
 
1011
- def initialize_clone(other)
1012
- @criteria = other.criteria.clone
1030
+ def initialize_clone(origin)
1031
+ @criteria = origin.criteria.clone
1013
1032
  reset
1014
1033
  end
1015
1034
 
@@ -1026,7 +1045,7 @@ module Chewy
1026
1045
  def _request
1027
1046
  @_request ||= begin
1028
1047
  request = criteria.request_body
1029
- request[:index] = _indexes.map(&:index_name)
1048
+ request[:index] = _indexes_hash.keys
1030
1049
  request[:type] = _types.map(&:type_name)
1031
1050
  request
1032
1051
  end
@@ -1048,14 +1067,7 @@ module Chewy
1048
1067
 
1049
1068
  def _results
1050
1069
  @_results ||= (criteria.none? || _response == {} ? [] : _response['hits']['hits']).map do |hit|
1051
- attributes = (hit['_source'] || {})
1052
- .reverse_merge(id: hit['_id'])
1053
- .merge!(_score: hit['_score'])
1054
- .merge!(_explanation: hit['_explanation'])
1055
-
1056
- wrapper = _derive_index(hit['_index']).type(hit['_type']).new(attributes)
1057
- wrapper._data = hit
1058
- wrapper
1070
+ _derive_type(hit['_index'], hit['_type']).build(hit)
1059
1071
  end
1060
1072
  end
1061
1073
 
@@ -1070,6 +1082,10 @@ module Chewy
1070
1082
  end
1071
1083
  end
1072
1084
 
1085
+ def _derive_type(index, type)
1086
+ (@types_cache ||= {})[[index, type]] ||= _derive_index(index).type(type)
1087
+ end
1088
+
1073
1089
  def _derive_index(index_name)
1074
1090
  (@derive_index ||= {})[index_name] ||= _indexes_hash[index_name] ||
1075
1091
  _indexes_hash[_indexes_hash.keys.sort_by(&:length).reverse.detect { |name| index_name.start_with?(name) }]