chewy 0.10.1 → 7.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. checksums.yaml +5 -5
  2. data/.circleci/config.yml +240 -0
  3. data/.github/ISSUE_TEMPLATE/bug_report.md +39 -0
  4. data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  5. data/.github/PULL_REQUEST_TEMPLATE.md +16 -0
  6. data/.rubocop.yml +25 -25
  7. data/Appraisals +12 -10
  8. data/CHANGELOG.md +312 -331
  9. data/CODE_OF_CONDUCT.md +14 -0
  10. data/CONTRIBUTING.md +63 -0
  11. data/Gemfile +5 -1
  12. data/LICENSE.txt +1 -1
  13. data/README.md +143 -77
  14. data/chewy.gemspec +10 -12
  15. data/gemfiles/{rails.4.2.mongoid.5.1.gemfile → rails.5.2.activerecord.gemfile} +6 -4
  16. data/gemfiles/{rails.4.2.activerecord.gemfile → rails.5.2.mongoid.6.4.gemfile} +6 -4
  17. data/gemfiles/{rails.4.0.activerecord.gemfile → rails.6.0.activerecord.gemfile} +6 -3
  18. data/gemfiles/rails.6.1.activerecord.gemfile +19 -0
  19. data/gemfiles/sequel.4.45.gemfile +2 -2
  20. data/lib/chewy.rb +2 -1
  21. data/lib/chewy/backports/duplicable.rb +1 -1
  22. data/lib/chewy/config.rb +10 -39
  23. data/lib/chewy/fields/base.rb +9 -3
  24. data/lib/chewy/fields/root.rb +13 -9
  25. data/lib/chewy/index.rb +3 -1
  26. data/lib/chewy/index/actions.rb +28 -16
  27. data/lib/chewy/index/aliases.rb +16 -5
  28. data/lib/chewy/index/settings.rb +2 -0
  29. data/lib/chewy/index/specification.rb +12 -10
  30. data/lib/chewy/minitest/helpers.rb +6 -6
  31. data/lib/chewy/minitest/search_index_receiver.rb +17 -17
  32. data/lib/chewy/multi_search.rb +62 -0
  33. data/lib/chewy/railtie.rb +3 -5
  34. data/lib/chewy/rake_helper.rb +5 -5
  35. data/lib/chewy/rspec/update_index.rb +3 -5
  36. data/lib/chewy/search.rb +4 -11
  37. data/lib/chewy/search/loader.rb +1 -1
  38. data/lib/chewy/search/pagination/will_paginate.rb +1 -1
  39. data/lib/chewy/search/parameters.rb +24 -6
  40. data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
  41. data/lib/chewy/search/parameters/concerns/query_storage.rb +4 -3
  42. data/lib/chewy/search/parameters/indices.rb +123 -0
  43. data/lib/chewy/search/parameters/none.rb +1 -3
  44. data/lib/chewy/search/request.rb +101 -74
  45. data/lib/chewy/search/scrolling.rb +7 -6
  46. data/lib/chewy/stash.rb +31 -22
  47. data/lib/chewy/strategy/active_job.rb +1 -1
  48. data/lib/chewy/strategy/atomic.rb +1 -1
  49. data/lib/chewy/strategy/sidekiq.rb +1 -1
  50. data/lib/chewy/type.rb +5 -2
  51. data/lib/chewy/type/adapter/active_record.rb +1 -1
  52. data/lib/chewy/type/adapter/base.rb +9 -9
  53. data/lib/chewy/type/adapter/mongoid.rb +2 -4
  54. data/lib/chewy/type/adapter/orm.rb +7 -4
  55. data/lib/chewy/type/adapter/sequel.rb +5 -7
  56. data/lib/chewy/type/crutch.rb +1 -1
  57. data/lib/chewy/type/import.rb +16 -5
  58. data/lib/chewy/type/import/bulk_builder.rb +1 -1
  59. data/lib/chewy/type/import/bulk_request.rb +4 -2
  60. data/lib/chewy/type/import/journal_builder.rb +3 -3
  61. data/lib/chewy/type/import/routine.rb +3 -3
  62. data/lib/chewy/type/mapping.rb +42 -36
  63. data/lib/chewy/type/observe.rb +16 -12
  64. data/lib/chewy/type/syncer.rb +6 -7
  65. data/lib/chewy/type/witchcraft.rb +5 -3
  66. data/lib/chewy/type/wrapper.rb +14 -4
  67. data/lib/chewy/version.rb +1 -1
  68. data/lib/sequel/plugins/chewy_observe.rb +4 -19
  69. data/migration_guide.md +34 -0
  70. data/spec/chewy/config_spec.rb +16 -21
  71. data/spec/chewy/fields/base_spec.rb +68 -70
  72. data/spec/chewy/fields/root_spec.rb +13 -13
  73. data/spec/chewy/index/actions_spec.rb +157 -38
  74. data/spec/chewy/index/aliases_spec.rb +3 -3
  75. data/spec/chewy/index/specification_spec.rb +25 -16
  76. data/spec/chewy/index_spec.rb +75 -45
  77. data/spec/chewy/journal_spec.rb +33 -29
  78. data/spec/chewy/minitest/search_index_receiver_spec.rb +11 -9
  79. data/spec/chewy/multi_search_spec.rb +85 -0
  80. data/spec/chewy/rake_helper_spec.rb +123 -95
  81. data/spec/chewy/rspec/update_index_spec.rb +47 -46
  82. data/spec/chewy/runtime_spec.rb +2 -2
  83. data/spec/chewy/search/parameters/indices_spec.rb +190 -0
  84. data/spec/chewy/search/parameters/none_spec.rb +1 -1
  85. data/spec/chewy/search/parameters_spec.rb +21 -4
  86. data/spec/chewy/search/request_spec.rb +103 -70
  87. data/spec/chewy/search/response_spec.rb +27 -17
  88. data/spec/chewy/search/scrolling_spec.rb +27 -17
  89. data/spec/chewy/search_spec.rb +49 -35
  90. data/spec/chewy/stash_spec.rb +15 -13
  91. data/spec/chewy/strategy/active_job_spec.rb +15 -2
  92. data/spec/chewy/strategy/shoryuken_spec.rb +8 -2
  93. data/spec/chewy/strategy/sidekiq_spec.rb +6 -2
  94. data/spec/chewy/type/adapter/active_record_spec.rb +16 -4
  95. data/spec/chewy/type/import/bulk_builder_spec.rb +9 -94
  96. data/spec/chewy/type/import/journal_builder_spec.rb +17 -15
  97. data/spec/chewy/type/import_spec.rb +15 -0
  98. data/spec/chewy/type/mapping_spec.rb +51 -18
  99. data/spec/chewy/type/observe_spec.rb +4 -4
  100. data/spec/chewy/type/witchcraft_spec.rb +15 -0
  101. data/spec/chewy/type/wrapper_spec.rb +3 -1
  102. data/spec/chewy_spec.rb +0 -7
  103. data/spec/spec_helper.rb +5 -1
  104. data/spec/support/active_record.rb +21 -0
  105. metadata +51 -116
  106. data/.travis.yml +0 -53
  107. data/LEGACY_DSL.md +0 -497
  108. data/gemfiles/rails.4.1.activerecord.gemfile +0 -14
  109. data/gemfiles/rails.5.0.activerecord.gemfile +0 -15
  110. data/gemfiles/rails.5.0.mongoid.6.0.gemfile +0 -15
  111. data/gemfiles/rails.5.1.activerecord.gemfile +0 -15
  112. data/gemfiles/rails.5.1.mongoid.6.1.gemfile +0 -15
  113. data/lib/chewy/query.rb +0 -1098
  114. data/lib/chewy/query/compose.rb +0 -68
  115. data/lib/chewy/query/criteria.rb +0 -191
  116. data/lib/chewy/query/filters.rb +0 -227
  117. data/lib/chewy/query/loading.rb +0 -111
  118. data/lib/chewy/query/nodes/and.rb +0 -25
  119. data/lib/chewy/query/nodes/base.rb +0 -17
  120. data/lib/chewy/query/nodes/bool.rb +0 -34
  121. data/lib/chewy/query/nodes/equal.rb +0 -34
  122. data/lib/chewy/query/nodes/exists.rb +0 -20
  123. data/lib/chewy/query/nodes/expr.rb +0 -28
  124. data/lib/chewy/query/nodes/field.rb +0 -110
  125. data/lib/chewy/query/nodes/has_child.rb +0 -15
  126. data/lib/chewy/query/nodes/has_parent.rb +0 -15
  127. data/lib/chewy/query/nodes/has_relation.rb +0 -59
  128. data/lib/chewy/query/nodes/match_all.rb +0 -11
  129. data/lib/chewy/query/nodes/missing.rb +0 -20
  130. data/lib/chewy/query/nodes/not.rb +0 -25
  131. data/lib/chewy/query/nodes/or.rb +0 -25
  132. data/lib/chewy/query/nodes/prefix.rb +0 -19
  133. data/lib/chewy/query/nodes/query.rb +0 -20
  134. data/lib/chewy/query/nodes/range.rb +0 -63
  135. data/lib/chewy/query/nodes/raw.rb +0 -15
  136. data/lib/chewy/query/nodes/regexp.rb +0 -35
  137. data/lib/chewy/query/nodes/script.rb +0 -20
  138. data/lib/chewy/query/pagination.rb +0 -25
  139. data/spec/chewy/query/criteria_spec.rb +0 -700
  140. data/spec/chewy/query/filters_spec.rb +0 -201
  141. data/spec/chewy/query/loading_spec.rb +0 -124
  142. data/spec/chewy/query/nodes/and_spec.rb +0 -12
  143. data/spec/chewy/query/nodes/bool_spec.rb +0 -14
  144. data/spec/chewy/query/nodes/equal_spec.rb +0 -32
  145. data/spec/chewy/query/nodes/exists_spec.rb +0 -18
  146. data/spec/chewy/query/nodes/has_child_spec.rb +0 -59
  147. data/spec/chewy/query/nodes/has_parent_spec.rb +0 -59
  148. data/spec/chewy/query/nodes/match_all_spec.rb +0 -11
  149. data/spec/chewy/query/nodes/missing_spec.rb +0 -16
  150. data/spec/chewy/query/nodes/not_spec.rb +0 -13
  151. data/spec/chewy/query/nodes/or_spec.rb +0 -12
  152. data/spec/chewy/query/nodes/prefix_spec.rb +0 -16
  153. data/spec/chewy/query/nodes/query_spec.rb +0 -12
  154. data/spec/chewy/query/nodes/range_spec.rb +0 -32
  155. data/spec/chewy/query/nodes/raw_spec.rb +0 -11
  156. data/spec/chewy/query/nodes/regexp_spec.rb +0 -43
  157. data/spec/chewy/query/nodes/script_spec.rb +0 -15
  158. data/spec/chewy/query/pagination/kaminari_spec.rb +0 -5
  159. data/spec/chewy/query/pagination/will_paginate_spec.rb +0 -5
  160. data/spec/chewy/query/pagination_spec.rb +0 -39
  161. data/spec/chewy/query_spec.rb +0 -636
  162. data/spec/chewy/search/parameters/indices_boost_spec.rb +0 -83
