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/type/observe.rb
CHANGED
@@ -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,14 +19,26 @@ module Chewy
|
|
27
19
|
type_name
|
28
20
|
end
|
29
21
|
|
30
|
-
Chewy.derive_type(reference)
|
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
|
|
34
38
|
def extract_callback_options!(args)
|
35
39
|
options = args.extract_options!
|
36
40
|
result = options.each_key.with_object({}) do |key, hash|
|
37
|
-
hash[key] = options.delete(key) if [
|
41
|
+
hash[key] = options.delete(key) if %i[if unless].include?(key)
|
38
42
|
end
|
39
43
|
args.push(options) unless options.empty?
|
40
44
|
result
|
@@ -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
|
@@ -0,0 +1,222 @@
|
|
1
|
+
module Chewy
|
2
|
+
class Type
|
3
|
+
# This class is able to find missing and outdated documents in the ES
|
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
|
6
|
+
# values for each source object and corresponding ES document. Usually,
|
7
|
+
# this field is `updated_at` and if its value in the source is not equal
|
8
|
+
# to the value in the index - this means that this document outdated and
|
9
|
+
# should be reindexed.
|
10
|
+
#
|
11
|
+
# To fetch necessary data from the source it uses adapter method
|
12
|
+
# {Chewy::Type::Adapter::Base#import_fields}, in case when the Object
|
13
|
+
# adapter is used it makes sense to read corresponding documentation.
|
14
|
+
#
|
15
|
+
# If `parallel` option is passed to the initializer - it will fetch surce and
|
16
|
+
# index data in parallel and then perform outdated objects calculation in
|
17
|
+
# parallel processes. Also, further import (if required) will be performed
|
18
|
+
# in parallel as well.
|
19
|
+
#
|
20
|
+
# @note
|
21
|
+
# In rails 4.0 time converted to json with the precision of seconds
|
22
|
+
# without milliseconds used, so outdated check is not so precise there.
|
23
|
+
#
|
24
|
+
# ATTENTION: synchronization may be slow in case when synchronized tables
|
25
|
+
# are missing compound index on primary key and `outdated_sync_field`.
|
26
|
+
#
|
27
|
+
# @see Chewy::Type::Actions::ClassMethods#sync
|
28
|
+
class Syncer
|
29
|
+
DEFAULT_SYNC_BATCH_SIZE = 20_000
|
30
|
+
ISO_DATETIME = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?\z/
|
31
|
+
OUTDATED_IDS_WORKER = lambda do |outdated_sync_field_type, source_data_hash, type, total, index_data|
|
32
|
+
::Process.setproctitle("chewy [#{type}]: sync outdated calculation (#{::Parallel.worker_number + 1}/#{total})") if type
|
33
|
+
index_data.each_with_object([]) do |(id, index_sync_value), result|
|
34
|
+
next unless source_data_hash[id]
|
35
|
+
|
36
|
+
outdated = if outdated_sync_field_type == 'date'
|
37
|
+
!Chewy::Type::Syncer.dates_equal(typecast_date(source_data_hash[id]), Time.iso8601(index_sync_value))
|
38
|
+
else
|
39
|
+
source_data_hash[id] != index_sync_value
|
40
|
+
end
|
41
|
+
|
42
|
+
result.push(id) if outdated
|
43
|
+
end
|
44
|
+
end
|
45
|
+
SOURCE_OR_INDEX_DATA_WORKER = lambda do |syncer, type, kind|
|
46
|
+
::Process.setproctitle("chewy [#{type}]: sync fetching data (#{kind})")
|
47
|
+
result = case kind
|
48
|
+
when :source
|
49
|
+
syncer.send(:fetch_source_data)
|
50
|
+
when :index
|
51
|
+
syncer.send(:fetch_index_data)
|
52
|
+
end
|
53
|
+
{kind => result}
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.typecast_date(string)
|
57
|
+
if string.is_a?(String) && (match = ISO_DATETIME.match(string))
|
58
|
+
microsec = (match[7].to_r * 1_000_000).to_i
|
59
|
+
date = "#{match[1]}-#{match[2]}-#{match[3]}T#{match[4]}:#{match[5]}:#{match[6]}.#{format('%06d', microsec)}+00:00"
|
60
|
+
Time.iso8601(date)
|
61
|
+
else
|
62
|
+
string
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# Compares times with ms precision.
|
67
|
+
def self.dates_equal(one, two)
|
68
|
+
[one.to_i, one.usec / 1000] == [two.to_i, two.usec / 1000]
|
69
|
+
end
|
70
|
+
|
71
|
+
# In ActiveSupport ~> 4.0 json dumpled times without any
|
72
|
+
# milliseconds, so ES stored time with the seconds precision.
|
73
|
+
if ActiveSupport::VERSION::STRING < '4.1.0'
|
74
|
+
def self.dates_equal(one, two)
|
75
|
+
one.to_i == two.to_i
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# @param type [Chewy::Type] chewy type
|
80
|
+
# @param parallel [true, Integer, Hash] options for parallel execution or the number of processes
|
81
|
+
def initialize(type, parallel: nil)
|
82
|
+
@type = type
|
83
|
+
@parallel = if !parallel || parallel.is_a?(Hash)
|
84
|
+
parallel
|
85
|
+
elsif parallel.is_a?(Integer)
|
86
|
+
{in_processes: parallel}
|
87
|
+
else
|
88
|
+
{}
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
# Finds all the missing and outdated ids and performs import for them.
|
93
|
+
#
|
94
|
+
# @return [Integer, nil] the amount of missing and outdated documents reindexed, nil in case of errors
|
95
|
+
def perform
|
96
|
+
ids = missing_ids | outdated_ids
|
97
|
+
return 0 if ids.blank?
|
98
|
+
@type.import(ids, parallel: @parallel) && ids.count
|
99
|
+
end
|
100
|
+
|
101
|
+
# Finds ids of all the objects that are not indexed yet or deleted
|
102
|
+
# from the source already.
|
103
|
+
#
|
104
|
+
# @return [Array<String>] an array of missing ids from both sides
|
105
|
+
def missing_ids
|
106
|
+
return [] if source_data.blank?
|
107
|
+
|
108
|
+
@missing_ids ||= begin
|
109
|
+
source_data_ids = data_ids(source_data)
|
110
|
+
index_data_ids = data_ids(index_data)
|
111
|
+
|
112
|
+
(source_data_ids - index_data_ids).concat(index_data_ids - source_data_ids)
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
# If type supports outdated sync, it compares for the values of the
|
117
|
+
# type `outdated_sync_field` for each object and document in the source
|
118
|
+
# and index and returns the ids of entities which which are having
|
119
|
+
# different values there.
|
120
|
+
#
|
121
|
+
# @see Chewy::Type::Mapping::ClassMethods#supports_outdated_sync?
|
122
|
+
# @return [Array<String>] an array of outdated ids
|
123
|
+
def outdated_ids
|
124
|
+
return [] if source_data.blank? || index_data.blank? || !@type.supports_outdated_sync?
|
125
|
+
@outdated_ids ||= begin
|
126
|
+
if @parallel
|
127
|
+
parallel_outdated_ids
|
128
|
+
else
|
129
|
+
linear_outdated_ids
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
private
|
135
|
+
|
136
|
+
def source_data
|
137
|
+
@source_data ||= source_and_index_data.first
|
138
|
+
end
|
139
|
+
|
140
|
+
def index_data
|
141
|
+
@index_data ||= source_and_index_data.second
|
142
|
+
end
|
143
|
+
|
144
|
+
def source_and_index_data
|
145
|
+
@source_and_index_data ||= begin
|
146
|
+
if @parallel
|
147
|
+
::ActiveRecord::Base.connection.close if defined?(::ActiveRecord::Base)
|
148
|
+
result = ::Parallel.map(%i[source index], @parallel, &SOURCE_OR_INDEX_DATA_WORKER.curry[self, @type])
|
149
|
+
::ActiveRecord::Base.connection.reconnect! if defined?(::ActiveRecord::Base)
|
150
|
+
if result.first.keys.first == :source
|
151
|
+
[result.first.values.first, result.second.values.first]
|
152
|
+
else
|
153
|
+
[result.second.values.first, result.first.values.first]
|
154
|
+
end
|
155
|
+
else
|
156
|
+
[fetch_source_data, fetch_index_data]
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
def fetch_source_data
|
162
|
+
if @type.supports_outdated_sync?
|
163
|
+
@type.adapter.import_fields(fields: [@type.outdated_sync_field], batch_size: DEFAULT_SYNC_BATCH_SIZE, typecast: false).to_a.flatten(1).each do |data|
|
164
|
+
data[0] = data[0].to_s
|
165
|
+
end
|
166
|
+
else
|
167
|
+
@type.adapter.import_fields(batch_size: DEFAULT_SYNC_BATCH_SIZE, typecast: false).to_a.flatten(1).map(&:to_s)
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
def fetch_index_data
|
172
|
+
if @type.supports_outdated_sync?
|
173
|
+
@type.pluck(:_id, @type.outdated_sync_field).each do |data|
|
174
|
+
data[0] = data[0].to_s
|
175
|
+
end
|
176
|
+
else
|
177
|
+
@type.pluck(:_id).map(&:to_s)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
def data_ids(data)
|
182
|
+
return data unless @type.supports_outdated_sync?
|
183
|
+
data.map(&:first)
|
184
|
+
end
|
185
|
+
|
186
|
+
def linear_outdated_ids
|
187
|
+
OUTDATED_IDS_WORKER.call(outdated_sync_field_type, source_data.to_h, nil, nil, index_data)
|
188
|
+
end
|
189
|
+
|
190
|
+
def parallel_outdated_ids
|
191
|
+
size = processor_count.zero? ? index_data.size : (index_data.size / processor_count.to_f).ceil
|
192
|
+
batches = index_data.each_slice(size)
|
193
|
+
|
194
|
+
::ActiveRecord::Base.connection.close if defined?(::ActiveRecord::Base)
|
195
|
+
result = ::Parallel.map(batches, @parallel, &OUTDATED_IDS_WORKER.curry[outdated_sync_field_type, source_data.to_h, @type, batches.size]).flatten(1)
|
196
|
+
::ActiveRecord::Base.connection.reconnect! if defined?(::ActiveRecord::Base)
|
197
|
+
result
|
198
|
+
end
|
199
|
+
|
200
|
+
def processor_count
|
201
|
+
@processor_count ||= @parallel[:in_processes] || @parallel[:in_threads] || ::Parallel.processor_count
|
202
|
+
end
|
203
|
+
|
204
|
+
def outdated_sync_field_type
|
205
|
+
return @outdated_sync_field_type if instance_variable_defined?(:@outdated_sync_field_type)
|
206
|
+
return unless @type.outdated_sync_field
|
207
|
+
|
208
|
+
mappings = @type.client.indices.get_mapping(
|
209
|
+
index: @type.index_name,
|
210
|
+
type: @type.type_name
|
211
|
+
).values.first.fetch('mappings', {})
|
212
|
+
|
213
|
+
@outdated_sync_field_type = mappings
|
214
|
+
.fetch(@type.type_name, {})
|
215
|
+
.fetch('properties', {})
|
216
|
+
.fetch(@type.outdated_sync_field.to_s, {})['type']
|
217
|
+
rescue Elasticsearch::Transport::Transport::Errors::NotFound
|
218
|
+
nil
|
219
|
+
end
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
@@ -35,17 +35,20 @@ module Chewy
|
|
35
35
|
!!_witchcraft
|
36
36
|
end
|
37
37
|
|
38
|
-
def cauldron
|
39
|
-
@cauldron ||= Cauldron.new(self)
|
38
|
+
def cauldron(**options)
|
39
|
+
(@cauldron ||= {})[options] ||= Cauldron.new(self, **options)
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
43
|
class Cauldron
|
44
44
|
attr_reader :locals
|
45
45
|
|
46
|
-
|
46
|
+
# @param type [Chewy::Type] type for composition
|
47
|
+
# @param fields [Array<Symbol>] restricts the fields for composition
|
48
|
+
def initialize(type, fields: [])
|
47
49
|
@type = type
|
48
50
|
@locals = []
|
51
|
+
@fields = fields
|
49
52
|
end
|
50
53
|
|
51
54
|
def brew(object, crutches = nil)
|
@@ -55,9 +58,9 @@ module Chewy
|
|
55
58
|
private
|
56
59
|
|
57
60
|
def alicorn
|
58
|
-
@alicorn ||= class_eval <<-RUBY
|
61
|
+
@alicorn ||= singleton_class.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
59
62
|
-> (locals, object0, crutches) do
|
60
|
-
#{composed_values(@type.
|
63
|
+
#{composed_values(@type.root, 0)}
|
61
64
|
end
|
62
65
|
RUBY
|
63
66
|
end
|
@@ -91,7 +94,7 @@ module Chewy
|
|
91
94
|
end
|
92
95
|
|
93
96
|
def non_proc_values(field, nesting)
|
94
|
-
non_proc_fields = non_proc_fields_for(field)
|
97
|
+
non_proc_fields = non_proc_fields_for(field, nesting)
|
95
98
|
object = "object#{nesting}"
|
96
99
|
|
97
100
|
if non_proc_fields.present?
|
@@ -99,14 +102,16 @@ module Chewy
|
|
99
102
|
(if #{object}.is_a?(Hash)
|
100
103
|
{
|
101
104
|
#{non_proc_fields.map do |f|
|
102
|
-
|
103
|
-
"#{
|
105
|
+
key_name = f.value.is_a?(Symbol) || f.value.is_a?(String) ? f.value : f.name
|
106
|
+
fetcher = "#{object}.has_key?(:#{key_name}) ? #{object}[:#{key_name}] : #{object}['#{key_name}']"
|
107
|
+
"'#{f.name}'.freeze => #{composed_value(f, fetcher, nesting)}"
|
104
108
|
end.join(', ')}
|
105
109
|
}
|
106
110
|
else
|
107
111
|
{
|
108
112
|
#{non_proc_fields.map do |f|
|
109
|
-
|
113
|
+
method_name = f.value.is_a?(Symbol) || f.value.is_a?(String) ? f.value : f.name
|
114
|
+
"'#{f.name}'.freeze => #{composed_value(f, "#{object}.#{method_name}", nesting)}"
|
110
115
|
end.join(', ')}
|
111
116
|
}
|
112
117
|
end)
|
@@ -117,13 +122,13 @@ module Chewy
|
|
117
122
|
end
|
118
123
|
|
119
124
|
def proc_values(field, nesting)
|
120
|
-
proc_fields = proc_fields_for(field)
|
125
|
+
proc_fields = proc_fields_for(field, nesting)
|
121
126
|
|
122
127
|
if proc_fields.present?
|
123
128
|
<<-RUBY
|
124
129
|
{
|
125
130
|
#{proc_fields.map do |f|
|
126
|
-
"#{f.name}
|
131
|
+
"'#{f.name}'.freeze => (#{composed_value(f, source_for(f.value, nesting), nesting)})"
|
127
132
|
end.join(', ')}
|
128
133
|
}
|
129
134
|
RUBY
|
@@ -132,14 +137,26 @@ module Chewy
|
|
132
137
|
end
|
133
138
|
end
|
134
139
|
|
135
|
-
def non_proc_fields_for(parent)
|
140
|
+
def non_proc_fields_for(parent, nesting)
|
136
141
|
return [] unless parent
|
137
|
-
(parent.children || []).
|
142
|
+
fields = (parent.children || []).reject { |field| field.value.is_a?(Proc) }
|
143
|
+
|
144
|
+
if nesting.zero? && @fields.present?
|
145
|
+
fields.select { |f| @fields.include?(f.name) }
|
146
|
+
else
|
147
|
+
fields
|
148
|
+
end
|
138
149
|
end
|
139
150
|
|
140
|
-
def proc_fields_for(parent)
|
151
|
+
def proc_fields_for(parent, nesting)
|
141
152
|
return [] unless parent
|
142
|
-
(parent.children || []).select { |field| field.value
|
153
|
+
fields = (parent.children || []).select { |field| field.value.is_a?(Proc) }
|
154
|
+
|
155
|
+
if nesting.zero? && @fields.present?
|
156
|
+
fields.select { |f| @fields.include?(f.name) }
|
157
|
+
else
|
158
|
+
fields
|
159
|
+
end
|
143
160
|
end
|
144
161
|
|
145
162
|
def source_for(proc, nesting)
|
@@ -166,7 +183,6 @@ module Chewy
|
|
166
183
|
locals.push(proc.binding.eval(variable.to_s))
|
167
184
|
source = replace_local(source, variable, locals.size - 1)
|
168
185
|
end
|
169
|
-
|
170
186
|
end
|
171
187
|
|
172
188
|
Unparser.unparse(source)
|
data/lib/chewy/type/wrapper.rb
CHANGED
@@ -3,22 +3,48 @@ module Chewy
|
|
3
3
|
module Wrapper
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
|
6
|
-
|
6
|
+
included do
|
7
|
+
attr_accessor :_data, :_object
|
8
|
+
attr_reader :attributes
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def build(hit)
|
13
|
+
attributes = (hit['_source'] || {})
|
14
|
+
.reverse_merge(id: hit['_id'])
|
15
|
+
.merge!(_score: hit['_score'])
|
16
|
+
.merge!(_explanation: hit['_explanation'])
|
17
|
+
|
18
|
+
wrapper = new(attributes)
|
19
|
+
wrapper._data = hit
|
20
|
+
wrapper
|
21
|
+
end
|
22
|
+
end
|
7
23
|
|
8
24
|
def initialize(attributes = {})
|
9
25
|
@attributes = attributes.stringify_keys
|
10
26
|
end
|
11
27
|
|
12
28
|
def ==(other)
|
29
|
+
return true if super
|
30
|
+
|
13
31
|
if other.is_a?(Chewy::Type)
|
14
32
|
self.class == other.class && (respond_to?(:id) ? id == other.id : attributes == other.attributes)
|
15
33
|
elsif other.respond_to?(:id)
|
16
|
-
|
34
|
+
self.class.adapter.target.is_a?(Class) &&
|
35
|
+
other.is_a?(self.class.adapter.target) &&
|
36
|
+
id.to_s == other.id.to_s
|
17
37
|
else
|
18
38
|
false
|
19
39
|
end
|
20
40
|
end
|
21
41
|
|
42
|
+
%w[_id _type _index].each do |name|
|
43
|
+
define_method name do
|
44
|
+
_data[name]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
22
48
|
def method_missing(method, *args, &block)
|
23
49
|
m = method.to_s
|
24
50
|
if (name = highlight_name(m))
|
@@ -44,11 +70,11 @@ module Chewy
|
|
44
70
|
end
|
45
71
|
|
46
72
|
def attribute_defined?(attribute)
|
47
|
-
self.class.
|
73
|
+
self.class.root && self.class.root.children.find { |a| a.name.to_s == attribute }.present?
|
48
74
|
end
|
49
75
|
|
50
76
|
def highlight(attribute)
|
51
|
-
_data['highlight'][attribute].first
|
77
|
+
_data['highlight'][attribute].first if highlight?(attribute)
|
52
78
|
end
|
53
79
|
|
54
80
|
def highlight?(attribute)
|