chewy 7.2.2 → 7.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +14 -6
  3. data/CHANGELOG.md +44 -0
  4. data/Gemfile +1 -0
  5. data/README.md +37 -3
  6. data/chewy.gemspec +1 -1
  7. data/gemfiles/rails.7.0.activerecord.gemfile +13 -0
  8. data/lib/chewy/config.rb +2 -2
  9. data/lib/chewy/errors.rb +6 -0
  10. data/lib/chewy/fields/base.rb +68 -12
  11. data/lib/chewy/fields/root.rb +3 -11
  12. data/lib/chewy/index/actions.rb +3 -3
  13. data/lib/chewy/index/adapter/active_record.rb +5 -0
  14. data/lib/chewy/index/adapter/object.rb +3 -3
  15. data/lib/chewy/index/adapter/orm.rb +8 -6
  16. data/lib/chewy/index/import/bulk_builder.rb +219 -31
  17. data/lib/chewy/index/import/bulk_request.rb +1 -1
  18. data/lib/chewy/index/import.rb +3 -4
  19. data/lib/chewy/index/mapping.rb +2 -2
  20. data/lib/chewy/index/observe/active_record_methods.rb +87 -0
  21. data/lib/chewy/index/observe/callback.rb +34 -0
  22. data/lib/chewy/index/observe.rb +3 -58
  23. data/lib/chewy/rake_helper.rb +21 -1
  24. data/lib/chewy/rspec/update_index.rb +11 -4
  25. data/lib/chewy/runtime.rb +1 -1
  26. data/lib/chewy/search/parameters/ignore_unavailable.rb +27 -0
  27. data/lib/chewy/search/parameters/track_total_hits.rb +16 -0
  28. data/lib/chewy/search/parameters.rb +1 -1
  29. data/lib/chewy/search/request.rb +30 -6
  30. data/lib/chewy/search/scoping.rb +1 -1
  31. data/lib/chewy/search.rb +2 -1
  32. data/lib/chewy/strategy/active_job.rb +1 -1
  33. data/lib/chewy/strategy/atomic_no_refresh.rb +18 -0
  34. data/lib/chewy/strategy/base.rb +10 -0
  35. data/lib/chewy/strategy/lazy_sidekiq.rb +64 -0
  36. data/lib/chewy/strategy/sidekiq.rb +1 -1
  37. data/lib/chewy/strategy.rb +2 -0
  38. data/lib/chewy/version.rb +1 -1
  39. data/lib/chewy.rb +18 -8
  40. data/lib/tasks/chewy.rake +1 -1
  41. data/migration_guide.md +1 -1
  42. data/spec/chewy/fields/base_spec.rb +38 -18
  43. data/spec/chewy/index/adapter/active_record_spec.rb +26 -0
  44. data/spec/chewy/index/import/bulk_builder_spec.rb +304 -0
  45. data/spec/chewy/index/import/routine_spec.rb +3 -3
  46. data/spec/chewy/index/import_spec.rb +40 -0
  47. data/spec/chewy/index/observe/active_record_methods_spec.rb +68 -0
  48. data/spec/chewy/index/observe/callback_spec.rb +139 -0
  49. data/spec/chewy/index/observe_spec.rb +27 -0
  50. data/spec/chewy/rake_helper_spec.rb +19 -0
  51. data/spec/chewy/search/parameters/ignore_unavailable_spec.rb +67 -0
  52. data/spec/chewy/search/parameters/track_total_hits_spec.rb +5 -0
  53. data/spec/chewy/search/parameters_spec.rb +5 -0
  54. data/spec/chewy/search/request_spec.rb +20 -8
  55. data/spec/chewy/search_spec.rb +9 -0
  56. data/spec/chewy/strategy/atomic_no_refresh_spec.rb +60 -0
  57. data/spec/chewy/strategy/lazy_sidekiq_spec.rb +214 -0
  58. data/spec/chewy_spec.rb +3 -3
  59. data/spec/support/active_record.rb +8 -1
  60. metadata +31 -8
  61. data/lib/chewy/backports/deep_dup.rb +0 -46
  62. data/lib/chewy/backports/duplicable.rb +0 -91
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 229c412b1e3464060b367c9c378d3662bccf2329b4e4eaa7617541243dbd6e38
4
- data.tar.gz: 9bc674550cd6152e8a18853fa46c91873c7a626c426cd50aa2cb8c67b1c9455e
3
+ metadata.gz: 37685481eff7312e08ba8d113f030ee5ab38e3414788c6cd2a0385829c99a712
4
+ data.tar.gz: d4a2a9d86d93e9a9c6ec5d11cabe51f67511f6b347faee3be9e276cf528c4dc2
5
5
  SHA512:
6
- metadata.gz: e3e8409f81bc2504e9edbf5093fc9329bf443862cb96e113c9c00fc586c1e0e3a4273b3d6e82466f9b3d301e3d9eafcc72892cce718aff7dcfbbce85aba1698d
7
- data.tar.gz: 33b5b0e21ef7c9896f1ed381130f225a1d51e073112213f631fb1c57c6d69c49af54817b1de3afc9be325c79ac24f9c173275b4448ffb7b43e82403bee03bd70
6
+ metadata.gz: c973f623b179e98284019a2b2dddc1cb7506719585064673cccab0e1c790f599dce43a1395e364d5ed8be814ee0af1fe4985e188f25dcaa32e75eefa57a4f6e3
7
+ data.tar.gz: bd1c55232fd02520382a20109899f3b4d59a174c1d1ff875592f08582b5f3072dd7d6f47760df3be58f38756718f31f802832476a1e0c64ebfb9909748e31eac
@@ -3,7 +3,7 @@ name: CI
3
3
  on: [push]
4
4
 
5
5
  jobs:
6
- tests:
6
+ ruby-2:
7
7
  runs-on: ubuntu-latest
8
8
  strategy:
9
9
  fail-fast: false
@@ -22,25 +22,33 @@ jobs:
22
22
  ruby-version: ${{ matrix.ruby }}
23
23
  bundler-cache: true
24
24
  - name: Run Elasticsearch
25
- uses: elastic/elastic-github-actions/elasticsearch@master
25
+ uses: elastic/elastic-github-actions/elasticsearch@9de0f78f306e4ebc0838f057e6b754364685e759
26
26
  with:
27
27
  stack-version: 7.10.1
28
28
  port: 9250
29
29
  - name: Tests
30
30
  run: bundle exec rspec
31
31
 
32
- ruby-3-0-activerecord-6-1:
32
+ ruby-3:
33
33
  runs-on: ubuntu-latest
34
+ strategy:
35
+ fail-fast: false
36
+ matrix:
37
+ ruby: [ '3.0', 3.1 ]
38
+ gemfile: [ rails.6.1.activerecord, rails.7.0.activerecord ]
39
+ name: ${{ matrix.ruby }}-${{ matrix.gemfile }}
40
+
34
41
  env:
35
- BUNDLE_GEMFILE: gemfiles/rails.6.1.activerecord.gemfile
42
+ BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
43
+
36
44
  steps:
37
45
  - uses: actions/checkout@v2
38
46
  - uses: ruby/setup-ruby@v1
39
47
  with:
40
- ruby-version: '3.0'
48
+ ruby-version: ${{ matrix.ruby }}
41
49
  bundler-cache: true
42
50
  - name: Run Elasticsearch
43
- uses: elastic/elastic-github-actions/elasticsearch@master
51
+ uses: elastic/elastic-github-actions/elasticsearch@9de0f78f306e4ebc0838f057e6b754364685e759
44
52
  with:
45
53
  stack-version: 7.10.1
46
54
  port: 9250
data/CHANGELOG.md CHANGED
@@ -8,6 +8,45 @@
8
8
 
9
9
  ### Bugs Fixed
10
10
 