@@ -10,13 +10,13 @@ describe Chewy::Index::Aliases do
10
10
 
11
11
  context do
12
12
  before { DummiesIndex.create! }
13
- specify { expect(DummiesIndex.indexes).to eq([]) }
13
+ specify { expect(DummiesIndex.indexes).to eq(['dummies']) }
14
14
  end
15
15
 
16
16
  context do
17
17
  before { DummiesIndex.create! }
18
18
  before { Chewy.client.indices.put_alias index: 'dummies', name: 'dummies_2013' }
19
- specify { expect(DummiesIndex.indexes).to eq([]) }
19
+ specify { expect(DummiesIndex.indexes).to eq(['dummies']) }
20
20
  end
21
21
 
22
22
  context do
@@ -43,7 +43,7 @@ describe Chewy::Index::Aliases do
43
43
 
44
44
  context do
45
45
  before { DummiesIndex.create! '2013' }
46
- specify { expect(DummiesIndex.aliases).to eq([]) }
46
+ specify { expect(DummiesIndex.aliases).to eq(['dummies']) }
47
47
  end
48
48
  end
49
49
  end
@@ -6,7 +6,7 @@ describe Chewy::Index::Specification do
6
6
  let(:index1) do
7
7
  stub_index(:places) do
8
8
  define_type(:city) do
