chewy 0.9.0 → 5.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.circleci/config.yml +214 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +41 -19
- data/.rubocop_todo.yml +2 -2
- data/.yardopts +5 -0
- data/Appraisals +58 -28
- data/CHANGELOG.md +153 -12
- data/Gemfile +20 -12
- data/LEGACY_DSL.md +497 -0
- data/LICENSE.txt +1 -1
- data/README.md +338 -528
- data/chewy.gemspec +11 -12
- data/gemfiles/rails.5.2.activerecord.gemfile +17 -0
- data/gemfiles/rails.5.2.mongoid.6.4.gemfile +17 -0
- data/gemfiles/rails.6.0.activerecord.gemfile +17 -0
- data/gemfiles/rails.6.1.activerecord.gemfile +19 -0
- data/gemfiles/ruby3.gemfile +10 -0
- data/gemfiles/sequel.4.45.gemfile +11 -0
- data/lib/chewy.rb +79 -44
- data/lib/chewy/backports/duplicable.rb +1 -1
- data/lib/chewy/config.rb +43 -17
- data/lib/chewy/errors.rb +2 -2
- data/lib/chewy/fields/base.rb +56 -31
- data/lib/chewy/fields/root.rb +44 -11
- data/lib/chewy/index.rb +237 -149
- data/lib/chewy/index/actions.rb +100 -35
- data/lib/chewy/index/aliases.rb +2 -1
- data/lib/chewy/index/settings.rb +11 -5
- data/lib/chewy/index/specification.rb +60 -0
- data/lib/chewy/journal.rb +40 -92
- data/lib/chewy/minitest/helpers.rb +6 -6
- data/lib/chewy/minitest/search_index_receiver.rb +17 -17
- data/lib/chewy/query.rb +182 -122
- data/lib/chewy/query/compose.rb +13 -13
- data/lib/chewy/query/criteria.rb +13 -13
- data/lib/chewy/query/filters.rb +21 -4
- data/lib/chewy/query/loading.rb +1 -2
- data/lib/chewy/query/nodes/and.rb +2 -2
- data/lib/chewy/query/nodes/bool.rb +1 -1
- data/lib/chewy/query/nodes/equal.rb +2 -2
- data/lib/chewy/query/nodes/exists.rb +1 -1
- data/lib/chewy/query/nodes/field.rb +1 -1
- data/lib/chewy/query/nodes/has_relation.rb +2 -2
- data/lib/chewy/query/nodes/match_all.rb +1 -1
- data/lib/chewy/query/nodes/missing.rb +1 -1
- data/lib/chewy/query/nodes/not.rb +2 -2
- data/lib/chewy/query/nodes/or.rb +2 -2
- data/lib/chewy/query/nodes/prefix.rb +1 -1
- data/lib/chewy/query/nodes/query.rb +2 -2
- data/lib/chewy/query/nodes/range.rb +4 -4
- data/lib/chewy/query/nodes/regexp.rb +4 -4
- data/lib/chewy/query/nodes/script.rb +3 -3
- data/lib/chewy/query/pagination.rb +10 -1
- data/lib/chewy/railtie.rb +4 -3
- data/lib/chewy/rake_helper.rb +265 -48
- data/lib/chewy/rspec/update_index.rb +33 -27
- data/lib/chewy/search.rb +79 -26
- data/lib/chewy/search/loader.rb +83 -0
- data/lib/chewy/{query → search}/pagination/kaminari.rb +13 -5
- data/lib/chewy/search/pagination/will_paginate.rb +43 -0
- data/lib/chewy/search/parameters.rb +168 -0
- data/lib/chewy/search/parameters/aggs.rb +16 -0
- data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
- data/lib/chewy/search/parameters/concerns/bool_storage.rb +24 -0
- data/lib/chewy/search/parameters/concerns/hash_storage.rb +23 -0
- data/lib/chewy/search/parameters/concerns/integer_storage.rb +14 -0
- data/lib/chewy/search/parameters/concerns/query_storage.rb +238 -0
- data/lib/chewy/search/parameters/concerns/string_array_storage.rb +23 -0
- data/lib/chewy/search/parameters/concerns/string_storage.rb +14 -0
- data/lib/chewy/search/parameters/docvalue_fields.rb +12 -0
- data/lib/chewy/search/parameters/explain.rb +16 -0
- data/lib/chewy/search/parameters/filter.rb +47 -0
- data/lib/chewy/search/parameters/highlight.rb +16 -0
- data/lib/chewy/search/parameters/indices.rb +123 -0
- data/lib/chewy/search/parameters/indices_boost.rb +52 -0
- data/lib/chewy/search/parameters/limit.rb +17 -0
- data/lib/chewy/search/parameters/load.rb +32 -0
- data/lib/chewy/search/parameters/min_score.rb +16 -0
- data/lib/chewy/search/parameters/none.rb +27 -0
- data/lib/chewy/search/parameters/offset.rb +17 -0
- data/lib/chewy/search/parameters/order.rb +64 -0
- data/lib/chewy/search/parameters/post_filter.rb +19 -0
- data/lib/chewy/search/parameters/preference.rb +16 -0
- data/lib/chewy/search/parameters/profile.rb +16 -0
- data/lib/chewy/search/parameters/query.rb +19 -0
- data/lib/chewy/search/parameters/request_cache.rb +27 -0
- data/lib/chewy/search/parameters/rescore.rb +29 -0
- data/lib/chewy/search/parameters/script_fields.rb +16 -0
- data/lib/chewy/search/parameters/search_after.rb +20 -0
- data/lib/chewy/search/parameters/search_type.rb +16 -0
- data/lib/chewy/search/parameters/source.rb +73 -0
- data/lib/chewy/search/parameters/storage.rb +95 -0
- data/lib/chewy/search/parameters/stored_fields.rb +63 -0
- data/lib/chewy/search/parameters/suggest.rb +16 -0
- data/lib/chewy/search/parameters/terminate_after.rb +16 -0
- data/lib/chewy/search/parameters/timeout.rb +16 -0
- data/lib/chewy/search/parameters/track_scores.rb +16 -0
- data/lib/chewy/search/parameters/types.rb +20 -0
- data/lib/chewy/search/parameters/version.rb +16 -0
- data/lib/chewy/search/query_proxy.rb +257 -0
- data/lib/chewy/search/request.rb +1046 -0
- data/lib/chewy/search/response.rb +119 -0
- data/lib/chewy/search/scoping.rb +50 -0
- data/lib/chewy/search/scrolling.rb +134 -0
- data/lib/chewy/stash.rb +79 -0
- data/lib/chewy/strategy.rb +10 -3
- data/lib/chewy/strategy/active_job.rb +2 -1
- data/lib/chewy/strategy/atomic.rb +2 -4
- data/lib/chewy/strategy/bypass.rb +1 -1
- data/lib/chewy/strategy/resque.rb +1 -0
- data/lib/chewy/strategy/shoryuken.rb +40 -0
- data/lib/chewy/strategy/sidekiq.rb +13 -3
- data/lib/chewy/type.rb +29 -7
- data/lib/chewy/type/actions.rb +26 -2
- data/lib/chewy/type/adapter/active_record.rb +44 -29
- data/lib/chewy/type/adapter/base.rb +27 -7
- data/lib/chewy/type/adapter/mongoid.rb +19 -10
- data/lib/chewy/type/adapter/object.rb +187 -26
- data/lib/chewy/type/adapter/orm.rb +59 -32
- data/lib/chewy/type/adapter/sequel.rb +33 -19
- data/lib/chewy/type/crutch.rb +1 -1
- data/lib/chewy/type/import.rb +146 -191
- data/lib/chewy/type/import/bulk_builder.rb +122 -0
- data/lib/chewy/type/import/bulk_request.rb +78 -0
- data/lib/chewy/type/import/journal_builder.rb +45 -0
- data/lib/chewy/type/import/routine.rb +138 -0
- data/lib/chewy/type/mapping.rb +51 -35
- data/lib/chewy/type/observe.rb +17 -13
- data/lib/chewy/type/syncer.rb +222 -0
- data/lib/chewy/type/witchcraft.rb +32 -16
- data/lib/chewy/type/wrapper.rb +30 -4
- data/lib/chewy/version.rb +1 -1
- data/lib/sequel/plugins/chewy_observe.rb +4 -19
- data/lib/tasks/chewy.rake +84 -26
- data/spec/chewy/config_spec.rb +98 -1
- data/spec/chewy/fields/base_spec.rb +170 -135
- data/spec/chewy/fields/root_spec.rb +124 -20
- data/spec/chewy/fields/time_fields_spec.rb +2 -3
- data/spec/chewy/index/actions_spec.rb +214 -52
- data/spec/chewy/index/aliases_spec.rb +2 -2
- data/spec/chewy/index/settings_spec.rb +67 -38
- data/spec/chewy/index/specification_spec.rb +169 -0
- data/spec/chewy/index_spec.rb +108 -64
- data/spec/chewy/journal_spec.rb +150 -55
- data/spec/chewy/minitest/helpers_spec.rb +4 -4
- data/spec/chewy/minitest/search_index_receiver_spec.rb +1 -1
- data/spec/chewy/query/criteria_spec.rb +179 -179
- data/spec/chewy/query/filters_spec.rb +16 -16
- data/spec/chewy/query/loading_spec.rb +22 -20
- data/spec/chewy/query/nodes/and_spec.rb +2 -2
- data/spec/chewy/query/nodes/bool_spec.rb +4 -4
- data/spec/chewy/query/nodes/equal_spec.rb +19 -19
- data/spec/chewy/query/nodes/exists_spec.rb +6 -6
- data/spec/chewy/query/nodes/has_child_spec.rb +19 -19
- data/spec/chewy/query/nodes/has_parent_spec.rb +19 -19
- data/spec/chewy/query/nodes/missing_spec.rb +5 -5
- data/spec/chewy/query/nodes/not_spec.rb +4 -2
- data/spec/chewy/query/nodes/or_spec.rb +2 -2
- data/spec/chewy/query/nodes/prefix_spec.rb +5 -5
- data/spec/chewy/query/nodes/query_spec.rb +2 -2
- data/spec/chewy/query/nodes/range_spec.rb +18 -18
- data/spec/chewy/query/nodes/raw_spec.rb +1 -1
- data/spec/chewy/query/nodes/regexp_spec.rb +14 -14
- data/spec/chewy/query/nodes/script_spec.rb +4 -4
- data/spec/chewy/query/pagination/kaminari_spec.rb +3 -55
- data/spec/chewy/query/pagination/will_paginate_spec.rb +5 -0
- data/spec/chewy/query/pagination_spec.rb +25 -21
- data/spec/chewy/query_spec.rb +503 -561
- data/spec/chewy/rake_helper_spec.rb +381 -0
- data/spec/chewy/repository_spec.rb +4 -4
- data/spec/chewy/rspec/update_index_spec.rb +89 -56
- data/spec/chewy/runtime_spec.rb +2 -2
- data/spec/chewy/search/loader_spec.rb +117 -0
- data/spec/chewy/search/pagination/kaminari_examples.rb +71 -0
- data/spec/chewy/search/pagination/kaminari_spec.rb +21 -0
- data/spec/chewy/search/pagination/will_paginate_examples.rb +63 -0
- data/spec/chewy/search/pagination/will_paginate_spec.rb +23 -0
- data/spec/chewy/search/parameters/aggs_spec.rb +5 -0
- data/spec/chewy/search/parameters/bool_storage_examples.rb +53 -0
- data/spec/chewy/search/parameters/docvalue_fields_spec.rb +5 -0
- data/spec/chewy/search/parameters/explain_spec.rb +5 -0
- data/spec/chewy/search/parameters/filter_spec.rb +5 -0
- data/spec/chewy/search/parameters/hash_storage_examples.rb +59 -0
- data/spec/chewy/search/parameters/highlight_spec.rb +5 -0
- data/spec/chewy/search/parameters/indices_spec.rb +191 -0
- data/spec/chewy/search/parameters/integer_storage_examples.rb +32 -0
- data/spec/chewy/search/parameters/limit_spec.rb +5 -0
- data/spec/chewy/search/parameters/load_spec.rb +60 -0
- data/spec/chewy/search/parameters/min_score_spec.rb +32 -0
- data/spec/chewy/search/parameters/none_spec.rb +5 -0
- data/spec/chewy/search/parameters/offset_spec.rb +5 -0
- data/spec/chewy/search/parameters/order_spec.rb +65 -0
- data/spec/chewy/search/parameters/post_filter_spec.rb +5 -0
- data/spec/chewy/search/parameters/preference_spec.rb +5 -0
- data/spec/chewy/search/parameters/profile_spec.rb +5 -0
- data/spec/chewy/search/parameters/query_spec.rb +5 -0
- data/spec/chewy/search/parameters/query_storage_examples.rb +388 -0
- data/spec/chewy/search/parameters/request_cache_spec.rb +67 -0
- data/spec/chewy/search/parameters/rescore_spec.rb +62 -0
- data/spec/chewy/search/parameters/script_fields_spec.rb +5 -0
- data/spec/chewy/search/parameters/search_after_spec.rb +32 -0
- data/spec/chewy/search/parameters/search_type_spec.rb +5 -0
- data/spec/chewy/search/parameters/source_spec.rb +156 -0
- data/spec/chewy/search/parameters/storage_spec.rb +60 -0
- data/spec/chewy/search/parameters/stored_fields_spec.rb +126 -0
- data/spec/chewy/search/parameters/string_array_storage_examples.rb +63 -0
- data/spec/chewy/search/parameters/string_storage_examples.rb +32 -0
- data/spec/chewy/search/parameters/suggest_spec.rb +5 -0
- data/spec/chewy/search/parameters/terminate_after_spec.rb +5 -0
- data/spec/chewy/search/parameters/timeout_spec.rb +5 -0
- data/spec/chewy/search/parameters/track_scores_spec.rb +5 -0
- data/spec/chewy/search/parameters/types_spec.rb +5 -0
- data/spec/chewy/search/parameters/version_spec.rb +5 -0
- data/spec/chewy/search/parameters_spec.rb +147 -0
- data/spec/chewy/search/query_proxy_spec.rb +68 -0
- data/spec/chewy/search/request_spec.rb +685 -0
- data/spec/chewy/search/response_spec.rb +198 -0
- data/spec/chewy/search/scrolling_spec.rb +169 -0
- data/spec/chewy/search_spec.rb +33 -16
- data/spec/chewy/stash_spec.rb +95 -0
- data/spec/chewy/strategy/active_job_spec.rb +21 -2
- data/spec/chewy/strategy/resque_spec.rb +6 -0
- data/spec/chewy/strategy/shoryuken_spec.rb +70 -0
- data/spec/chewy/strategy/sidekiq_spec.rb +13 -1
- data/spec/chewy/strategy_spec.rb +6 -6
- data/spec/chewy/type/actions_spec.rb +29 -10
- data/spec/chewy/type/adapter/active_record_spec.rb +203 -91
- data/spec/chewy/type/adapter/mongoid_spec.rb +112 -54
- data/spec/chewy/type/adapter/object_spec.rb +101 -28
- data/spec/chewy/type/adapter/sequel_spec.rb +149 -82
- data/spec/chewy/type/import/bulk_builder_spec.rb +279 -0
- data/spec/chewy/type/import/bulk_request_spec.rb +102 -0
- data/spec/chewy/type/import/journal_builder_spec.rb +95 -0
- data/spec/chewy/type/import/routine_spec.rb +110 -0
- data/spec/chewy/type/import_spec.rb +356 -271
- data/spec/chewy/type/mapping_spec.rb +96 -29
- data/spec/chewy/type/observe_spec.rb +9 -5
- data/spec/chewy/type/syncer_spec.rb +123 -0
- data/spec/chewy/type/witchcraft_spec.rb +61 -29
- data/spec/chewy/type/wrapper_spec.rb +63 -23
- data/spec/chewy/type_spec.rb +28 -7
- data/spec/chewy_spec.rb +75 -7
- data/spec/spec_helper.rb +17 -3
- data/spec/support/active_record.rb +5 -1
- data/spec/support/class_helpers.rb +0 -14
- data/spec/support/mongoid.rb +15 -3
- data/spec/support/sequel.rb +6 -1
- metadata +219 -58
- data/.travis.yml +0 -36
- data/gemfiles/rails.3.2.activerecord.gemfile +0 -16
- data/gemfiles/rails.3.2.activerecord.kaminari.gemfile +0 -15
- data/gemfiles/rails.3.2.activerecord.will_paginate.gemfile +0 -15
- data/gemfiles/rails.4.2.activerecord.gemfile +0 -17
- data/gemfiles/rails.4.2.activerecord.kaminari.gemfile +0 -16
- data/gemfiles/rails.4.2.activerecord.will_paginate.gemfile +0 -16
- data/gemfiles/rails.4.2.mongoid.4.0.gemfile +0 -16
- data/gemfiles/rails.4.2.mongoid.4.0.kaminari.gemfile +0 -15
- data/gemfiles/rails.4.2.mongoid.4.0.will_paginate.gemfile +0 -15
- data/gemfiles/rails.4.2.mongoid.5.1.gemfile +0 -16
- data/gemfiles/rails.4.2.mongoid.5.1.kaminari.gemfile +0 -15
- data/gemfiles/rails.4.2.mongoid.5.1.will_paginate.gemfile +0 -15
- data/gemfiles/rails.5.0.activerecord.gemfile +0 -17
- data/gemfiles/rails.5.0.activerecord.kaminari.gemfile +0 -16
- data/gemfiles/rails.5.0.activerecord.will_paginate.gemfile +0 -16
- data/gemfiles/sequel.4.38.gemfile +0 -14
- data/lib/chewy/journal/apply.rb +0 -31
- data/lib/chewy/journal/clean.rb +0 -24
- data/lib/chewy/journal/entry.rb +0 -83
- data/lib/chewy/journal/query.rb +0 -87
- data/lib/chewy/query/pagination/will_paginate.rb +0 -27
- data/lib/chewy/query/scoping.rb +0 -20
- data/spec/chewy/journal/apply_spec.rb +0 -120
- data/spec/chewy/journal/entry_spec.rb +0 -237
- data/spec/chewy/query/pagination/will_paginage_spec.rb +0 -59
data/lib/chewy/index/actions.rb
CHANGED
@@ -30,8 +30,8 @@ module Chewy
|
|
30
30
|
# Suffixed index names might be used for zero-downtime mapping change, for example.
|
31
31
|
# Description: (http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/).
|
32
32
|
#
|
33
|
-
def create(*args)
|
34
|
-
create!(*args)
|
33
|
+
def create(*args, **kwargs)
|
34
|
+
create!(*args, **kwargs)
|
35
35
|
rescue Elasticsearch::Transport::Transport::Errors::BadRequest
|
36
36
|
false
|
37
37
|
end
|
@@ -52,18 +52,14 @@ 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!(
|
56
|
-
options
|
57
|
-
|
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
|
-
|
61
|
-
|
62
|
-
result = client.indices.create(index: name, body: body)
|
63
|
-
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
|
66
|
-
end
|
60
|
+
body = specification_hash
|
61
|
+
body[:aliases] = {general_name => {}} if options[:alias] && suffixed_name != general_name
|
62
|
+
result = client.indices.create(index: suffixed_name, body: body)
|
67
63
|
|
68
64
|
Chewy.wait_for_status if result
|
69
65
|
result
|
@@ -78,7 +74,13 @@ module Chewy
|
|
78
74
|
# UsersIndex.delete '01-2014' # deletes `users_01-2014` index
|
79
75
|
#
|
80
76
|
def delete(suffix = nil)
|
81
|
-
|
77
|
+
# Verify that the index_name is really the index_name and not an alias.
|
78
|
+
#
|
79
|
+
# "The index parameter in the delete index API no longer accepts alias names.
|
80
|
+
# Instead, it accepts only index names (or wildcards which will expand to matching indices)."
|
81
|
+
# https://www.elastic.co/guide/en/elasticsearch/reference/6.8/breaking-changes-6.0.html#_delete_index_api_resolves_indices_expressions_only_against_indices
|
82
|
+
index_names = client.indices.get_alias(index: index_name(suffix: suffix)).keys
|
83
|
+
result = client.indices.delete index: index_names.join(',')
|
82
84
|
Chewy.wait_for_status if result
|
83
85
|
result
|
84
86
|
# es-ruby >= 1.0.10 handles Elasticsearch::Transport::Transport::Errors::NotFound
|
@@ -137,10 +139,17 @@ module Chewy
|
|
137
139
|
#
|
138
140
|
# See [import.rb](lib/chewy/type/import.rb) for more details.
|
139
141
|
#
|
140
|
-
[
|
142
|
+
%i[import import!].each do |method|
|
141
143
|
class_eval <<-METHOD, __FILE__, __LINE__ + 1
|
142
|
-
def #{method}
|
143
|
-
|
144
|
+
def #{method}(*args)
|
145
|
+
options = args.extract_options!
|
146
|
+
if args.one? && type_names.one?
|
147
|
+
objects = {type_names.first.to_sym => args.first}
|
148
|
+
elsif args.one?
|
149
|
+
fail ArgumentError, "Please pass objects for `#{method}` as a hash with type names"
|
150
|
+
else
|
151
|
+
objects = options.reject { |k, v| !type_names.map(&:to_sym).include?(k) }
|
152
|
+
end
|
144
153
|
types.map do |type|
|
145
154
|
args = [objects[type.type_name.to_sym], options.dup].reject(&:blank?)
|
146
155
|
type.#{method} *args
|
@@ -149,33 +158,89 @@ module Chewy
|
|
149
158
|
METHOD
|
150
159
|
end
|
151
160
|
|
152
|
-
# Deletes, creates and imports data to the index.
|
153
|
-
#
|
154
|
-
#
|
155
|
-
# UsersIndex.reset!
|
161
|
+
# Deletes, creates and imports data to the index. Returns the
|
162
|
+
# import result. If index name suffix is passed as the first
|
163
|
+
# argument - performs zero-downtime index resetting.
|
156
164
|
#
|
157
|
-
#
|
158
|
-
#
|
159
|
-
# http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/).
|
165
|
+
# It also applies journal if anything was journaled during the
|
166
|
+
# reset.
|
160
167
|
#
|
161
|
-
#
|
162
|
-
#
|
163
|
-
|
164
|
-
|
168
|
+
# @example
|
169
|
+
# UsersIndex.reset!
|
170
|
+
# UsersIndex.reset! Time.now.to_i
|
171
|
+
#
|
172
|
+
# @see http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime
|
173
|
+
# @param suffix [String] a suffix for the newly created index
|
174
|
+
# @param apply_journal [true, false] if true, journal is applied after the import is completed
|
175
|
+
# @param journal [true, false] journaling is switched off for import during reset by default
|
176
|
+
# @param import_options [Hash] options, passed to the import call
|
177
|
+
# @return [true, false] false in case of errors
|
178
|
+
def reset!(suffix = nil, apply_journal: true, journal: false, **import_options)
|
179
|
+
result = if suffix.present?
|
180
|
+
start_time = Time.now
|
181
|
+
indexes = self.indexes
|
165
182
|
create! suffix, alias: false
|
166
|
-
|
167
|
-
|
183
|
+
|
184
|
+
general_name = index_name
|
185
|
+
suffixed_name = index_name(suffix: suffix)
|
186
|
+
|
187
|
+
optimize_index_settings suffixed_name
|
188
|
+
result = import import_options.merge(suffix: suffix, journal: journal, refresh: !Chewy.reset_disable_refresh_interval)
|
189
|
+
original_index_settings suffixed_name
|
190
|
+
|
191
|
+
delete if indexes.blank?
|
192
|
+
client.indices.update_aliases body: {actions: [
|
168
193
|
*indexes.map do |index|
|
169
|
-
{
|
194
|
+
{remove: {index: index, alias: general_name}}
|
170
195
|
end,
|
171
|
-
{
|
172
|
-
]
|
196
|
+
{add: {index: suffixed_name, alias: general_name}}
|
197
|
+
]}
|
173
198
|
client.indices.delete index: indexes if indexes.present?
|
199
|
+
|
200
|
+
self.journal.apply(start_time, **import_options) if apply_journal
|
174
201
|
result
|
175
202
|
else
|
176
|
-
purge!
|
177
|
-
import journal: journal
|
203
|
+
purge!
|
204
|
+
import import_options.merge(journal: journal)
|
178
205
|
end
|
206
|
+
|
207
|
+
specification.lock!
|
208
|
+
result
|
209
|
+
end
|
210
|
+
|
211
|
+
# A {Chewy::Journal} instance for the particular index
|
212
|
+
#
|
213
|
+
# @return [Chewy::Journal] journal instance
|
214
|
+
def journal
|
215
|
+
@journal ||= Chewy::Journal.new(self)
|
216
|
+
end
|
217
|
+
|
218
|
+
private
|
219
|
+
|
220
|
+
def optimize_index_settings(index_name)
|
221
|
+
settings = {}
|
222
|
+
settings[:refresh_interval] = -1 if Chewy.reset_disable_refresh_interval
|
223
|
+
settings[:number_of_replicas] = 0 if Chewy.reset_no_replicas
|
224
|
+
update_settings index_name, settings: settings if settings.any?
|
225
|
+
end
|
226
|
+
|
227
|
+
def original_index_settings(index_name)
|
228
|
+
settings = {}
|
229
|
+
if Chewy.reset_disable_refresh_interval
|
230
|
+
settings.merge! index_settings(:refresh_interval)
|
231
|
+
settings[:refresh_interval] = '1s' if settings.empty?
|
232
|
+
end
|
233
|
+
settings.merge! index_settings(:number_of_replicas) if Chewy.reset_no_replicas
|
234
|
+
update_settings index_name, settings: settings if settings.any?
|
235
|
+
end
|
236
|
+
|
237
|
+
def update_settings(index_name, **options)
|
238
|
+
client.indices.put_settings index: index_name, body: {index: options[:settings]}
|
239
|
+
end
|
240
|
+
|
241
|
+
def index_settings(setting_name)
|
242
|
+
return {} unless settings_hash.key?(:settings) && settings_hash[:settings].key?(:index)
|
243
|
+
settings_hash[:settings][:index].slice(setting_name)
|
179
244
|
end
|
180
245
|
end
|
181
246
|
end
|
data/lib/chewy/index/aliases.rb
CHANGED
@@ -11,7 +11,8 @@ module Chewy
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def aliases
|
14
|
-
|
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
|
data/lib/chewy/index/settings.rb
CHANGED
@@ -4,12 +4,14 @@ module Chewy
|
|
4
4
|
# hash. At first, you need to store some analyzers or other
|
5
5
|
# analysis options to the corresponding repository:
|
6
6
|
#
|
7
|
+
# @example
|
7
8
|
# Chewy.analyzer :title_analyzer, type: 'custom', filter: %w(lowercase icu_folding title_nysiis)
|
8
9
|
# Chewy.filter :title_nysiis, type: 'phonetic', encoder: 'nysiis', replace: false
|
9
10
|
#
|
10
11
|
# `title_nysiis` filter here will be expanded automatically when
|
11
12
|
# `title_analyzer` analyser will be used in index settings:
|
12
13
|
#
|
14
|
+
# @example
|
13
15
|
# class ProductsIndex < Chewy::Index
|
14
16
|
# settings analysis: {
|
15
17
|
# analyzer: [
|
@@ -23,20 +25,23 @@ module Chewy
|
|
23
25
|
# might be used as well.
|
24
26
|
#
|
25
27
|
class Settings
|
26
|
-
def initialize(params = {})
|
28
|
+
def initialize(params = {}, &block)
|
27
29
|
@params = params
|
30
|
+
@proc_params = block
|
28
31
|
end
|
29
32
|
|
30
33
|
def to_hash
|
31
34
|
settings = @params.deep_symbolize_keys
|
35
|
+
settings.merge!((@proc_params.call || {}).deep_symbolize_keys) if @proc_params
|
32
36
|
|
33
37
|
settings[:analysis] = resolve_analysis(settings[:analysis]) if settings[:analysis]
|
38
|
+
|
34
39
|
if settings[:index] || Chewy.configuration[:index]
|
35
40
|
settings[:index] = (Chewy.configuration[:index] || {})
|
36
41
|
.deep_merge((settings[:index] || {}).deep_symbolize_keys)
|
37
42
|
end
|
38
43
|
|
39
|
-
settings.present? ? {
|
44
|
+
settings.present? ? {settings: settings} : {}
|
40
45
|
end
|
41
46
|
|
42
47
|
private
|
@@ -44,14 +49,15 @@ module Chewy
|
|
44
49
|
def resolve_analysis(analysis)
|
45
50
|
analyzer = resolve(analysis[:analyzer], Chewy.analyzers)
|
46
51
|
|
47
|
-
options = [
|
52
|
+
options = %i[tokenizer filter char_filter].each.with_object({}) do |type, result|
|
48
53
|
dependencies = collect_dependencies(type, analyzer)
|
49
54
|
resolved = resolve(dependencies.push(analysis[type]), Chewy.send(type.to_s.pluralize))
|
50
55
|
result.merge!(type => resolved) if resolved.present?
|
51
56
|
end
|
52
57
|
|
53
58
|
options[:analyzer] = analyzer if analyzer.present?
|
54
|
-
|
59
|
+
analysis = analysis.except(:analyzer, :tokenizer, :filter, :char_filter)
|
60
|
+
analysis.merge(options)
|
55
61
|
end
|
56
62
|
|
57
63
|
def collect_dependencies(type, analyzer)
|
@@ -66,7 +72,7 @@ module Chewy
|
|
66
72
|
else
|
67
73
|
name_or_hash = name_or_hash.to_sym
|
68
74
|
resolved = repository[name_or_hash]
|
69
|
-
resolved ? {
|
75
|
+
resolved ? {name_or_hash => resolved} : {}
|
70
76
|
end
|
71
77
|
result.merge!(options)
|
72
78
|
end
|
@@ -0,0 +1,60 @@
|
|
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::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
|
+
#
|
11
|
+
# @see Chewy::Stash::Specification
|
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::Specification`
|
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
|
+
specification: Base64.encode64(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
|
+
document = Chewy::Stash::Specification.filter(filter).first
|
38
|
+
return {} unless document
|
39
|
+
JSON.load(Base64.decode64(document.specification)) # rubocop:disable Security/JSONLoad
|
40
|
+
end
|
41
|
+
|
42
|
+
# Simply returns `Chewy::Index.specification_hash`, but
|
43
|
+
# prepared for JSON with `as_json` method. This means all the
|
44
|
+
# keys are strings and there are only values of types handled in JSON.
|
45
|
+
#
|
46
|
+
# @see Chewy::Index.specification_hash
|
47
|
+
# @return [Hash] a JSON-ready hash
|
48
|
+
def current
|
49
|
+
@index.specification_hash.as_json
|
50
|
+
end
|
51
|
+
|
52
|
+
# Compares previously locked and current specifications.
|
53
|
+
#
|
54
|
+
# @return [true, false] the result of comparison
|
55
|
+
def changed?
|
56
|
+
current != locked
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/chewy/journal.rb
CHANGED
@@ -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
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
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
|
-
|
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
|
-
|
51
|
-
|
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
|
57
|
-
|
58
|
-
|
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
|