11
+ ## 7.2.5 (2022-03-04)
12
+
13
+ ### New Features
14
+
15
+ * [#827](https://github.com/toptal/chewy/pull/827): Add `:lazy_sidekiq` strategy, that defers not only importing but also `update_index` callback evaluation for created and updated objects. ([@sl4vr][])
16
+ * [#827](https://github.com/toptal/chewy/pull/827): Add `:atomic_no_refresh` strategy. Like `:atomic`, but `refresh=false` parameter is set. ([@barthez][])
17
+ * [#827](https://github.com/toptal/chewy/pull/827): Add `:no_refresh` chain call to `update_index` matcher to ensure import was called with `refresh=false`. ([@barthez][])
18
+
19
+ ### Bugs Fixed
20
+
21
+ * [#835](https://github.com/toptal/chewy/pull/835): Support keyword arguments in named scopes. ([@milk1000cc][])
22
+
23
+ ## 7.2.4 (2022-02-03)
24
+
25
+ ### New Features
26
+
27
+ * [#760](https://github.com/toptal/chewy/pull/760): Replace parent-child mapping with a [join field](https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html#parent-child-mapping-types) ([@mrzasa][])
28
+
29
+ ### Bugs Fixed
30
+
31
+ * [#825](https://github.com/toptal/chewy/issues/825): Fix mismatch argument names at update_mapping rake task ([@AgeevAndrew][])
32
+ * [#832](https://github.com/toptal/chewy/issues/832): Fix "cannot load such file -- `i18n/core_ext/hash`" ([@chrisandreae][])
33
+
34
+ ## 7.2.3 (2021-10-29)
35
+
36
+ ### New Features
37
+
38
+ * [#801](https://github.com/toptal/chewy/pull/801): Add the [`track_total_hits`](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-your-data.html#track-total-hits) option to the query. ([@milk1000cc][])
39
+ * [#810](https://github.com/toptal/chewy/pull/810): Use `unsafe_load` when loading config to support Psych 4.
40
+ * [#819](https://github.com/toptal/chewy/pull/819): Add the [`ignore_unavailable`](https://www.elastic.co/guide/en/elasticsearch/reference/current/multi-index.html#multi-index) option to the request. ([@kolauren][])
41
+
42
+ ### Changes
43
+
44
+ * [#817](https://github.com/toptal/chewy/pull/817): Show warning message during rake chewy:reset or chewy:upgrade if journaling is disabled. ([@konalegi][])
45
+
46
+ ### Bugs Fixed
47
+
48
+ * [#816](https://github.com/toptal/chewy/pull/816): Move query [`preference`](https://www.elastic.co/guide/en/elasticsearch/reference/7.x/search-search.html#search-preference) from body to query parameters. ([@afg419][])
49
+
11
50
  ## 7.2.2 (2021-05-24)
12
51
 
13
52
  ### Changes
@@ -618,9 +657,11 @@
618
657
  * Initial version
619
658
 
620
659
  [@0x0badc0de]: https://github.com/0x0badc0de
660
+ [@AgeevAndrew]: https://github.com/AgeevAndrew
621
661
  [@aglushkov]: https://github.com/aglushkov
622
662
  [@AlexVPopov]: https://github.com/AlexVPopov
623
663
  [@AndreySavelyev]: https://github.com/AndreySavelyev
664
+ [@afg419]: https://github.com/afg419
624
665
  [@arion]: https://github.com/arion
625
666
  [@arturtr]: https://github.com/arturtr
626
667
  [@averell23]: https://github.com/averell23
@@ -631,6 +672,7 @@
631
672
  [@biow0lf]: https://github.com/biow0lf
632
673
  [@Borzik]: https://github.com/Borzik
633
674
  [@caldwecr]: https://github.com/caldwecr
675
+ [@chrisandreae]: https://github.com/chrisandreae
634
676
  [@clupprich]: https://github.com/clupprich
635
677
  [@dalthon]: https://github.com/dalthon
636
678
  [@davekaro]: https://github.com/davekaro
@@ -661,6 +703,7 @@
661
703
  [@josephchoe]: https://github.com/josephchoe
662
704
  [@jshirley]: https://github.com/jshirley
663
705
  [@ka8725]: https://github.com/ka8725
706
+ [@kolauren]: https://github.com/kolauren
664
707
  [@konalegi]: https://github.com/konalegi
665
708
  [@lardawge]: https://github.com/lardawge
666
709
  [@leemhenson]: https://github.com/leemhenson
@@ -674,6 +717,7 @@
674
717
  [@mattzollinhofer]: https://github.com/mattzollinhofer
675
718
  [@menglewis]: https://github.com/menglewis
676
719
  [@mikeyhogarth]: https://github.com/mikeyhogarth
720
+ [@milk1000cc]: https://github.com/milk1000cc
677
721
  [@mkcode]: https://github.com/mkcode
678
722
  [@mpeychich]: https://github.com/mpeychich
679
723
  [@mrbrdo]: https://github.com/mrbrdo
data/Gemfile CHANGED
@@ -19,3 +19,4 @@ gem 'redcarpet'
19
19
  gem 'yard'
20
20
 
21
21
  gem 'rexml' if RUBY_VERSION >= '3.0.0'
22
+ gem 'ruby2_keywords' if RUBY_VERSION < '2.7'
data/README.md CHANGED
@@ -340,7 +340,7 @@ Chewy.settings = {
340
340
  [See index settings here](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html).
341
341
  [See root object settings here](https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-field-mapping.html).
342
342
 
343
- See [mapping.rb](lib/chewy/type/mapping.rb) for more details.
343
+ See [mapping.rb](lib/chewy/index/mapping.rb) for more details.
344
344
 
345
345
  5. Add model-observing code
346
346
 
@@ -446,6 +446,23 @@ end
446
446
 
447
447
  See the section on *Script fields* for details on calculating distance in a search.
448
448
 
449
+ ### Join fields
450
+
451
+ You can use a [join field](https://www.elastic.co/guide/en/elasticsearch/reference/current/parent-join.html)
452
+ to implement parent-child relationships between documents.
453
+ It [replaces the old `parent_id` based parent-child mapping](https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html#parent-child-mapping-types)
454
+
455
+ To use it, you need to pass `relations` and `join` (with `type` and `id`) options:
456
+ ```ruby
457
+ field :hierarchy_link, type: :join, relations: {question: %i[answer comment], answer: :vote, vote: :subvote}, join: {type: :comment_type, id: :commented_id}
458
+ ```
459
+ assuming you have `comment_type` and `commented_id` fields in your model.
460
+
461
+ Note that when you reindex a parent, it's children and grandchildren will be reindexed as well.
462
+ This may require additional queries to the primary database and to elastisearch.
463
+
464
+ Also note that the join field doesn't support crutches (it should be a field directly defined on the model).
465
+
449
466
  ### Crutches™ technology
450
467
 
451
468
  Assume you are defining your index like this (product has_many categories through product_categories):
@@ -737,6 +754,23 @@ The default queue name is `chewy`, you can customize it in settings: `sidekiq.qu
737
754
  Chewy.settings[:sidekiq] = {queue: :low}
738
755
  ```
739
756
 
757
+ #### `:lazy_sidekiq`
758
+
759
+ This does the same thing as `:sidekiq`, but with lazy evaluation. Beware it does not allow you to use any non-persistent record state for indices and conditions because record will be re-fetched from database asynchronously using sidekiq. However for destroying records strategy will fallback to `:sidekiq` because it's not possible to re-fetch deleted records from database.
760
+
761
+ The purpose of this strategy is to improve the response time of the code that should update indexes, as it does not only defer actual ES calls to a background job but `update_index` callbacks evaluation (for created and updated objects) too. Similar to `:sidekiq`, index update is asynchronous so this strategy cannot be used when data and index synchronization is required.
762
+
763
+ ```ruby
764
+ Chewy.strategy(:lazy_sidekiq) do
765
+ City.popular.map(&:do_some_update_action!)
766
+ end
767
+ ```
768
+
769
+ The default queue name is `chewy`, you can customize it in settings: `sidekiq.queue_name`
770
+ ```
771
+ Chewy.settings[:sidekiq] = {queue: :low}
772
+ ```
773
+
740
774
  #### `:active_job`
741
775
 
742
776
  This does the same thing as `:atomic`, but using ActiveJob. This will inherit the ActiveJob configuration settings including the `active_job.queue_adapter` setting for the environment. Patch `Chewy::Strategy::ActiveJob::Worker` for index updates improving.
@@ -959,7 +993,7 @@ Main methods of the request DSL are: `query`, `filter` and `post_filter`, it is
959
993
  ```ruby
960
994
  CitiesIndex
961
995
  .filter(term: {name: 'Bangkok'})
962
- .query { match name: 'London' }
996
+ .query(match: {name: 'London'})
963
997
  .query.not(range: {population: {gt: 1_000_000}})
964
998
  ```
965
999
 
@@ -969,7 +1003,7 @@ You can query a set of indexes at once:
969
1003
  CitiesIndex.indices(CountriesIndex).query(match: {name: 'Some'})
970
1004
  ```
971
1005
 
972
- See https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html and https://github.com/elastic/elasticsearch-ruby/tree/master/elasticsearch-dsl for more details.
1006
+ See https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html and https://github.com/elastic/elasticsearch-dsl-ruby for more details.
973
1007
 
974
1008
  An important part of requests manipulation is merging. There are 4 methods to perform it: `merge`, `and`, `or`, `not`. See [Chewy::Search::QueryProxy](lib/chewy/search/query_proxy.rb) for details. Also, `only` and `except` methods help to remove unneeded parts of the request.
975
1009
 
data/chewy.gemspec CHANGED
@@ -31,6 +31,6 @@ Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength
31
31
  spec.add_development_dependency 'unparser'
32
32
 
33
33
  spec.add_dependency 'activesupport', '>= 5.2'
34
- spec.add_dependency 'elasticsearch', '>= 7.12.0'
34
+ spec.add_dependency 'elasticsearch', '>= 7.12.0', '< 7.14.0'
35
35
  spec.add_dependency 'elasticsearch-dsl'
36
36
  end
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'activejob', '~> 7.0.0'
4
+ gem 'activerecord', '~> 7.0.0'
5
+ gem 'activesupport', '~> 7.0.0'
6
+ gem 'kaminari-core', '~> 1.1.0', require: false
7
+ gem 'parallel', require: false
8
+ gem 'rspec_junit_formatter', '~> 0.4.1'
9
+ gem 'sidekiq', require: false
10
+
11
+ gem 'rexml' if RUBY_VERSION >= '3.0.0'
12
+
13
+ gemspec path: '../'
data/lib/chewy/config.rb CHANGED
@@ -32,7 +32,7 @@ module Chewy
32
32
  # Set number_of_replicas to 0 before reset and put the original value after
33
33
  # https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html
34
34
  :reset_no_replicas,
35
- # Refresh or not when import async (sidekiq, activejob)
35
+ # Refresh or not when import async (sidekiq, lazy_sidekiq, activejob)
36
36
  :disable_refresh_async,
37
37
  # Default options for root of Chewy type. Allows to set default options
38
38
  # for type mappings like `_all`.
@@ -133,7 +133,7 @@ module Chewy
133
133
 
134
134
  if File.exist?(file)
135
135
  yaml = ERB.new(File.read(file)).result
136
- hash = YAML.load(yaml) # rubocop:disable Security/YAMLLoad
136
+ hash = YAML.respond_to?(:unsafe_load) ? YAML.unsafe_load(yaml) : YAML.load(yaml) # rubocop:disable Security/YAMLLoad
137
137
  hash[Rails.env].try(:deep_symbolize_keys) if hash
138
138
  end
139
139
  end || {}
data/lib/chewy/errors.rb CHANGED
@@ -30,4 +30,10 @@ module Chewy
30
30
  super message
31
31
  end
32
32
  end
33
+
34
+ class InvalidJoinFieldType < Error
35
+ def initialize(join_field_type, join_field_name, relations)
36
+ super("`#{join_field_type}` set for the join field `#{join_field_name}` is not on the :relations list (#{relations})")
37
+ end
38
+ end
33
39
  end
@@ -1,8 +1,8 @@
1
1
  module Chewy
2
2
  module Fields
3
3
  class Base
4
- attr_reader :name, :options, :value, :children
5
- attr_accessor :parent
4
+ attr_reader :name, :join_options, :options, :children
5
+ attr_accessor :parent # used by Chewy::Index::Mapping to expand nested fields
6
6
 
7
7
  def initialize(name, value: nil, **options)
8
8
  @name = name.to_sym
@@ -10,9 +10,11 @@ module Chewy
10
10
  update_options!(**options)
11
11
  @value = value
12
12
  @children = []
13
+ @allowed_relations = find_allowed_relations(options[:relations]) # for join fields
13
14
  end
14
15
 
15
16
  def update_options!(**options)
17
+ @join_options = options.delete(:join) || {}
16
18
  @options = options
17
19
  end
18
20
 
@@ -53,30 +55,70 @@ module Chewy
53
55
  {name => result}
54
56
  end
55
57
 
58
+ def value
59
+ if join_field?
60
+ join_type = join_options[:type]
61
+ join_id = join_options[:id]
62
+ # memoize
63
+ @value ||= proc do |object|
64
+ validate_join_type!(value_by_name_proc(join_type).call(object))
65
+ # If it's a join field and it has join_id, the value is compound and contains
66
+ # both name (type) and id of the parent object
67
+ if value_by_name_proc(join_id).call(object).present?
68
+ {
69
+ name: value_by_name_proc(join_type).call(object), # parent type
70
+ parent: value_by_name_proc(join_id).call(object) # parent id
71
+ }
72
+ else
73
+ value_by_name_proc(join_type).call(object)
74
+ end
75
+ end
76
+ else
77
+ @value
78
+ end
79
+ end
80
+
56
81
  private
57
82
 
58
83
  def geo_point?
59
84
  @options[:type].to_s == 'geo_point'
60
85
  end
61
86
 
87
+ def join_field?
88
+ @options[:type].to_s == 'join'
89
+ end
90
+
62
91
  def ignore_blank?
63
92
  @options.fetch(:ignore_blank) { geo_point? }
64
93
  end
65
94
 
66
95
  def evaluate(objects)
67
- object = objects.first
68
-
69
96
  if value.is_a?(Proc)
70
- if value.arity.zero?
71
- object.instance_exec(&value)
72
- elsif value.arity.negative?
73
- value.call(*object)
74
- else
75
- value.call(*objects.first(value.arity))
76
- end
97
+ value_by_proc(objects, value)
77
98
  else
78
- message = value.is_a?(Symbol) || value.is_a?(String) ? value.to_sym : name
99
+ value_by_name(objects, value)
100
+ end
101
+ end
79
102
 
103
+ def value_by_proc(objects, value)
104
+ object = objects.first
105
+ if value.arity.zero?
106
+ object.instance_exec(&value)
107
+ elsif value.arity.negative?
108
+ value.call(*object)
109
+ else
110
+ value.call(*objects.first(value.arity))
111
+ end
112
+ end
113
+
114
+ def value_by_name(objects, value)
115
+ object = objects.first
116
+ message = value.is_a?(Symbol) || value.is_a?(String) ? value.to_sym : name
117
+ value_by_name_proc(message).call(object)
118
+ end
119
+
120
+ def value_by_name_proc(message)
121
+ proc do |object|
80
122
  if object.is_a?(Hash)
81
123
  if object.key?(message)
82
124
  object[message]
@@ -89,6 +131,20 @@ module Chewy
89
131
  end
90
132
  end
91
133
 
134
+ def validate_join_type!(type)
135
+ return unless type
136
+ return if @allowed_relations.include?(type.to_sym)
137
+
138
+ raise Chewy::InvalidJoinFieldType.new(type, @name, options[:relations])
139
+ end
140
+
141
+ def find_allowed_relations(relations)
142
+ return [] unless relations
143
+ return relations unless relations.is_a?(Hash)
144
+
145
+ (relations.keys + relations.values).flatten.uniq
146
+ end
147
+
92
148
  def compose_children(value, *parent_objects)
93
149
  return unless value
94
150
 
@@ -1,7 +1,7 @@
1
1
  module Chewy
2
2
  module Fields
3
3
  class Root < Chewy::Fields::Base
4
- attr_reader :dynamic_templates, :id, :parent, :parent_id
4
+ attr_reader :dynamic_templates, :id
5
5
 
6
6
  def initialize(name, **options)
7
7
  super(name, **options)
@@ -12,9 +12,7 @@ module Chewy
12
12
 
13
13
  def update_options!(**options)
14
14
  @id = options.fetch(:id, options.fetch(:_id, @id))
15
- @parent = options.fetch(:parent, options.fetch(:_parent, @parent))
16
- @parent_id = options.fetch(:parent_id, @parent_id)
17
- @options.merge!(options.except(:id, :_id, :parent, :_parent, :parent_id, :type))
15
+ @options.merge!(options.except(:id, :_id, :type))
18
16
  end
19
17
 
20
18
  def mappings_hash
@@ -29,7 +27,7 @@ module Chewy
29
27
  mappings[name]
30
28
  end
31
29
 
32
- def dynamic_template(*args)
30
+ ruby2_keywords def dynamic_template(*args)
33
31
  options = args.extract_options!.deep_symbolize_keys
34
32
  if args.first
35
33
  template_name = :"template_#{dynamic_templates.count.next}"
@@ -50,12 +48,6 @@ module Chewy
50
48
  end
51
49
  end
52
50
 
53
- def compose_parent(object)
54
- return unless parent_id
55
-
56
- parent_id.arity.zero? ? object.instance_exec(&parent_id) : parent_id.call(object)
57
- end
58
-
59
51
  def compose_id(object)
60
52
  return unless id
61
53
 
@@ -156,11 +156,11 @@ module Chewy
156
156
  suffixed_name = index_name(suffix: suffix)
157
157
 
158
158
  optimize_index_settings suffixed_name
159
- result = import import_options.merge(
159
+ result = import(**import_options.merge(
160
160
  suffix: suffix,
161
161
  journal: journal,
162
162
  refresh: !Chewy.reset_disable_refresh_interval
163
- )
163
+ ))
164
164
  original_index_settings suffixed_name
165
165
 
166
166
  delete if indexes.blank?
@@ -176,7 +176,7 @@ module Chewy
176
176
  result
177
177
  else
178
178
  purge!
179
- import import_options.merge(journal: journal)
179
+ import(**import_options.merge(journal: journal))
180
180
  end
181
181
 
182
182
  specification.lock!
@@ -94,6 +94,11 @@ module Chewy
94
94
  object_class.connection.execute(sql).map(&converter)
95
95
  end
96
96
 
97
+ def raw(scope, converter)
98
+ sql = scope.to_sql
99
+ object_class.connection.execute(sql).map(&converter)
100
+ end
101
+
97
102
  def relation_class
98
103
  ::ActiveRecord::Relation
99
104
  end
@@ -85,7 +85,7 @@ module Chewy
85
85
  # @param args [Array<#to_json>]
86
86
  # @option options [Integer] :batch_size import processing batch size
87
87
  # @return [true, false]
88
- def import(*args, &block)
88
+ ruby2_keywords def import(*args, &block)
89
89
  collection, options = import_args(*args)
90
90
  import_objects(collection, options, &block)
91
91
  end
@@ -113,7 +113,7 @@ module Chewy
113
113
  # end
114
114
  #
115
115
  # @see Chewy::Index::Adapter::Base#import_fields
116
- def import_fields(*args, &block)
116
+ ruby2_keywords def import_fields(*args, &block)
117
117
  return enum_for(:import_fields, *args) unless block_given?
118
118
 
119
119
  options = args.extract_options!
@@ -139,7 +139,7 @@ module Chewy
139
139
  # For the Object adapter returns the objects themselves in batches.
140
140
  #
141
141
  # @see Chewy::Index::Adapter::Base#import_references
142
- def import_references(*args, &block)
142
+ ruby2_keywords def import_references(*args, &block)
143
143
  return enum_for(:import_references, *args) unless block_given?
144
144
 
145
145
  collection, options = import_args(*args)
@@ -72,7 +72,7 @@ module Chewy
72
72
  # # or
73
73
  # UsersIndex.import users.map(&:id) # user ids will be deleted from index
74
74
  #
75
- def import(*args, &block)
75
+ ruby2_keywords def import(*args, &block)
76
76
  collection, options = import_args(*args)
77
77
 
78
78
  if !collection.is_a?(relation_class) || options[:direct_import]
@@ -82,7 +82,7 @@ module Chewy
82
82
  end
83
83
  end
84
84
 
85
- def import_fields(*args, &block)
85
+ ruby2_keywords def import_fields(*args, &block)
86
86
  return enum_for(:import_fields, *args) unless block_given?
87
87
 
88
88
  collection, options = import_args(*args)
@@ -101,11 +101,13 @@ module Chewy
101
101
  additional_scope = options[options[:_index].to_sym].try(:[], :scope) || options[:scope]
102
102
 
103
103
  loaded_objects = load_scope_objects(scope, additional_scope)
104
- .index_by do |object|
105
- object.public_send(primary_key).to_s
106
- end
104
+ loaded_objects = raw(loaded_objects, options[:raw_import]) if options[:raw_import]
105
+
106
+ indexed_objects = loaded_objects.index_by do |object|
107
+ object.public_send(primary_key).to_s
108
+ end
107
109
 
108
- ids.map { |id| loaded_objects[id.to_s] }
110
+ ids.map { |id| indexed_objects[id.to_s] }
109
111
  end
110
112
 
111
113
  private