9
- field :name, type: 'string'
9
+ field :founded_on, type: 'date'
10
10
  end
11
11
  end
12
12
  end
@@ -15,7 +15,7 @@ describe Chewy::Index::Specification do
15
15
  stub_index(:places) do
16
16
  settings analyzer: {}
17
17
  define_type(:city) do
18
- field :name, type: 'string'
18
+ field :founded_on, type: 'date'
19
19
  end
20
20
  end
21
21
  end
@@ -23,7 +23,7 @@ describe Chewy::Index::Specification do
23
23
  let(:index3) do
24
24
  stub_index(:places) do
25
25
  define_type(:city) do
26
- field :name, type: 'string'
26
+ field :founded_on, type: 'date'
27
27
  field :population, type: 'integer'
28
28
  end
29
29
  end
@@ -33,7 +33,7 @@ describe Chewy::Index::Specification do
33
33
  stub_index(:places) do
34
34
  define_type(:city) do
35
35
  field :population, type: 'integer'
36
- field :name, type: 'string'
36
+ field :founded_on, type: 'date'
37
37
  end
38
38
  end
39
39
  end
@@ -55,11 +55,14 @@ describe Chewy::Index::Specification do
55
55
  describe '#lock!' do
56
56
  specify do
57
57
  expect { specification1.lock! }.to change { Chewy::Stash::Specification.all.hits }.from([]).to([{
58
- '_index' => 'chewy_stash',
58
+ '_index' => 'chewy_specifications',
59
59
  '_type' => 'specification',
60
60
  '_id' => 'places',
61
61
  '_score' => 1.0,
62
- '_source' => {'value' => '{"settings":{"index":{"number_of_shards":1,"number_of_replicas":0}},"mappings":{"city":{"properties":{"name":{"type":"string"}}}}}'}
62
+ '_source' => {'specification' => Base64.encode64({
63
+ 'settings' => {'index' => {'number_of_shards' => 1, 'number_of_replicas' => 0}},
64
+ 'mappings' => {'city' => {'properties' => {'founded_on' => {'type' => 'date'}}}}
65
+ }.to_json)}
63
66
  }])
