chewy 0.8.4 → 7.3.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/CODEOWNERS +1 -0
- data/.github/ISSUE_TEMPLATE/bug_report.md +39 -0
- data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +16 -0
- data/.github/workflows/ruby.yml +74 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +61 -0
- data/.rubocop_todo.yml +132 -0
- data/.yardopts +5 -0
- data/CHANGELOG.md +554 -245
- data/CODE_OF_CONDUCT.md +14 -0
- data/CONTRIBUTING.md +63 -0
- data/Gemfile +14 -11
- data/Guardfile +8 -6
- data/LICENSE.txt +1 -1
- data/README.md +748 -623
- data/Rakefile +11 -1
- data/chewy.gemspec +15 -19
- data/gemfiles/rails.5.2.activerecord.gemfile +11 -0
- data/gemfiles/rails.6.0.activerecord.gemfile +11 -0
- data/gemfiles/rails.6.1.activerecord.gemfile +13 -0
- data/gemfiles/rails.7.0.activerecord.gemfile +13 -0
- data/lib/chewy/config.rb +64 -50
- data/lib/chewy/errors.rb +10 -16
- data/lib/chewy/fields/base.rb +122 -32
- data/lib/chewy/fields/root.rb +48 -23
- data/lib/chewy/index/actions.rb +140 -54
- data/lib/chewy/index/adapter/active_record.rb +112 -0
- data/lib/chewy/{type → index}/adapter/base.rb +31 -12
- data/lib/chewy/index/adapter/object.rb +249 -0
- data/lib/chewy/index/adapter/orm.rb +194 -0
- data/lib/chewy/index/aliases.rb +14 -4
- data/lib/chewy/index/crutch.rb +40 -0
- data/lib/chewy/index/import/bulk_builder.rb +311 -0
- data/lib/chewy/index/import/bulk_request.rb +77 -0
- data/lib/chewy/index/import/journal_builder.rb +44 -0
- data/lib/chewy/index/import/routine.rb +139 -0
- data/lib/chewy/index/import.rb +243 -0
- data/lib/chewy/{type → index}/mapping.rb +79 -68
- data/lib/chewy/index/observe/active_record_methods.rb +87 -0
- data/lib/chewy/index/observe/callback.rb +34 -0
- data/lib/chewy/index/observe.rb +17 -0
- data/lib/chewy/index/settings.rb +10 -5
- data/lib/chewy/index/specification.rb +61 -0
- data/lib/chewy/index/syncer.rb +221 -0
- data/lib/chewy/{type → index}/witchcraft.rb +100 -39
- data/lib/chewy/index/wrapper.rb +95 -0
- data/lib/chewy/index.rb +216 -140
- data/lib/chewy/journal.rb +66 -0
- data/lib/chewy/log_subscriber.rb +8 -8
- data/lib/chewy/minitest/helpers.rb +150 -0
- data/lib/chewy/minitest/search_index_receiver.rb +76 -0
- data/lib/chewy/minitest.rb +1 -0
- data/lib/chewy/multi_search.rb +62 -0
- data/lib/chewy/railtie.rb +12 -25
- data/lib/chewy/rake_helper.rb +335 -37
- data/lib/chewy/repository.rb +2 -2
- data/lib/chewy/rspec/build_query.rb +12 -0
- data/lib/chewy/rspec/helpers.rb +55 -0
- data/lib/chewy/rspec/update_index.rb +106 -90
- data/lib/chewy/rspec.rb +3 -1
- data/lib/chewy/runtime/version.rb +4 -4
- data/lib/chewy/runtime.rb +1 -1
- data/lib/chewy/search/loader.rb +61 -0
- data/lib/chewy/{query → search}/pagination/kaminari.rb +13 -5
- 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/collapse.rb +16 -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/ignore_unavailable.rb +27 -0
- data/lib/chewy/search/parameters/indices.rb +78 -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 +25 -0
- data/lib/chewy/search/parameters/offset.rb +17 -0
- data/lib/chewy/search/parameters/order.rb +51 -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 +77 -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/track_total_hits.rb +16 -0
- data/lib/chewy/search/parameters/version.rb +16 -0
- data/lib/chewy/search/parameters.rb +170 -0
- data/lib/chewy/search/query_proxy.rb +264 -0
- data/lib/chewy/search/request.rb +1071 -0
- data/lib/chewy/search/response.rb +119 -0
- data/lib/chewy/search/scoping.rb +49 -0
- data/lib/chewy/search/scrolling.rb +137 -0
- data/lib/chewy/search.rb +68 -28
- data/lib/chewy/stash.rb +68 -0
- data/lib/chewy/strategy/active_job.rb +3 -2
- data/lib/chewy/strategy/atomic.rb +2 -4
- data/lib/chewy/strategy/atomic_no_refresh.rb +18 -0
- data/lib/chewy/strategy/base.rb +13 -3
- data/lib/chewy/strategy/bypass.rb +1 -2
- data/lib/chewy/strategy/delayed_sidekiq/scheduler.rb +148 -0
- data/lib/chewy/strategy/delayed_sidekiq/worker.rb +52 -0
- data/lib/chewy/strategy/delayed_sidekiq.rb +17 -0
- data/lib/chewy/strategy/lazy_sidekiq.rb +64 -0
- data/lib/chewy/strategy/sidekiq.rb +15 -2
- data/lib/chewy/strategy/urgent.rb +1 -1
- data/lib/chewy/strategy.rb +16 -20
- data/lib/chewy/version.rb +1 -1
- data/lib/chewy.rb +81 -82
- data/lib/generators/chewy/install_generator.rb +3 -3
- data/lib/tasks/chewy.rake +99 -32
- data/migration_guide.md +56 -0
- data/spec/chewy/config_spec.rb +87 -15
- data/spec/chewy/fields/base_spec.rb +542 -233
- data/spec/chewy/fields/root_spec.rb +115 -17
- data/spec/chewy/fields/time_fields_spec.rb +13 -12
- data/spec/chewy/index/actions_spec.rb +595 -77
- data/spec/chewy/index/adapter/active_record_spec.rb +601 -0
- data/spec/chewy/index/adapter/object_spec.rb +243 -0
- data/spec/chewy/index/aliases_spec.rb +5 -5
- data/spec/chewy/index/import/bulk_builder_spec.rb +494 -0
- data/spec/chewy/index/import/bulk_request_spec.rb +95 -0
- data/spec/chewy/index/import/journal_builder_spec.rb +87 -0
- data/spec/chewy/index/import/routine_spec.rb +110 -0
- data/spec/chewy/index/import_spec.rb +615 -0
- data/spec/chewy/index/mapping_spec.rb +135 -0
- data/spec/chewy/index/observe/active_record_methods_spec.rb +68 -0
- data/spec/chewy/index/observe/callback_spec.rb +139 -0
- data/spec/chewy/index/observe_spec.rb +143 -0
- data/spec/chewy/index/settings_spec.rb +103 -50
- data/spec/chewy/index/specification_spec.rb +159 -0
- data/spec/chewy/index/syncer_spec.rb +118 -0
- data/spec/chewy/index/witchcraft_spec.rb +245 -0
- data/spec/chewy/index/wrapper_spec.rb +100 -0
- data/spec/chewy/index_spec.rb +149 -121
- data/spec/chewy/journal_spec.rb +223 -0
- data/spec/chewy/minitest/helpers_spec.rb +198 -0
- data/spec/chewy/minitest/search_index_receiver_spec.rb +118 -0
- data/spec/chewy/multi_search_spec.rb +84 -0
- data/spec/chewy/rake_helper_spec.rb +656 -0
- data/spec/chewy/repository_spec.rb +8 -8
- data/spec/chewy/rspec/build_query_spec.rb +34 -0
- data/spec/chewy/rspec/helpers_spec.rb +61 -0
- data/spec/chewy/rspec/update_index_spec.rb +220 -114
- data/spec/chewy/runtime_spec.rb +2 -2
- data/spec/chewy/search/loader_spec.rb +83 -0
- data/spec/chewy/search/pagination/kaminari_examples.rb +69 -0
- data/spec/chewy/search/pagination/kaminari_spec.rb +21 -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/collapse_spec.rb +5 -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/ignore_unavailable_spec.rb +67 -0
- data/spec/chewy/search/parameters/indices_spec.rb +99 -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 +72 -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 +434 -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 +35 -0
- data/spec/chewy/search/parameters/search_type_spec.rb +5 -0
- data/spec/chewy/search/parameters/source_spec.rb +162 -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/track_total_hits_spec.rb +5 -0
- data/spec/chewy/search/parameters/version_spec.rb +5 -0
- data/spec/chewy/search/parameters_spec.rb +161 -0
- data/spec/chewy/search/query_proxy_spec.rb +119 -0
- data/spec/chewy/search/request_spec.rb +880 -0
- data/spec/chewy/search/response_spec.rb +202 -0
- data/spec/chewy/search/scrolling_spec.rb +171 -0
- data/spec/chewy/search_spec.rb +82 -55
- data/spec/chewy/stash_spec.rb +85 -0
- data/spec/chewy/strategy/active_job_spec.rb +27 -8
- data/spec/chewy/strategy/atomic_no_refresh_spec.rb +60 -0
- data/spec/chewy/strategy/atomic_spec.rb +13 -11
- data/spec/chewy/strategy/delayed_sidekiq_spec.rb +190 -0
- data/spec/chewy/strategy/lazy_sidekiq_spec.rb +214 -0
- data/spec/chewy/strategy/sidekiq_spec.rb +19 -7
- data/spec/chewy/strategy_spec.rb +19 -15
- data/spec/chewy_spec.rb +65 -88
- data/spec/spec_helper.rb +11 -20
- data/spec/support/active_record.rb +48 -6
- data/spec/support/class_helpers.rb +4 -19
- metadata +299 -183
- data/.travis.yml +0 -76
- data/Appraisals +0 -76
- data/gemfiles/rails.3.2.activerecord.gemfile +0 -15
- data/gemfiles/rails.3.2.activerecord.kaminari.gemfile +0 -14
- data/gemfiles/rails.3.2.activerecord.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.0.activerecord.gemfile +0 -15
- data/gemfiles/rails.4.0.activerecord.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.0.activerecord.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.0.mongoid.4.0.0.gemfile +0 -15
- data/gemfiles/rails.4.0.mongoid.4.0.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.0.mongoid.4.0.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.0.mongoid.5.1.0.gemfile +0 -15
- data/gemfiles/rails.4.0.mongoid.5.1.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.0.mongoid.5.1.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.1.activerecord.gemfile +0 -15
- data/gemfiles/rails.4.1.activerecord.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.1.activerecord.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.1.mongoid.4.0.0.gemfile +0 -15
- data/gemfiles/rails.4.1.mongoid.4.0.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.1.mongoid.4.0.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.1.mongoid.5.1.0.gemfile +0 -15
- data/gemfiles/rails.4.1.mongoid.5.1.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.1.mongoid.5.1.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.2.activerecord.gemfile +0 -16
- data/gemfiles/rails.4.2.activerecord.kaminari.gemfile +0 -15
- data/gemfiles/rails.4.2.activerecord.will_paginate.gemfile +0 -15
- data/gemfiles/rails.4.2.mongoid.4.0.0.gemfile +0 -15
- data/gemfiles/rails.4.2.mongoid.4.0.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.2.mongoid.4.0.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.2.mongoid.5.1.0.gemfile +0 -15
- data/gemfiles/rails.4.2.mongoid.5.1.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.2.mongoid.5.1.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.5.0.0.beta3.activerecord.gemfile +0 -16
- data/gemfiles/rails.5.0.0.beta3.activerecord.kaminari.gemfile +0 -16
- data/gemfiles/rails.5.0.0.beta3.activerecord.will_paginate.gemfile +0 -15
- data/gemfiles/sequel.4.31.gemfile +0 -13
- data/lib/chewy/backports/deep_dup.rb +0 -46
- data/lib/chewy/backports/duplicable.rb +0 -90
- data/lib/chewy/query/compose.rb +0 -69
- data/lib/chewy/query/criteria.rb +0 -181
- data/lib/chewy/query/filters.rb +0 -227
- data/lib/chewy/query/loading.rb +0 -111
- data/lib/chewy/query/nodes/and.rb +0 -25
- data/lib/chewy/query/nodes/base.rb +0 -17
- data/lib/chewy/query/nodes/bool.rb +0 -32
- data/lib/chewy/query/nodes/equal.rb +0 -34
- data/lib/chewy/query/nodes/exists.rb +0 -20
- data/lib/chewy/query/nodes/expr.rb +0 -28
- data/lib/chewy/query/nodes/field.rb +0 -106
- data/lib/chewy/query/nodes/has_child.rb +0 -14
- data/lib/chewy/query/nodes/has_parent.rb +0 -14
- data/lib/chewy/query/nodes/has_relation.rb +0 -61
- data/lib/chewy/query/nodes/match_all.rb +0 -11
- data/lib/chewy/query/nodes/missing.rb +0 -20
- data/lib/chewy/query/nodes/not.rb +0 -25
- data/lib/chewy/query/nodes/or.rb +0 -25
- data/lib/chewy/query/nodes/prefix.rb +0 -18
- data/lib/chewy/query/nodes/query.rb +0 -20
- data/lib/chewy/query/nodes/range.rb +0 -63
- data/lib/chewy/query/nodes/raw.rb +0 -15
- data/lib/chewy/query/nodes/regexp.rb +0 -31
- data/lib/chewy/query/nodes/script.rb +0 -20
- data/lib/chewy/query/pagination/will_paginate.rb +0 -27
- data/lib/chewy/query/pagination.rb +0 -16
- data/lib/chewy/query/scoping.rb +0 -20
- data/lib/chewy/query.rb +0 -1026
- data/lib/chewy/strategy/resque.rb +0 -26
- data/lib/chewy/type/actions.rb +0 -19
- data/lib/chewy/type/adapter/active_record.rb +0 -72
- data/lib/chewy/type/adapter/mongoid.rb +0 -58
- data/lib/chewy/type/adapter/object.rb +0 -89
- data/lib/chewy/type/adapter/orm.rb +0 -156
- data/lib/chewy/type/adapter/sequel.rb +0 -75
- data/lib/chewy/type/crutch.rb +0 -31
- data/lib/chewy/type/import.rb +0 -224
- data/lib/chewy/type/observe.rb +0 -76
- data/lib/chewy/type/wrapper.rb +0 -53
- data/lib/chewy/type.rb +0 -89
- data/lib/sequel/plugins/chewy_observe.rb +0 -78
- data/spec/chewy/query/criteria_spec.rb +0 -433
- data/spec/chewy/query/filters_spec.rb +0 -173
- data/spec/chewy/query/loading_spec.rb +0 -86
- data/spec/chewy/query/nodes/and_spec.rb +0 -16
- data/spec/chewy/query/nodes/bool_spec.rb +0 -22
- data/spec/chewy/query/nodes/equal_spec.rb +0 -32
- data/spec/chewy/query/nodes/exists_spec.rb +0 -18
- data/spec/chewy/query/nodes/has_child_spec.rb +0 -40
- data/spec/chewy/query/nodes/has_parent_spec.rb +0 -40
- data/spec/chewy/query/nodes/match_all_spec.rb +0 -11
- data/spec/chewy/query/nodes/missing_spec.rb +0 -15
- data/spec/chewy/query/nodes/not_spec.rb +0 -16
- data/spec/chewy/query/nodes/or_spec.rb +0 -16
- data/spec/chewy/query/nodes/prefix_spec.rb +0 -16
- data/spec/chewy/query/nodes/query_spec.rb +0 -12
- data/spec/chewy/query/nodes/range_spec.rb +0 -32
- data/spec/chewy/query/nodes/raw_spec.rb +0 -11
- data/spec/chewy/query/nodes/regexp_spec.rb +0 -31
- data/spec/chewy/query/nodes/script_spec.rb +0 -15
- data/spec/chewy/query/pagination/kaminari_spec.rb +0 -57
- data/spec/chewy/query/pagination/will_paginage_spec.rb +0 -60
- data/spec/chewy/query/pagination_spec.rb +0 -36
- data/spec/chewy/query_spec.rb +0 -632
- data/spec/chewy/strategy/resque_spec.rb +0 -40
- data/spec/chewy/type/actions_spec.rb +0 -31
- data/spec/chewy/type/adapter/active_record_spec.rb +0 -317
- data/spec/chewy/type/adapter/mongoid_spec.rb +0 -253
- data/spec/chewy/type/adapter/object_spec.rb +0 -139
- data/spec/chewy/type/adapter/sequel_spec.rb +0 -320
- data/spec/chewy/type/import_spec.rb +0 -433
- data/spec/chewy/type/mapping_spec.rb +0 -106
- data/spec/chewy/type/observe_spec.rb +0 -127
- data/spec/chewy/type/witchcraft_spec.rb +0 -154
- data/spec/chewy/type/wrapper_spec.rb +0 -58
- data/spec/chewy/type_spec.rb +0 -33
- data/spec/support/mongoid.rb +0 -81
- data/spec/support/sequel.rb +0 -75
@@ -0,0 +1,243 @@
|
|
1
|
+
require 'chewy/index/import/journal_builder'
|
2
|
+
require 'chewy/index/import/bulk_builder'
|
3
|
+
require 'chewy/index/import/bulk_request'
|
4
|
+
require 'chewy/index/import/routine'
|
5
|
+
|
6
|
+
module Chewy
|
7
|
+
class Index
|
8
|
+
module Import
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
IMPORT_WORKER = lambda do |index, options, total, ids, iteration|
|
12
|
+
::Process.setproctitle("chewy [#{index}]: import data (#{iteration + 1}/#{total})")
|
13
|
+
routine = Routine.new(index, **options)
|
14
|
+
index.adapter.import(*ids, routine.options) do |action_objects|
|
15
|
+
routine.process(**action_objects)
|
16
|
+
end
|
17
|
+
{errors: routine.errors, import: routine.stats, leftovers: routine.leftovers}
|
18
|
+
end
|
19
|
+
|
20
|
+
LEFTOVERS_WORKER = lambda do |index, options, total, body, iteration|
|
21
|
+
::Process.setproctitle("chewy [#{index}]: import leftovers (#{iteration + 1}/#{total})")
|
22
|
+
routine = Routine.new(index, **options)
|
23
|
+
routine.perform_bulk(body)
|
24
|
+
routine.errors
|
25
|
+
end
|
26
|
+
|
27
|
+
module ClassMethods
|
28
|
+
# @!method import(*collection, **options)
|
29
|
+
# Basically, one of the main methods for an index. Performs any objects import
|
30
|
+
# to the index. Does all the objects handling routines.
|
31
|
+
# Performs document import by utilizing bulk API. Bulk size and objects batch
|
32
|
+
# size are controlled by the corresponding options.
|
33
|
+
#
|
34
|
+
# It accepts ORM/ODM objects, PORO, hashes, ids which are used by adapter to
|
35
|
+
# fetch objects from the source depending on the used adapter. It destroys
|
36
|
+
# passed objects from the index if they are not in the default scope
|
37
|
+
# or marked for destruction.
|
38
|
+
#
|
39
|
+
# It handles parent-child relationships with a join field reindexing children when the parent is reindexed.
|
40
|
+
#
|
41
|
+
# Performs journaling if enabled: it stores all the ids of the imported
|
42
|
+
# objects to a specialized index. It is possible to replay particular import
|
43
|
+
# later to restore the data consistency.
|
44
|
+
#
|
45
|
+
# Performs partial index update using `update` bulk action if any `fields` are
|
46
|
+
# specified. Note that if document doesn't exist yet, an error will be raised
|
47
|
+
# by ES, but import catches this an errors and performs full indexing
|
48
|
+
# for the corresponding documents. This feature can be disabled by setting
|
49
|
+
# `update_failover` to `false`.
|
50
|
+
#
|
51
|
+
# Utilizes `ActiveSupport::Notifications`, so it is possible to get imported
|
52
|
+
# objects later by listening to the `import_objects.chewy` queue. It is also
|
53
|
+
# possible to get the list of occurred errors from the payload if something
|
54
|
+
# went wrong.
|
55
|
+
#
|
56
|
+
# Import can also be run in parallel using the Parallel gem functionality.
|
57
|
+
#
|
58
|
+
# @example
|
59
|
+
# UsersIndex.import(parallel: true) # imports everything in parallel with automatic workers number
|
60
|
+
# UsersIndex.import(parallel: 3) # using 3 workers
|
61
|
+
# UsersIndex.import(parallel: {in_threads: 10}) # in 10 threads
|
62
|
+
#
|
63
|
+
# @see https://github.com/elastic/elasticsearch-ruby/blob/master/elasticsearch-api/lib/elasticsearch/api/actions/bulk.rb
|
64
|
+
# @param collection [Array<Object>] and array or anything to import
|
65
|
+
# @param options [Hash{Symbol => Object}] besides specific import options, it accepts all the options suitable for the bulk API call like `refresh` or `timeout`
|
66
|
+
# @option options [String] suffix an index name suffix, used for zero-downtime reset mostly, no suffix by default
|
67
|
+
# @option options [Integer] bulk_size bulk API chunk size in bytes; if passed, the request is performed several times for each chunk, empty by default
|
68
|
+
# @option options [Integer] batch_size passed to the adapter import method, used to split imported objects in chunks, 1000 by default
|
69
|
+
# @option options [Boolean] direct_import skips object reloading in ORM adapter, `false` by default
|
70
|
+
# @option options [true, false] journal enables imported objects journaling, false by default
|
71
|
+
# @option options [Array<Symbol, String>] update_fields list of fields for the partial import, empty by default
|
72
|
+
# @option options [true, false] update_failover enables full objects reimport in cases of partial update errors, `true` by default
|
73
|
+
# @option options [true, Integer, Hash] parallel enables parallel import processing with the Parallel gem, accepts the number of workers or any Parallel gem acceptable options
|
74
|
+
# @return [true, false] false in case of errors
|
75
|
+
ruby2_keywords def import(*args)
|
76
|
+
intercept_import_using_strategy(*args).blank?
|
77
|
+
end
|
78
|
+
|
79
|
+
# @!method import!(*collection, **options)
|
80
|
+
# (see #import)
|
81
|
+
#
|
82
|
+
# The only difference from {#import} is that it raises an exception
|
83
|
+
# in case of any import errors.
|
84
|
+
#
|
85
|
+
# @raise [Chewy::ImportFailed] in case of errors
|
86
|
+
ruby2_keywords def import!(*args)
|
87
|
+
errors = intercept_import_using_strategy(*args)
|
88
|
+
|
89
|
+
raise Chewy::ImportFailed.new(self, errors) if errors.present?
|
90
|
+
|
91
|
+
true
|
92
|
+
end
|
93
|
+
|
94
|
+
# Wraps elasticsearch API bulk method, adds additional features like
|
95
|
+
# `bulk_size` and `suffix`.
|
96
|
+
#
|
97
|
+
# @see https://github.com/elastic/elasticsearch-ruby/blob/master/elasticsearch-api/lib/elasticsearch/api/actions/bulk.rb
|
98
|
+
# @see Chewy::Index::Import::BulkRequest
|
99
|
+
# @param options [Hash{Symbol => Object}] besides specific import options, it accepts all the options suitable for the bulk API call like `refresh` or `timeout`
|
100
|
+
# @option options [String] suffix bulk API chunk size in bytes; if passed, the request is performed several times for each chunk, empty by default
|
101
|
+
# @option options [Integer] bulk_size bulk API chunk size in bytes; if passed, the request is performed several times for each chunk, empty by default
|
102
|
+
# @option options [Array<Hash>] body elasticsearch API bulk method body
|
103
|
+
# @return [Hash] tricky transposed errors hash, empty if everything is fine
|
104
|
+
def bulk(**options)
|
105
|
+
error_items = BulkRequest.new(self, **options).perform(options[:body])
|
106
|
+
Chewy.wait_for_status
|
107
|
+
|
108
|
+
payload_errors(error_items)
|
109
|
+
end
|
110
|
+
|
111
|
+
# Composes a single document from the passed object. Uses either witchcraft
|
112
|
+
# or normal composing under the hood.
|
113
|
+
#
|
114
|
+
# @param object [Object] a data source object
|
115
|
+
# @param crutches [Object] optional crutches object; if omitted - a crutch for the single passed object is created as a fallback
|
116
|
+
# @param fields [Array<Symbol>] and array of fields to restrict the generated document
|
117
|
+
# @return [Hash] a JSON-ready hash
|
118
|
+
def compose(object, crutches = nil, fields: [])
|
119
|
+
crutches ||= Chewy::Index::Crutch::Crutches.new self, [object]
|
120
|
+
|
121
|
+
if witchcraft? && root.children.present?
|
122
|
+
cauldron(fields: fields).brew(object, crutches)
|
123
|
+
else
|
124
|
+
root.compose(object, crutches, fields: fields)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
private
|
129
|
+
|
130
|
+
def intercept_import_using_strategy(*args)
|
131
|
+
args_clone = args.deep_dup
|
132
|
+
options = args_clone.extract_options!
|
133
|
+
strategy = options.delete(:strategy)
|
134
|
+
|
135
|
+
return import_routine(*args) if strategy.blank?
|
136
|
+
|
137
|
+
ids = args_clone.flatten
|
138
|
+
return {} if ids.blank?
|
139
|
+
return {argument: {"#{strategy} supports ids only!" => ids}} unless ids.all? do |id|
|
140
|
+
id.respond_to?(:to_i)
|
141
|
+
end
|
142
|
+
|
143
|
+
case strategy
|
144
|
+
when :delayed_sidekiq
|
145
|
+
begin
|
146
|
+
Chewy::Strategy::DelayedSidekiq::Scheduler.new(self, ids, options).postpone
|
147
|
+
{} # success. errors handling convention
|
148
|
+
rescue StandardError => e
|
149
|
+
{scheduler: {e.message => ids}}
|
150
|
+
end
|
151
|
+
else
|
152
|
+
{argument: {"unsupported strategy: '#{strategy}'" => ids}}
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
def import_routine(*args)
|
157
|
+
return if !args.first.nil? && empty_objects_or_scope?(args.first)
|
158
|
+
|
159
|
+
routine = Routine.new(self, **args.extract_options!)
|
160
|
+
routine.create_indexes!
|
161
|
+
|
162
|
+
if routine.parallel_options
|
163
|
+
import_parallel(args, routine)
|
164
|
+
else
|
165
|
+
import_linear(args, routine)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
def empty_objects_or_scope?(objects_or_scope)
|
170
|
+
if objects_or_scope.respond_to?(:empty?)
|
171
|
+
objects_or_scope.empty?
|
172
|
+
else
|
173
|
+
objects_or_scope.blank?
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
def import_linear(objects, routine)
|
178
|
+
ActiveSupport::Notifications.instrument 'import_objects.chewy', index: self do |payload|
|
179
|
+
adapter.import(*objects, routine.options) do |action_objects|
|
180
|
+
routine.process(**action_objects)
|
181
|
+
end
|
182
|
+
routine.perform_bulk(routine.leftovers)
|
183
|
+
payload[:import] = routine.stats
|
184
|
+
payload[:errors] = payload_errors(routine.errors) if routine.errors.present?
|
185
|
+
payload[:errors]
|
186
|
+
end
|
187
|
+
end
|
188
|
+
|
189
|
+
def import_parallel(objects, routine)
|
190
|
+
raise "The `parallel` gem is required for parallel import, please add `gem 'parallel'` to your Gemfile" unless '::Parallel'.safe_constantize
|
191
|
+
|
192
|
+
ActiveSupport::Notifications.instrument 'import_objects.chewy', index: self do |payload|
|
193
|
+
batches = adapter.import_references(*objects, routine.options.slice(:batch_size)).to_a
|
194
|
+
|
195
|
+
::ActiveRecord::Base.connection.close if defined?(::ActiveRecord::Base)
|
196
|
+
results = ::Parallel.map_with_index(
|
197
|
+
batches,
|
198
|
+
routine.parallel_options,
|
199
|
+
&IMPORT_WORKER.curry[self, routine.options, batches.size]
|
200
|
+
)
|
201
|
+
::ActiveRecord::Base.connection.reconnect! if defined?(::ActiveRecord::Base)
|
202
|
+
errors, import, leftovers = process_parallel_import_results(results)
|
203
|
+
|
204
|
+
if leftovers.present?
|
205
|
+
batches = leftovers.each_slice(routine.options[:batch_size])
|
206
|
+
results = ::Parallel.map_with_index(
|
207
|
+
batches,
|
208
|
+
routine.parallel_options,
|
209
|
+
&LEFTOVERS_WORKER.curry[self, routine.options, batches.size]
|
210
|
+
)
|
211
|
+
errors.concat(results.flatten(1))
|
212
|
+
end
|
213
|
+
|
214
|
+
payload[:import] = import
|
215
|
+
payload[:errors] = payload_errors(errors) if errors.present?
|
216
|
+
payload[:errors]
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
220
|
+
def process_parallel_import_results(results)
|
221
|
+
results.each_with_object([[], {}, []]) do |r, (e, i, l)|
|
222
|
+
e.concat(r[:errors])
|
223
|
+
i.merge!(r[:import]) { |_k, v1, v2| v1.to_i + v2.to_i }
|
224
|
+
l.concat(r[:leftovers])
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
def payload_errors(errors)
|
229
|
+
errors.each_with_object({}) do |error, result|
|
230
|
+
action = error.keys.first.to_sym
|
231
|
+
item = error.values.first
|
232
|
+
error = item['error']
|
233
|
+
id = item['_id']
|
234
|
+
|
235
|
+
result[action] ||= {}
|
236
|
+
result[action][error] ||= []
|
237
|
+
result[action][error].push(id)
|
238
|
+
end
|
239
|
+
end
|
240
|
+
end
|
241
|
+
end
|
242
|
+
end
|
243
|
+
end
|
@@ -1,5 +1,5 @@
|
|
1
1
|
module Chewy
|
2
|
-
class
|
2
|
+
class Index
|
3
3
|
module Mapping
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
|
@@ -8,50 +8,55 @@ module Chewy
|
|
8
8
|
class_attribute :_templates
|
9
9
|
class_attribute :_agg_defs
|
10
10
|
self._agg_defs = {}
|
11
|
+
class_attribute :outdated_sync_field
|
12
|
+
self.outdated_sync_field = :updated_at
|
11
13
|
end
|
12
14
|
|
13
15
|
module ClassMethods
|
14
|
-
# Defines root object for mapping and is optional for
|
16
|
+
# Defines root object for mapping and is optional for index
|
15
17
|
# definition. Use it only if you need to pass options for root
|
16
18
|
# object mapping, such as `date_detection` or `dynamic_date_formats`
|
17
19
|
#
|
20
|
+
# @example
|
18
21
|
# class UsersIndex < Chewy::Index
|
19
|
-
#
|
20
|
-
#
|
21
|
-
#
|
22
|
-
# end
|
22
|
+
# index_scope User
|
23
|
+
# # root object defined implicitly and optionless for current type
|
24
|
+
# field :full_name, type: 'keyword'
|
23
25
|
# end
|
24
26
|
#
|
25
27
|
# class CarsIndex < Chewy::Index
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
# end
|
28
|
+
# index_scope Car
|
29
|
+
# # explicit root definition with additional options
|
30
|
+
# root dynamic_date_formats: ['yyyy-MM-dd'] do
|
31
|
+
# field :model_name, type: 'keyword'
|
31
32
|
# end
|
32
33
|
# end
|
33
34
|
#
|
34
|
-
def root
|
35
|
-
|
36
|
-
|
35
|
+
def root(**options)
|
36
|
+
self.root_object ||= Chewy::Fields::Root.new(:root, **Chewy.default_root_options.merge(options))
|
37
|
+
root_object.update_options!(**options)
|
38
|
+
yield if block_given?
|
39
|
+
root_object
|
37
40
|
end
|
38
41
|
|
39
|
-
# Defines mapping field for
|
42
|
+
# Defines mapping field for index
|
40
43
|
#
|
44
|
+
# @example
|
41
45
|
# class UsersIndex < Chewy::Index
|
42
|
-
#
|
43
|
-
#
|
44
|
-
#
|
45
|
-
# end
|
46
|
+
# index_scope User
|
47
|
+
# # passing all the options to field definition:
|
48
|
+
# field :full_name, analyzer: 'special'
|
46
49
|
# end
|
47
50
|
#
|
48
51
|
# The `type` is optional and defaults to `string` if not defined:
|
49
52
|
#
|
53
|
+
# @example
|
50
54
|
# field :full_name
|
51
55
|
#
|
52
56
|
# Also, multiple fields might be defined with one call and
|
53
57
|
# with the same options:
|
54
58
|
#
|
59
|
+
# @example
|
55
60
|
# field :first_name, :last_name, analyzer: 'special'
|
56
61
|
#
|
57
62
|
# The only special option in the field definition
|
@@ -59,31 +64,35 @@ module Chewy
|
|
59
64
|
# method will be called for the indexed object. Also
|
60
65
|
# `:value` might be a proc or indexed object method name:
|
61
66
|
#
|
67
|
+
# @example
|
62
68
|
# class User < ActiveRecord::Base
|
63
69
|
# def user_full_name
|
64
70
|
# [first_name, last_name].join(' ')
|
65
71
|
# end
|
66
72
|
# end
|
67
73
|
#
|
68
|
-
# field :full_name, type: '
|
74
|
+
# field :full_name, type: 'keyword', value: :user_full_name
|
69
75
|
#
|
70
76
|
# The proc evaluates inside the indexed object context if
|
71
77
|
# its arity is 0 and in present contexts if there is an argument:
|
72
78
|
#
|
73
|
-
#
|
79
|
+
# @example
|
80
|
+
# field :full_name, type: 'keyword', value: -> { [first_name, last_name].join(' ') }
|
74
81
|
#
|
75
82
|
# separator = ' '
|
76
|
-
# field :full_name, type: '
|
83
|
+
# field :full_name, type: 'keyword', value: ->(user) { [user.first_name, user.last_name].join(separator) }
|
77
84
|
#
|
78
85
|
# If array was returned as value - it will be put in index as well.
|
79
86
|
#
|
80
|
-
#
|
87
|
+
# @example
|
88
|
+
# field :tags, type: 'keyword', value: -> { tags.map(&:name) }
|
81
89
|
#
|
82
90
|
# Fields supports nesting in case of `object` field type. If
|
83
91
|
# `user.quiz` will return an array of objects, then result index content
|
84
92
|
# will be an array of hashes, if `user.quiz` is not a collection association
|
85
93
|
# then just values hash will be put in the index.
|
86
94
|
#
|
95
|
+
# @example
|
87
96
|
# field :quiz do
|
88
97
|
# field :question, :answer
|
89
98
|
# field :score, type: 'integer'
|
@@ -91,109 +100,111 @@ module Chewy
|
|
91
100
|
#
|
92
101
|
# Nested fields are composed from nested objects:
|
93
102
|
#
|
103
|
+
# @example
|
94
104
|
# field :name, value: -> { name_translations } do
|
95
105
|
# field :ru, value: ->(name) { name['ru'] }
|
96
106
|
# field :en, value: ->(name) { name['en'] }
|
97
107
|
# end
|
98
108
|
#
|
99
|
-
#
|
109
|
+
# Of course it is possible to define object fields contents dynamically
|
100
110
|
# but make sure evaluation proc returns hash:
|
101
111
|
#
|
112
|
+
# @example
|
102
113
|
# field :name, type: 'object', value: -> { name_translations }
|
103
114
|
#
|
104
115
|
# The special case is multi_field. If type options and block are
|
105
116
|
# both present field is treated as a multi-field. In that case field
|
106
117
|
# composition changes satisfy elasticsearch rules:
|
107
118
|
#
|
108
|
-
#
|
119
|
+
# @example
|
120
|
+
# field :full_name, type: 'text', analyzer: 'name', value: ->{ full_name.try(:strip) } do
|
109
121
|
# field :sorted, analyzer: 'sorted'
|
110
122
|
# end
|
111
123
|
#
|
112
|
-
def field
|
113
|
-
options = args.extract_options!
|
114
|
-
build_root
|
115
|
-
|
124
|
+
def field(*args, **options, &block)
|
116
125
|
if args.size > 1
|
117
|
-
args.map { |name| field(name, options) }
|
126
|
+
args.map { |name| field(name, **options) }
|
118
127
|
else
|
119
|
-
expand_nested(Chewy::Fields::Base.new(args.first, options), &block)
|
128
|
+
expand_nested(Chewy::Fields::Base.new(args.first, **options), &block)
|
120
129
|
end
|
121
130
|
end
|
122
131
|
|
123
132
|
# Defines an aggregation that can be bound to a query or filter
|
124
133
|
#
|
125
|
-
#
|
126
|
-
#
|
127
|
-
#
|
134
|
+
# @example
|
135
|
+
# # Suppose that a user has posts and each post has ratings
|
136
|
+
# # avg_post_rating is the mean of all ratings
|
128
137
|
# class UsersIndex < Chewy::Index
|
129
|
-
#
|
130
|
-
#
|
131
|
-
#
|
132
|
-
#
|
133
|
-
#
|
134
|
-
#
|
135
|
-
#
|
136
|
-
# end
|
138
|
+
# index_scope User
|
139
|
+
# field :posts do
|
140
|
+
# field :rating
|
141
|
+
# end
|
142
|
+
#
|
143
|
+
# agg :avg_rating do
|
144
|
+
# { avg: { field: 'posts.rating' } }
|
137
145
|
# end
|
138
146
|
# end
|
139
|
-
def agg
|
140
|
-
|
141
|
-
build_root
|
142
|
-
self._agg_defs = _agg_defs.merge(args.first => block)
|
147
|
+
def agg(name, &block)
|
148
|
+
self._agg_defs = _agg_defs.merge(name => block)
|
143
149
|
end
|
144
150
|
alias_method :aggregation, :agg
|
145
151
|
|
146
152
|
# Defines dynamic template in mapping root objects
|
147
153
|
#
|
154
|
+
# @example
|
148
155
|
# class CarsIndex < Chewy::Index
|
149
|
-
#
|
150
|
-
#
|
151
|
-
#
|
152
|
-
#
|
153
|
-
# end
|
156
|
+
# index_scope Car
|
157
|
+
# template 'model.*', type: 'text', analyzer: 'special'
|
158
|
+
# field 'model', type: 'object' # here we can put { de: 'Der Mercedes', en: 'Mercedes' }
|
159
|
+
# # and template will be applied to this field
|
154
160
|
# end
|
155
161
|
#
|
156
162
|
# Name for each template is generated with the following
|
157
|
-
# rule:
|
163
|
+
# rule: `template_#!{dynamic_templates.size + 1}`.
|
158
164
|
#
|
165
|
+
# @example Templates
|
159
166
|
# template 'tit*', mapping_hash
|
160
167
|
# template 'title.*', mapping_hash # dot in template causes "path_match" using
|
161
168
|
# template /tit.+/, mapping_hash # using "match_pattern": "regexp"
|
162
169
|
# template /title\..+/, mapping_hash # "\." - escaped dot causes "path_match" using
|
163
|
-
# template /tit.+/, '
|
170
|
+
# template /tit.+/, type: 'text', mapping_hash # "match_mapping_type" as an optional second argument
|
164
171
|
# template template42: {match: 'hello*', mapping: {type: 'object'}} # or even pass a template as is
|
165
172
|
#
|
166
|
-
def template
|
167
|
-
|
173
|
+
def template(*args, **options)
|
174
|
+
root.dynamic_template(*args, **options)
|
168
175
|
end
|
169
176
|
alias_method :dynamic_template, :template
|
170
177
|
|
171
178
|
# Returns compiled mappings hash for current type
|
172
179
|
#
|
173
180
|
def mappings_hash
|
174
|
-
|
181
|
+
root.mappings_hash
|
182
|
+
end
|
183
|
+
|
184
|
+
# Check whether the type has outdated_sync_field defined with a simple value.
|
185
|
+
#
|
186
|
+
# @return [true, false]
|
187
|
+
def supports_outdated_sync?
|
188
|
+
updated_at_field = root.child_hash[outdated_sync_field] if outdated_sync_field
|
189
|
+
!!updated_at_field && updated_at_field.value.nil?
|
175
190
|
end
|
176
191
|
|
177
192
|
private
|
178
193
|
|
179
|
-
def expand_nested
|
194
|
+
def expand_nested(field)
|
195
|
+
@_current_field ||= root
|
196
|
+
|
180
197
|
if @_current_field
|
181
198
|
field.parent = @_current_field
|
182
199
|
@_current_field.children.push(field)
|
183
200
|
end
|
184
201
|
|
185
|
-
|
186
|
-
previous_field, @_current_field = @_current_field, field
|
187
|
-
block.call
|
188
|
-
@_current_field = previous_field
|
189
|
-
end
|
190
|
-
end
|
202
|
+
return unless block_given?
|
191
203
|
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
@_current_field = root_object
|
204
|
+
previous_field = @_current_field
|
205
|
+
@_current_field = field
|
206
|
+
yield
|
207
|
+
@_current_field = previous_field
|
197
208
|
end
|
198
209
|
end
|
199
210
|
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module Chewy
|
2
|
+
class Index
|
3
|
+
module Observe
|
4
|
+
module Helpers
|
5
|
+
def update_proc(index_name, *args, &block)
|
6
|
+
options = args.extract_options!
|
7
|
+
method = args.first
|
8
|
+
|
9
|
+
proc do
|
10
|
+
reference = if index_name.is_a?(Proc)
|
11
|
+
if index_name.arity.zero?
|
12
|
+
instance_exec(&index_name)
|
13
|
+
else
|
14
|
+
index_name.call(self)
|
15
|
+
end
|
16
|
+
else
|
17
|
+
index_name
|
18
|
+
end
|
19
|
+
|
20
|
+
index = Chewy.derive_name(reference)
|
21
|
+
|
22
|
+
next if Chewy.strategy.current.name == :bypass
|
23
|
+
|
24
|
+
backreference = if method && method.to_s == 'self'
|
25
|
+
self
|
26
|
+
elsif method
|
27
|
+
send(method)
|
28
|
+
else
|
29
|
+
instance_eval(&block)
|
30
|
+
end
|
31
|
+
|
32
|
+
index.update_index(backreference, options)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def extract_callback_options!(args)
|
37
|
+
options = args.extract_options!
|
38
|
+
result = options.each_key.with_object({}) do |key, hash|
|
39
|
+
hash[key] = options.delete(key) if %i[if unless].include?(key)
|
40
|
+
end
|
41
|
+
args.push(options) unless options.empty?
|
42
|
+
result
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
extend Helpers
|
47
|
+
|
48
|
+
module ActiveRecordMethods
|
49
|
+
extend ActiveSupport::Concern
|
50
|
+
|
51
|
+
def run_chewy_callbacks
|
52
|
+
chewy_callbacks.each { |callback| callback.call(self) }
|
53
|
+
end
|
54
|
+
|
55
|
+
def update_chewy_indices
|
56
|
+
Chewy.strategy.current.update_chewy_indices(self)
|
57
|
+
end
|
58
|
+
|
59
|
+
included do
|
60
|
+
class_attribute :chewy_callbacks, default: []
|
61
|
+
end
|
62
|
+
|
63
|
+
class_methods do
|
64
|
+
def initialize_chewy_callbacks
|
65
|
+
if Chewy.use_after_commit_callbacks
|
66
|
+
after_commit :update_chewy_indices, on: %i[create update]
|
67
|
+
after_commit :run_chewy_callbacks, on: :destroy
|
68
|
+
else
|
69
|
+
after_save :update_chewy_indices
|
70
|
+
after_destroy :run_chewy_callbacks
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
ruby2_keywords def update_index(type_name, *args, &block)
|
75
|
+
callback_options = Observe.extract_callback_options!(args)
|
76
|
+
update_proc = Observe.update_proc(type_name, *args, &block)
|
77
|
+
callback = Chewy::Index::Observe::Callback.new(update_proc, callback_options)
|
78
|
+
|
79
|
+
initialize_chewy_callbacks if chewy_callbacks.empty?
|
80
|
+
|
81
|
+
self.chewy_callbacks = chewy_callbacks.dup << callback
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Chewy
|
2
|
+
class Index
|
3
|
+
module Observe
|
4
|
+
class Callback
|
5
|
+
def initialize(executable, filters = {})
|
6
|
+
@executable = executable
|
7
|
+
@if_filter = filters[:if]
|
8
|
+
@unless_filter = filters[:unless]
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(context)
|
12
|
+
return if !@if_filter.nil? && !eval_filter(@if_filter, context)
|
13
|
+
return if !@unless_filter.nil? && eval_filter(@unless_filter, context)
|
14
|
+
|
15
|
+
eval_proc(@executable, context)
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def eval_filter(filter, context)
|
21
|
+
case filter
|
22
|
+
when Symbol then context.send(filter)
|
23
|
+
when Proc then eval_proc(filter, context)
|
24
|
+
else filter
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def eval_proc(executable, context)
|
29
|
+
executable.arity.zero? ? context.instance_exec(&executable) : executable.call(context)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'chewy/index/observe/callback'
|
2
|
+
require 'chewy/index/observe/active_record_methods'
|
3
|
+
|
4
|
+
module Chewy
|
5
|
+
class Index
|
6
|
+
module Observe
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def update_index(objects, options = {})
|
11
|
+
Chewy.strategy.current.update(self, objects, options)
|
12
|
+
true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|