64
67
  end
65
68
 
@@ -68,17 +71,23 @@ describe Chewy::Index::Specification do
68
71
 
69
72
  specify do
70
73
  expect { specification5.lock! }.to change { Chewy::Stash::Specification.all.hits }.to([{
71
- '_index' => 'chewy_stash',
74
+ '_index' => 'chewy_specifications',
72
75
  '_type' => 'specification',
73
76
  '_id' => 'places',
74
77
  '_score' => 1.0,
75
- '_source' => {'value' => '{"settings":{"index":{"number_of_shards":1,"number_of_replicas":0}},"mappings":{"city":{"properties":{"name":{"type":"string"}}}}}'}
78
+ '_source' => {'specification' => Base64.encode64({
79
+ 'settings' => {'index' => {'number_of_shards' => 1, 'number_of_replicas' => 0}},
80
+ 'mappings' => {'city' => {'properties' => {'founded_on' => {'type' => 'date'}}}}
81
+ }.to_json)}
76
82
  }, {
77
- '_index' => 'chewy_stash',
83
+ '_index' => 'chewy_specifications',
78
84
  '_type' => 'specification',
79
85
  '_id' => 'namespace/cities',
80
86
  '_score' => 1.0,
81
- '_source' => {'value' => '{"settings":{"index":{"number_of_shards":1,"number_of_replicas":0}},"mappings":{"city":{"properties":{"population":{"type":"integer"}}}}}'}
87
+ '_source' => {'specification' => Base64.encode64({
88
+ 'settings' => {'index' => {'number_of_shards' => 1, 'number_of_replicas' => 0}},
89
+ 'mappings' => {'city' => {'properties' => {'population' => {'type' => 'integer'}}}}
90
+ }.to_json)}
82
91
  }])
83
92
  end
84
93
  end
@@ -88,7 +97,7 @@ describe Chewy::Index::Specification do
88
97
  specify do
89
98
  expect { specification1.lock! }.to change { specification1.locked }.from({}).to(
90
99
  'settings' => {'index' => {'number_of_shards' => 1, 'number_of_replicas' => 0}},
91
- 'mappings' => {'city' => {'properties' => {'name' => {'type' => 'string'}}}}
100
+ 'mappings' => {'city' => {'properties' => {'founded_on' => {'type' => 'date'}}}}
92
101
  )
93
102
  end
94
103
 
@@ -105,20 +114,20 @@ describe Chewy::Index::Specification do
105
114
  specify do
106
115
  expect { specification2.lock! }.to change { specification2.locked }.from(
107
116
  'settings' => {'index' => {'number_of_shards' => 1, 'number_of_replicas' => 0}},
108
- 'mappings' => {'city' => {'properties' => {'name' => {'type' => 'string'}}}}
117
+ 'mappings' => {'city' => {'properties' => {'founded_on' => {'type' => 'date'}}}}
109
118
  ).to(
110
119
  'settings' => {'analyzer' => {}, 'index' => {'number_of_shards' => 1, 'number_of_replicas' => 0}},
111
- 'mappings' => {'city' => {'properties' => {'name' => {'type' => 'string'}}}}
120
+ 'mappings' => {'city' => {'properties' => {'founded_on' => {'type' => 'date'}}}}
112
121
  )
113
122
  end
114
123
 
115
124
  specify do
116
125
  expect { specification3.lock! }.to change { specification3.locked }.from(
117
126
  'settings' => {'index' => {'number_of_shards' => 1, 'number_of_replicas' => 0}},
118
- 'mappings' => {'city' => {'properties' => {'name' => {'type' => 'string'}}}}
127
+ 'mappings' => {'city' => {'properties' => {'founded_on' => {'type' => 'date'}}}}
119
128
  ).to(
120
129
  'settings' => {'index' => {'number_of_shards' => 1, 'number_of_replicas' => 0}},
121
- 'mappings' => {'city' => {'properties' => {'name' => {'type' => 'string'}, 'population' => {'type' => 'integer'}}}}
130
+ 'mappings' => {'city' => {'properties' => {'founded_on' => {'type' => 'date'}, 'population' => {'type' => 'integer'}}}}
122
131
  )
123
132
  end
124
133
  end
@@ -127,7 +136,7 @@ describe Chewy::Index::Specification do
127
136
  describe '#current' do
128
137
  specify do
129
138
  expect(specification2.current).to eq(
130
- 'mappings' => {'city' => {'properties' => {'name' => {'type' => 'string'}}}},
139
+ 'mappings' => {'city' => {'properties' => {'founded_on' => {'type' => 'date'}}}},
131
140
  'settings' => {'analyzer' => {}, 'index' => {'number_of_shards' => 1, 'number_of_replicas' => 0}}
132
141
  )
133
142
  end
@@ -12,8 +12,11 @@ describe Chewy::Index do
12
12
  stub_model(:city)
13
13
  stub_model(:country)
14
14
 
15
- stub_index(:places) do
15
+ stub_index(:cities) do
16
16
  define_type City
17
+ end
18
+
19
+ stub_index(:countries) do
17
20
  define_type Country
18
21
  end
19
22
  end
@@ -22,23 +25,23 @@ describe Chewy::Index do
22
25
  let!(:countries) { Array.new(2) { |i| Country.create! id: i + 1 } }
23
26
 
24
27
  specify do
25
- expect { PlacesIndex.import }.to update_index(PlacesIndex::City).and_reindex(cities)
26
- expect { PlacesIndex.import }.to update_index(PlacesIndex::Country).and_reindex(countries)
28
+ expect { CitiesIndex.import }.to update_index(CitiesIndex).and_reindex(cities)
29
+ expect { CountriesIndex.import }.to update_index(CountriesIndex).and_reindex(countries)
27
30
  end
28
31
 
29
32
  specify do
30
- expect { PlacesIndex.import city: cities.first }.to update_index(PlacesIndex::City).and_reindex(cities.first).only
31
- expect { PlacesIndex.import city: cities.first }.to update_index(PlacesIndex::Country).and_reindex(countries)
33
+ expect { CitiesIndex.import city: cities.first }.to update_index(CitiesIndex).and_reindex(cities.first).only
34
+ expect { CountriesIndex.import city: cities.first }.to update_index(CountriesIndex).and_reindex(countries)
32
35
  end
33
36
 
34
37
  specify do
35
- expect { PlacesIndex.import city: cities.first, country: countries.last }.to update_index(PlacesIndex::City).and_reindex(cities.first).only
36
- expect { PlacesIndex.import city: cities.first, country: countries.last }.to update_index(PlacesIndex::Country).and_reindex(countries.last).only
38
+ expect { CitiesIndex.import city: cities.first, country: countries.last }.to update_index(CitiesIndex).and_reindex(cities.first).only
39
+ expect { CountriesIndex.import city: cities.first, country: countries.last }.to update_index(CountriesIndex).and_reindex(countries.last).only
37
40
  end
38
41
 
39
42
  specify do
40
- expect(PlacesIndex.client).to receive(:bulk).with(hash_including(refresh: false)).twice
41
- PlacesIndex.import city: cities.first, refresh: false
43
+ expect(CitiesIndex.client).to receive(:bulk).with(hash_including(refresh: false)).once
44
+ CitiesIndex.import city: cities.first, refresh: false
42
45
  end
43
46
  end
44
47
 
@@ -101,43 +104,27 @@ describe Chewy::Index do
101
104
 
102
105
  context do
103
106
  before { stub_class('City') }
104
- before { stub_class('City::District', City) }
107
+ before { stub_class('Country') }
105
108
 
106
109
  specify do
107
110
  expect do
108
111
  Kernel.eval <<-DUMMY_CITY_INDEX
109
- class DummyCityIndex < Chewy::Index
112
+ class DummyCityIndex2 < Chewy::Index
110
113
  define_type City
111
- define_type City::District
114
+ define_type Country
112
115
  end
113
116
  DUMMY_CITY_INDEX
114
- end.not_to raise_error
115
- end
117
+ end.to raise_error(/Multiple types are deprecated/)
116
118
 
117
- specify do
118
119
  expect do
119
120
  Kernel.eval <<-DUMMY_CITY_INDEX
120
121
  class DummyCityIndex2 < Chewy::Index
121
- define_type City
122
122
  define_type City::Nothing
123
123
  end
124
124
  DUMMY_CITY_INDEX
125
125
  end.to raise_error(NameError)
126
126
  end
127
127
  end
128
-
129
- context 'type methods should be deprecated and can\'t redefine existing ones' do
130
- before do
131
- stub_index(:places) do
132
- def self.city; end
133
- define_type :city
134
- define_type :country
135
- end
136
- end
137
-
138
- specify { expect(PlacesIndex.city).to be_nil }
139
- specify { expect(PlacesIndex::Country).to be < Chewy::Type }
140
- end
141
128
  end
142
129
 
143
130
  describe '.type_hash' do
@@ -191,16 +178,44 @@ describe Chewy::Index do
191
178
  stub_index(:places) do
192
179
  def self.by_rating; end
193
180
 
194
- def self.by_name; end
181
+ def self.colors(*colors)
182
+ filter(terms: {colors: colors.flatten(1).map(&:to_s)})
183
+ end
195
184
 
196
185
  define_type :city do
197
186
  def self.by_id; end
187
+ field :colors
198
188
  end
199
189
  end
200
190
  end
201
191
 
202
192
  specify { expect(described_class.scopes).to eq([]) }
203
- specify { expect(PlacesIndex.scopes).to match_array(%i[by_rating by_name]) }
193
+ specify { expect(PlacesIndex.scopes).to match_array(%i[by_rating colors]) }
194
+
195
+ context do
196
+ before do
197
+ Chewy.massacre
198
+ PlacesIndex::City.import!(
199
+ double(colors: ['red']),
200
+ double(colors: %w[red green]),
201
+ double(colors: %w[green yellow])
202
+ )
203
+ end
204
+
205
+ specify do
206
+ # This `blank?`` call is for the messed scopes bug reproduction. See #573
207
+ PlacesIndex::City.blank?
208
+ expect(PlacesIndex.colors(:green).map(&:colors))
209
+ .to contain_exactly(%w[red green], %w[green yellow])
210
+ end
211
+
212
+ specify do
213
+ # This `blank?` call is for the messed scopes bug reproduction. See #573
214
+ PlacesIndex::City.blank?
215
+ expect(PlacesIndex::City.colors(:green).map(&:colors))
216
+ .to contain_exactly(%w[red green], %w[green yellow])
217
+ end
218
+ end
204
219
  end
205
220
 
206
221
  describe '.settings_hash' do
@@ -216,19 +231,9 @@ describe Chewy::Index do
216
231
  specify do
217
232
  expect(stub_index(:documents) do
218
233
  define_type :document do
219
- field :name, type: 'string'
234
+ field :date, type: 'date'
220
235
  end
221
- end.mappings_hash).to eq(mappings: {document: {properties: {name: {type: 'string'}}}})
222
- end
223
- specify do
224
- expect(stub_index(:documents) do
225
- define_type :document do
226
- field :name, type: 'string'
227
- end
228
- define_type :document2 do
229
- field :name, type: 'string'
230
- end
231
- end.mappings_hash[:mappings].keys).to match_array(%i[document document2])
236
+ end.mappings_hash).to eq(mappings: {document: {properties: {date: {type: 'date'}}}})
232
237
  end
233
238
  end
234
239
 
@@ -240,7 +245,7 @@ describe Chewy::Index do
240
245
  specify do
241
246
  expect(stub_index(:documents) do
242
247
  define_type :document do
243
- field :name, type: 'string'
248
+ field :name
244
249
  end
245
250
  end.specification_hash.keys).to eq([:mappings])
246
251
  end
@@ -248,7 +253,7 @@ describe Chewy::Index do
248
253
  expect(stub_index(:documents) do
249
254
  settings number_of_shards: 1
250
255
  define_type :document do
251
- field :name, type: 'string'
256
+ field :name
252
257
  end
253
258
  end.specification_hash.keys).to match_array(%i[mappings settings])
254
259
  end
@@ -281,4 +286,29 @@ describe Chewy::Index do
281
286
  specify { expect(DummiesIndex.index_name).to eq('borogoves_dummies') }
282
287
  end
283
288
  end
289
+
290
+ context 'index call inside index', :orm do
291
+ before do
292
+ stub_index(:cities) do
293
+ define_type :city do
294
+ field :country_name, value: (lambda do |city|
295
+ CountriesIndex::Country.filter(term: {_id: city.country_id}).first.name
296
+ end)
297
+ end
298
+ end
299
+
300
+ stub_index(:countries) do
301
+ define_type :country do
302
+ field :name
303
+ end
304
+ end
305
+
306
+ CountriesIndex::Country.import!(double(id: 1, name: 'Country'))
307
+ end
308
+
309
+ specify do
310
+ expect { CitiesIndex::City.import!(double(country_id: 1)) }
311
+ .to update_index(CitiesIndex::City).and_reindex(country_name: 'Country')
312
+ end
313
+ end
284
314
  end
@@ -6,16 +6,18 @@ describe Chewy::Journal do
6
6
  context namespace.present? ? 'with namespace' : 'without namespace' do
7
7
  before do
8
8
  stub_model(:city) do
9
- update_index "#{namespace}places#city", :self
9
+ update_index "#{namespace}cities", :self
10
10
  end
11
11
  stub_model(:country) do
12
- update_index "#{namespace}places#country", :self
12
+ update_index "#{namespace}countries", :self
13
13
  end
14
14
 
15
- stub_index("#{namespace}places") do
15
+ stub_index("#{namespace}cities") do
16
16
  define_type City do
17
17
  default_import_options journal: true
18
18
  end
19
+ end
20
+ stub_index("#{namespace}countries") do
19
21
  define_type Country do
20
22
  default_import_options journal: true
21
23
  end
@@ -41,7 +43,8 @@ describe Chewy::Journal do
41
43
  end
42
44
 
43
45
  specify do
44
- places_index = namespace.present? ? Namespace::PlacesIndex : PlacesIndex
46
+ cities_index = namespace.present? ? Namespace::CitiesIndex : CitiesIndex
47
+ countries_index = namespace.present? ? Namespace::CountriesIndex : CountriesIndex
45
48
  Chewy.strategy(:urgent) do
46
49
  cities = Array.new(2) { |i| City.create!(id: i + 1) }
47
50
  countries = Array.new(2) { |i| Country.create!(id: i + 1) }
@@ -49,12 +52,13 @@ describe Chewy::Journal do
49
52
 
50
53
  Timecop.freeze(import_time)
51
54
 
52
- places_index.import
55
+ cities_index.import
56
+ countries_index.import
53
57
 
54
- expect(Chewy::Stash.exists?).to eq true
58
+ expect(Chewy::Stash::Journal.exists?).to eq true
55
59
 
56
60
  Timecop.freeze(update_time)
57
- cities.first.update_attributes!(name: 'Supername')
61
+ cities.first.update!(name: 'Supername')
58
62
 
59
63
  Timecop.freeze(destroy_time)
60
64
  countries.last.destroy
@@ -62,66 +66,66 @@ describe Chewy::Journal do
62
66
  journal_entries = Chewy::Stash::Journal.order(:created_at).hits.map { |r| r['_source'] }
63
67
  expected_journal = [
64
68
  {
65
- 'index_name' => "#{namespace}places",
69
+ 'index_name' => "#{namespace}cities",
66
70
  'type_name' => 'city',
67
71
  'action' => 'index',
68
- 'references' => ['1'],
72
+ 'references' => ['1'].map(&Base64.method(:encode64)),
69
73
  'created_at' => time.utc.as_json
70
74
  },
71
75
  {
72
- 'index_name' => "#{namespace}places",
76
+ 'index_name' => "#{namespace}cities",
73
77
  'type_name' => 'city',
74
78
  'action' => 'index',
75
- 'references' => ['2'],
79
+ 'references' => ['2'].map(&Base64.method(:encode64)),
76
80
  'created_at' => time.utc.as_json
77
81
  },
78
82
  {
79
- 'index_name' => "#{namespace}places",
83
+ 'index_name' => "#{namespace}countries",
80
84
  'type_name' => 'country',
81
85
  'action' => 'index',
82
- 'references' => ['1'],
86
+ 'references' => ['1'].map(&Base64.method(:encode64)),
83
87
  'created_at' => time.utc.as_json
84
88
  },
85
89
  {
86
- 'index_name' => "#{namespace}places",
90
+ 'index_name' => "#{namespace}countries",
87
91
  'type_name' => 'country',
88
92
  'action' => 'index',
89
- 'references' => ['2'],
93
+ 'references' => ['2'].map(&Base64.method(:encode64)),
90
94
  'created_at' => time.utc.as_json
91
95
  },
92
96
  {
93
- 'index_name' => "#{namespace}places",
97
+ 'index_name' => "#{namespace}countries",
94
98
  'type_name' => 'country',
95
99
  'action' => 'index',
96
- 'references' => ['3'],
100
+ 'references' => ['3'].map(&Base64.method(:encode64)),
97
101
  'created_at' => time.utc.as_json
98
102
  },
99
103
  {
100
- 'index_name' => "#{namespace}places",
104
+ 'index_name' => "#{namespace}cities",
101
105
  'type_name' => 'city',
102
106
  'action' => 'index',
103
- 'references' => %w[1 2],
107
+ 'references' => %w[1 2].map(&Base64.method(:encode64)),
104
108
  'created_at' => import_time.utc.as_json
105
109
  },
106
110
  {
107
- 'index_name' => "#{namespace}places",
111
+ 'index_name' => "#{namespace}countries",
108
112
  'type_name' => 'country',
109
113
  'action' => 'index',
110
- 'references' => %w[1 2 3],
114
+ 'references' => %w[1 2 3].map(&Base64.method(:encode64)),
111
115
  'created_at' => import_time.utc.as_json
112
116
  },
113
117
  {
114
- 'index_name' => "#{namespace}places",
118
+ 'index_name' => "#{namespace}cities",
115
119
  'type_name' => 'city',
116
120
  'action' => 'index',
117
- 'references' => ['1'],
121
+ 'references' => ['1'].map(&Base64.method(:encode64)),
118
122
  'created_at' => update_time.utc.as_json
119
123
  },
120
124
  {
121
- 'index_name' => "#{namespace}places",
125
+ 'index_name' => "#{namespace}countries",
122
126
  'type_name' => 'country',
123
127
  'action' => 'delete',
124
- 'references' => ['2'],
128
+ 'references' => ['2'].map(&Base64.method(:encode64)),
125
129
  'created_at' => destroy_time.utc.as_json
126
130
  }
127
131
  ]
@@ -133,11 +137,11 @@ describe Chewy::Journal do
133
137
  expect(journal_entries.size).to eq 4
134
138
 
135
139
  # simulate lost data
136
- Chewy.client.delete(index: "#{Chewy.settings[:prefix]}_places", type: 'city', id: 1, refresh: true)
137
- expect(places_index::City.count).to eq 1
140
+ Chewy.client.delete(index: "#{Chewy.settings[:prefix]}_cities", type: 'city', id: 1, refresh: true)
141
+ expect(cities_index.count).to eq 1
138
142
 
139
143
  described_class.new.apply(time)
140
- expect(places_index::City.count).to eq 2
144
+ expect(cities_index.count).to eq 2
141
145
 
142
146
  clean_response = described_class.new.clean(import_time)
143
147
  expect(clean_response['deleted'] || clean_response['_indices']['_all']['deleted']).to eq 7
@@ -231,7 +235,7 @@ describe Chewy::Journal do
231
235
  let!(:journal_entries) do
232
236
  record = Chewy::Stash::Journal.entries(time).first
233
237
  Array.new(count_of_checks) do |i|
234
- Chewy::Stash::Journal.new(
238
+ Chewy::Stash::Journal::Journal.new(
235
239
  record.attributes.merge(
236
240
  'created_at' => time.to_i + i,
237
241
  'references' => [i.to_s]