chewy 5.2.0 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (103) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +36 -10
  3. data/CHANGELOG.md +219 -326
  4. data/Gemfile +1 -1
  5. data/README.md +9 -21
  6. data/chewy.gemspec +1 -1
  7. data/gemfiles/rails.6.1.activerecord.gemfile +1 -1
  8. data/lib/chewy/config.rb +2 -20
  9. data/lib/chewy/fields/base.rb +0 -6
  10. data/lib/chewy/index.rb +2 -0
  11. data/lib/chewy/index/actions.rb +5 -1
  12. data/lib/chewy/multi_search.rb +62 -0
  13. data/lib/chewy/search.rb +1 -4
  14. data/lib/chewy/search/pagination/will_paginate.rb +1 -1
  15. data/lib/chewy/search/parameters/none.rb +1 -3
  16. data/lib/chewy/search/request.rb +25 -24
  17. data/lib/chewy/search/scrolling.rb +4 -1
  18. data/lib/chewy/type.rb +4 -1
  19. data/lib/chewy/type/adapter/orm.rb +6 -3
  20. data/lib/chewy/type/import.rb +1 -0
  21. data/lib/chewy/type/syncer.rb +4 -5
  22. data/lib/chewy/type/witchcraft.rb +3 -1
  23. data/lib/chewy/type/wrapper.rb +11 -1
  24. data/lib/chewy/version.rb +1 -1
  25. data/migration_guide.md +18 -0
  26. data/spec/chewy/config_spec.rb +0 -21
  27. data/spec/chewy/index/actions_spec.rb +24 -0
  28. data/spec/chewy/index_spec.rb +16 -39
  29. data/spec/chewy/journal_spec.rb +21 -17
  30. data/spec/chewy/minitest/search_index_receiver_spec.rb +11 -9
  31. data/spec/chewy/multi_search_spec.rb +85 -0
  32. data/spec/chewy/rake_helper_spec.rb +102 -87
  33. data/spec/chewy/rspec/update_index_spec.rb +47 -46
  34. data/spec/chewy/runtime_spec.rb +2 -2
  35. data/spec/chewy/search/parameters/indices_spec.rb +6 -7
  36. data/spec/chewy/search/parameters/none_spec.rb +1 -1
  37. data/spec/chewy/search/parameters_spec.rb +1 -1
  38. data/spec/chewy/search/request_spec.rb +82 -67
  39. data/spec/chewy/search/response_spec.rb +19 -15
  40. data/spec/chewy/search/scrolling_spec.rb +25 -16
  41. data/spec/chewy/search_spec.rb +45 -41
  42. data/spec/chewy/stash_spec.rb +14 -12
  43. data/spec/chewy/type/adapter/active_record_spec.rb +13 -1
  44. data/spec/chewy/type/import/bulk_builder_spec.rb +9 -94
  45. data/spec/chewy/type/import/journal_builder_spec.rb +9 -7
  46. data/spec/chewy/type/mapping_spec.rb +3 -1
  47. data/spec/chewy/type/witchcraft_spec.rb +15 -0
  48. data/spec/chewy/type/wrapper_spec.rb +3 -1
  49. data/spec/chewy_spec.rb +0 -7
  50. data/spec/spec_helper.rb +0 -7
  51. data/spec/support/active_record.rb +20 -0
  52. metadata +8 -78
  53. data/LEGACY_DSL.md +0 -497
  54. data/gemfiles/ruby3.gemfile +0 -10
  55. data/lib/chewy/query.rb +0 -1142
  56. data/lib/chewy/query/compose.rb +0 -68
  57. data/lib/chewy/query/criteria.rb +0 -191
  58. data/lib/chewy/query/filters.rb +0 -244
  59. data/lib/chewy/query/loading.rb +0 -110
  60. data/lib/chewy/query/nodes/and.rb +0 -25
  61. data/lib/chewy/query/nodes/base.rb +0 -17
  62. data/lib/chewy/query/nodes/bool.rb +0 -34
  63. data/lib/chewy/query/nodes/equal.rb +0 -34
  64. data/lib/chewy/query/nodes/exists.rb +0 -20
  65. data/lib/chewy/query/nodes/expr.rb +0 -28
  66. data/lib/chewy/query/nodes/field.rb +0 -110
  67. data/lib/chewy/query/nodes/has_child.rb +0 -15
  68. data/lib/chewy/query/nodes/has_parent.rb +0 -15
  69. data/lib/chewy/query/nodes/has_relation.rb +0 -59
  70. data/lib/chewy/query/nodes/match_all.rb +0 -11
  71. data/lib/chewy/query/nodes/missing.rb +0 -20
  72. data/lib/chewy/query/nodes/not.rb +0 -25
  73. data/lib/chewy/query/nodes/or.rb +0 -25
  74. data/lib/chewy/query/nodes/prefix.rb +0 -19
  75. data/lib/chewy/query/nodes/query.rb +0 -20
  76. data/lib/chewy/query/nodes/range.rb +0 -63
  77. data/lib/chewy/query/nodes/raw.rb +0 -15
  78. data/lib/chewy/query/nodes/regexp.rb +0 -35
  79. data/lib/chewy/query/nodes/script.rb +0 -20
  80. data/lib/chewy/query/pagination.rb +0 -25
  81. data/spec/chewy/query/criteria_spec.rb +0 -700
  82. data/spec/chewy/query/filters_spec.rb +0 -201
  83. data/spec/chewy/query/loading_spec.rb +0 -124
  84. data/spec/chewy/query/nodes/and_spec.rb +0 -12
  85. data/spec/chewy/query/nodes/bool_spec.rb +0 -14
  86. data/spec/chewy/query/nodes/equal_spec.rb +0 -32
  87. data/spec/chewy/query/nodes/exists_spec.rb +0 -18
  88. data/spec/chewy/query/nodes/has_child_spec.rb +0 -59
  89. data/spec/chewy/query/nodes/has_parent_spec.rb +0 -59
  90. data/spec/chewy/query/nodes/match_all_spec.rb +0 -11
  91. data/spec/chewy/query/nodes/missing_spec.rb +0 -16
  92. data/spec/chewy/query/nodes/not_spec.rb +0 -14
  93. data/spec/chewy/query/nodes/or_spec.rb +0 -12
  94. data/spec/chewy/query/nodes/prefix_spec.rb +0 -16
  95. data/spec/chewy/query/nodes/query_spec.rb +0 -12
  96. data/spec/chewy/query/nodes/range_spec.rb +0 -32
  97. data/spec/chewy/query/nodes/raw_spec.rb +0 -11
  98. data/spec/chewy/query/nodes/regexp_spec.rb +0 -43
  99. data/spec/chewy/query/nodes/script_spec.rb +0 -15
  100. data/spec/chewy/query/pagination/kaminari_spec.rb +0 -5
  101. data/spec/chewy/query/pagination/will_paginate_spec.rb +0 -5
  102. data/spec/chewy/query/pagination_spec.rb +0 -39
  103. data/spec/chewy/query_spec.rb +0 -637
@@ -3,8 +3,10 @@ require 'spec_helper'
3
3
  describe Chewy::Type::Import::JournalBuilder, :orm do
4
4
  before do
5
5
  stub_model(:country)
6
- stub_index 'namespace/places' do
6
+ stub_index 'namespace/cities' do
7
7
  define_type :city
8
+ end
9
+ stub_index 'namespace/countries' do
8
10
  define_type Country
9
11
  end
10
12
  Timecop.freeze(time)
@@ -13,7 +15,7 @@ describe Chewy::Type::Import::JournalBuilder, :orm do
13
15
 
14
16
  let(:time) { Time.parse('2017-07-14 12:00Z') }
15
17
 
16
- let(:type) { Namespace::PlacesIndex::City }
18
+ let(:type) { Namespace::CitiesIndex::City }
17
19
  let(:index) { [] }
18
20
  let(:delete) { [] }
19
21
  subject { described_class.new(type, index: index, delete: delete) }
@@ -29,7 +31,7 @@ describe Chewy::Type::Import::JournalBuilder, :orm do
29
31
  _index: 'chewy_journal',
30
32
  _type: 'journal',
31
33
  data: {
32
- 'index_name' => 'namespace/places',
34
+ 'index_name' => 'namespace/cities',
33
35
  'type_name' => 'city',
34
36
  'action' => 'index',
35
37
  'references' => [Base64.encode64('{"id":1,"name":"City"}')],
@@ -48,7 +50,7 @@ describe Chewy::Type::Import::JournalBuilder, :orm do
48
50
  _index: 'chewy_journal',
49
51
  _type: 'journal',
50
52
  data: {
51
- 'index_name' => 'namespace/places',
53
+ 'index_name' => 'namespace/cities',
52
54
  'type_name' => 'city',
53
55
  'action' => 'delete',
54
56
  'references' => [Base64.encode64('{"id":1,"name":"City"}')],
@@ -60,7 +62,7 @@ describe Chewy::Type::Import::JournalBuilder, :orm do
60
62
  end
61
63
 
62
64
  context do
63
- let(:type) { Namespace::PlacesIndex::Country }
65
+ let(:type) { Namespace::CountriesIndex::Country }
64
66
  let(:index) { [Country.new(id: 1, name: 'City')] }
65
67
  let(:delete) { [Country.new(id: 2, name: 'City')] }
66
68
  specify do
@@ -69,7 +71,7 @@ describe Chewy::Type::Import::JournalBuilder, :orm do
69
71
  _index: 'chewy_journal',
70
72
  _type: 'journal',
71
73
  data: {
72
- 'index_name' => 'namespace/places',
74
+ 'index_name' => 'namespace/countries',
73
75
  'type_name' => 'country',
74
76
  'action' => 'index',
75
77
  'references' => [Base64.encode64('1')],
@@ -81,7 +83,7 @@ describe Chewy::Type::Import::JournalBuilder, :orm do
81
83
  _index: 'chewy_journal',
82
84
  _type: 'journal',
83
85
  data: {
84
- 'index_name' => 'namespace/places',
86
+ 'index_name' => 'namespace/countries',
85
87
  'type_name' => 'country',
86
88
  'action' => 'delete',
87
89
  'references' => [Base64.encode64('2')],
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe Chewy::Type::Mapping do
4
4
  let(:product) { ProductsIndex::Product }
5
- let(:review) { ProductsIndex::Review }
5
+ let(:review) { ReviewsIndex::Review }
6
6
 
7
7
  before do
8
8
  stub_index(:products) do
@@ -20,6 +20,8 @@ describe Chewy::Type::Mapping do
20
20
  end
21
21
  end
22
22
  end
23
+ end
24
+ stub_index(:reviews) do
23
25
  define_type :review do
24
26
  field :title, :body
25
27
  field :comments do
@@ -181,6 +181,21 @@ describe Chewy::Type::Witchcraft do
181
181
  ]}.as_json)
182
182
  end
183
183
  end
184
+
185
+ context do
186
+ mapping do
187
+ field :not_present do
188
+ field :nothing
189
+ end
190
+ end
191
+
192
+ let(:object) do
193
+ double(not_present: nil)
194
+ end
195
+ specify do
196
+ expect(type.cauldron.brew(object)).to eq({not_present: nil}.as_json)
197
+ end
198
+ end
184
199
  end
185
200
 
186
201
  context 'dynamic fields' do
@@ -63,9 +63,11 @@ describe Chewy::Type::Wrapper do
63
63
 
64
64
  it do
65
65
  is_expected.to respond_to(:name_highlight)
66
+ .and respond_to(:name_highlights)
66
67
  .and have_attributes(
67
68
  name: 'Martin',
68
- name_highlight: '<b>Mar</b>tin'
69
+ name_highlight: '<b>Mar</b>tin',
70
+ name_highlights: ['<b>Mar</b>tin']
69
71
  )
70
72
  end
71
73
  end
data/spec/chewy_spec.rb CHANGED
@@ -15,21 +15,18 @@ describe Chewy do
15
15
 
16
16
  stub_index('namespace/autocomplete') do
17
17
  define_type :developer
18
- define_type :company
19
18
  end
20
19
  end
21
20
 
22
21
  specify { expect { described_class.derive_type('some#developers') }.to raise_error(Chewy::UnderivableType, /SomeIndex/) }
23
22
  specify { expect { described_class.derive_type('borogoves#developers') }.to raise_error(Chewy::UnderivableType, /Borogoves/) }
24
23
  specify { expect { described_class.derive_type('developers#borogoves') }.to raise_error(Chewy::UnderivableType, /DevelopersIndex.*borogoves/) }
25
- specify { expect { described_class.derive_type('namespace/autocomplete') }.to raise_error(Chewy::UnderivableType, %r{AutocompleteIndex.*namespace/autocomplete#type_name}) }
26
24
 
27
25
  specify { expect(described_class.derive_type(DevelopersIndex::Developer)).to eq(DevelopersIndex::Developer) }
28
26
  specify { expect(described_class.derive_type('developers_index')).to eq(DevelopersIndex::Developer) }
29
27
  specify { expect(described_class.derive_type('developers')).to eq(DevelopersIndex::Developer) }
30
28
  specify { expect(described_class.derive_type('developers#developer')).to eq(DevelopersIndex::Developer) }
31
29
  specify { expect(described_class.derive_type('namespace/autocomplete#developer')).to eq(Namespace::AutocompleteIndex::Developer) }
32
- specify { expect(described_class.derive_type('namespace/autocomplete#company')).to eq(Namespace::AutocompleteIndex::Company) }
33
30
  end
34
31
 
35
32
  describe '.derive_types' do
@@ -42,7 +39,6 @@ describe Chewy do
42
39
 
43
40
  stub_index('namespace/autocomplete') do
44
41
  define_type :developer
45
- define_type :company
46
42
  end
47
43
  end
48
44
 
@@ -58,7 +54,6 @@ describe Chewy do
58
54
  specify { expect(described_class.derive_types('developers#developer')).to eq([DevelopersIndex::Developer]) }
59
55
  specify { expect(described_class.derive_types('namespace/autocomplete')).to match_array(Namespace::AutocompleteIndex.types) }
60
56
  specify { expect(described_class.derive_types('namespace/autocomplete#developer')).to eq([Namespace::AutocompleteIndex::Developer]) }
61
- specify { expect(described_class.derive_types('namespace/autocomplete#company')).to eq([Namespace::AutocompleteIndex::Company]) }
62
57
  end
63
58
 
64
59
  describe '.create_type' do
@@ -130,7 +125,6 @@ describe Chewy do
130
125
  stub_index(:admins).create!
131
126
  allow(Chewy).to receive_messages(configuration: Chewy.configuration.merge(prefix: 'prefix2'))
132
127
  stub_index(:developers).create!
133
- stub_index(:companies).create!
134
128
 
135
129
  Chewy.massacre
136
130
 
@@ -139,7 +133,6 @@ describe Chewy do
139
133
 
140
134
  specify { expect(AdminsIndex.exists?).to eq(true) }
141
135
  specify { expect(DevelopersIndex.exists?).to eq(false) }
142
- specify { expect(CompaniesIndex.exists?).to eq(false) }
143
136
  end
144
137
 
145
138
  describe '.client' do
data/spec/spec_helper.rb CHANGED
@@ -33,15 +33,8 @@ Chewy.settings = {
33
33
  }
34
34
  }
35
35
 
36
- Chewy.default_field_type = 'string' if Chewy::Runtime.version < '5.0'
37
36
  # Chewy.transport_logger = Logger.new(STDERR)
38
37
 
39
- KEYWORD_FIELD = if Chewy::Runtime.version < '5.0'
40
- {type: 'string', index: 'not_analyzed'}
41
- else
42
- {type: 'keyword'}
43
- end
44
-
45
38
  RSpec.configure do |config|
46
39
  config.mock_with :rspec
47
40
  config.order = :random
@@ -29,6 +29,26 @@ module ActiveRecordClassHelpers
29
29
  :active_record
30
30
  end
31
31
 
32
+ def expects_db_queries(&block)
33
+ have_queries = false
34
+ ActiveSupport::Notifications.subscribed(
35
+ ->(*_) { have_queries = true },
36
+ 'sql.active_record',
37
+ &block
38
+ )
39
+ raise 'Expected some db queries, but none were made' unless have_queries
40
+ end
41
+
42
+ def expects_no_query(&block)
43
+ queries = []
44
+ ActiveSupport::Notifications.subscribed(
45
+ ->(*args) { queries << args[4][:sql] },
46
+ 'sql.active_record',
47
+ &block
48
+ )
49
+ raise "Expected no DB queries, but the following ones were made: #{queries.join(', ')}" if queries.present?
50
+ end
51
+
32
52
  def stub_model(name, superclass = nil, &block)
33
53
  stub_class(name, superclass || ActiveRecord::Base, &block)
34
54
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chewy
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.2.0
4
+ version: 6.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Toptal, LLC
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2021-01-28 00:00:00.000000000 Z
12
+ date: 2021-02-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: appraisal
@@ -213,14 +213,14 @@ dependencies:
213
213
  requirements:
214
214
  - - ">="
215
215
  - !ruby/object:Gem::Version
216
- version: 2.0.0
216
+ version: 6.3.0
217
217
  type: :runtime
218
218
  prerelease: false
219
219
  version_requirements: !ruby/object:Gem::Requirement
220
220
  requirements:
221
221
  - - ">="
222
222
  - !ruby/object:Gem::Version
223
- version: 2.0.0
223
+ version: 6.3.0
224
224
  - !ruby/object:Gem::Dependency
225
225
  name: elasticsearch-dsl
226
226
  requirement: !ruby/object:Gem::Requirement
@@ -254,7 +254,6 @@ files:
254
254
  - CHANGELOG.md
255
255
  - Gemfile
256
256
  - Guardfile
257
- - LEGACY_DSL.md
258
257
  - LICENSE.txt
259
258
  - README.md
260
259
  - Rakefile
@@ -264,7 +263,6 @@ files:
264
263
  - gemfiles/rails.5.2.mongoid.6.4.gemfile
265
264
  - gemfiles/rails.6.0.activerecord.gemfile
266
265
  - gemfiles/rails.6.1.activerecord.gemfile
267
- - gemfiles/ruby3.gemfile
268
266
  - gemfiles/sequel.4.45.gemfile
269
267
  - lib/chewy.rb
270
268
  - lib/chewy/backports/deep_dup.rb
@@ -283,32 +281,7 @@ files:
283
281
  - lib/chewy/minitest.rb
284
282
  - lib/chewy/minitest/helpers.rb
285
283
  - lib/chewy/minitest/search_index_receiver.rb
286
- - lib/chewy/query.rb
287
- - lib/chewy/query/compose.rb
288
- - lib/chewy/query/criteria.rb
289
- - lib/chewy/query/filters.rb
290
- - lib/chewy/query/loading.rb
291
- - lib/chewy/query/nodes/and.rb
292
- - lib/chewy/query/nodes/base.rb
293
- - lib/chewy/query/nodes/bool.rb
294
- - lib/chewy/query/nodes/equal.rb
295
- - lib/chewy/query/nodes/exists.rb
296
- - lib/chewy/query/nodes/expr.rb
297
- - lib/chewy/query/nodes/field.rb
298
- - lib/chewy/query/nodes/has_child.rb
299
- - lib/chewy/query/nodes/has_parent.rb
300
- - lib/chewy/query/nodes/has_relation.rb
301
- - lib/chewy/query/nodes/match_all.rb
302
- - lib/chewy/query/nodes/missing.rb
303
- - lib/chewy/query/nodes/not.rb
304
- - lib/chewy/query/nodes/or.rb
305
- - lib/chewy/query/nodes/prefix.rb
306
- - lib/chewy/query/nodes/query.rb
307
- - lib/chewy/query/nodes/range.rb
308
- - lib/chewy/query/nodes/raw.rb
309
- - lib/chewy/query/nodes/regexp.rb
310
- - lib/chewy/query/nodes/script.rb
311
- - lib/chewy/query/pagination.rb
284
+ - lib/chewy/multi_search.rb
312
285
  - lib/chewy/railtie.rb
313
286
  - lib/chewy/rake_helper.rb
314
287
  - lib/chewy/repository.rb
@@ -398,6 +371,7 @@ files:
398
371
  - lib/generators/templates/chewy.yml
399
372
  - lib/sequel/plugins/chewy_observe.rb
400
373
  - lib/tasks/chewy.rake
374
+ - migration_guide.md
401
375
  - spec/chewy/config_spec.rb
402
376
  - spec/chewy/fields/base_spec.rb
403
377
  - spec/chewy/fields/root_spec.rb
@@ -410,29 +384,7 @@ files:
410
384
  - spec/chewy/journal_spec.rb
411
385
  - spec/chewy/minitest/helpers_spec.rb
412
386
  - spec/chewy/minitest/search_index_receiver_spec.rb
413
- - spec/chewy/query/criteria_spec.rb
414
- - spec/chewy/query/filters_spec.rb
415
- - spec/chewy/query/loading_spec.rb
416
- - spec/chewy/query/nodes/and_spec.rb
417
- - spec/chewy/query/nodes/bool_spec.rb
418
- - spec/chewy/query/nodes/equal_spec.rb
419
- - spec/chewy/query/nodes/exists_spec.rb
420
- - spec/chewy/query/nodes/has_child_spec.rb
421
- - spec/chewy/query/nodes/has_parent_spec.rb
422
- - spec/chewy/query/nodes/match_all_spec.rb
423
- - spec/chewy/query/nodes/missing_spec.rb
424
- - spec/chewy/query/nodes/not_spec.rb
425
- - spec/chewy/query/nodes/or_spec.rb
426
- - spec/chewy/query/nodes/prefix_spec.rb
427
- - spec/chewy/query/nodes/query_spec.rb
428
- - spec/chewy/query/nodes/range_spec.rb
429
- - spec/chewy/query/nodes/raw_spec.rb
430
- - spec/chewy/query/nodes/regexp_spec.rb
431
- - spec/chewy/query/nodes/script_spec.rb
432
- - spec/chewy/query/pagination/kaminari_spec.rb
433
- - spec/chewy/query/pagination/will_paginate_spec.rb
434
- - spec/chewy/query/pagination_spec.rb
435
- - spec/chewy/query_spec.rb
387
+ - spec/chewy/multi_search_spec.rb
436
388
  - spec/chewy/rake_helper_spec.rb
437
389
  - spec/chewy/repository_spec.rb
438
390
  - spec/chewy/rspec/update_index_spec.rb
@@ -551,29 +503,7 @@ test_files:
551
503
  - spec/chewy/journal_spec.rb
552
504
  - spec/chewy/minitest/helpers_spec.rb
553
505
  - spec/chewy/minitest/search_index_receiver_spec.rb
554
- - spec/chewy/query/criteria_spec.rb
555
- - spec/chewy/query/filters_spec.rb
556
- - spec/chewy/query/loading_spec.rb
557
- - spec/chewy/query/nodes/and_spec.rb
558
- - spec/chewy/query/nodes/bool_spec.rb
559
- - spec/chewy/query/nodes/equal_spec.rb
560
- - spec/chewy/query/nodes/exists_spec.rb
561
- - spec/chewy/query/nodes/has_child_spec.rb
562
- - spec/chewy/query/nodes/has_parent_spec.rb
563
- - spec/chewy/query/nodes/match_all_spec.rb
564
- - spec/chewy/query/nodes/missing_spec.rb
565
- - spec/chewy/query/nodes/not_spec.rb
566
- - spec/chewy/query/nodes/or_spec.rb
567
- - spec/chewy/query/nodes/prefix_spec.rb
568
- - spec/chewy/query/nodes/query_spec.rb
569
- - spec/chewy/query/nodes/range_spec.rb
570
- - spec/chewy/query/nodes/raw_spec.rb
571
- - spec/chewy/query/nodes/regexp_spec.rb
572
- - spec/chewy/query/nodes/script_spec.rb
573
- - spec/chewy/query/pagination/kaminari_spec.rb
574
- - spec/chewy/query/pagination/will_paginate_spec.rb
575
- - spec/chewy/query/pagination_spec.rb
576
- - spec/chewy/query_spec.rb
506
+ - spec/chewy/multi_search_spec.rb
577
507
  - spec/chewy/rake_helper_spec.rb
578
508
  - spec/chewy/repository_spec.rb
579
509
  - spec/chewy/rspec/update_index_spec.rb
data/LEGACY_DSL.md DELETED
@@ -1,497 +0,0 @@
1
- # Previous DSL readme.
2
-
3
- See `Chewy::Query` for details.
4
-
5
- * [Index querying](#index-querying)
6
- * [Additional query action.](#additional-query-action)
7
- * [Filters query DSL](#filters-query-dsl)
8
- * [Faceting](#faceting)
9
- * [Aggregations](#aggregations)
10
- * [Script fields](#script-fields)
11
- * [Script scoring](#script-scoring)
12
- * [Boost Factor](#boost-factor)
13
- * [Objects loading](#objects-loading)
14
-
15
- ## Index querying
16
-
17
- ```ruby
18
- scope = UsersIndex.query(term: {name: 'foo'})
19
- .filter(range: {rating: {gte: 100}})
20
- .order(created: :desc)
21
- .limit(20).offset(100)
22
-
23
- scope.to_a # => will produce array of UserIndex::User or other types instances
24
- scope.map { |user| user.email }
25
- scope.total_count # => will return total objects count
26
-
27
- scope.per(10).page(3) # supports kaminari pagination
28
- scope.explain.map { |user| user._explanation }
29
- scope.only(:id, :email) # returns ids and emails only
30
-
31
- scope.merge(other_scope) # queries could be merged
32
- ```
33
-
34
- Also, queries can be performed on a type individually:
35
-
36
- ```ruby
37
- UsersIndex::User.filter(term: {name: 'foo'}) # will return UserIndex::User collection only
38
- ```
39
-
40
- If you are performing more than one `filter` or `query` in the chain, all the filters and queries will be concatenated in the way specified by
41
- `filter_mode` and `query_mode` respectively.
42
-
43
- The default `filter_mode` is `:and` and the default `query_mode` is `bool`.
44
-
45
- Available filter modes are: `:and`, `:or`, `:must`, `:should` and any minimum_should_match-acceptable value
46
-
47
- Available query modes are: `:must`, `:should`, `:dis_max`, any minimum_should_match-acceptable value or float value for dis_max query with tie_breaker specified.
48
-
49
- ```ruby
50
- UsersIndex::User.filter{ name == 'Fred' }.filter{ age < 42 } # will be wrapped with `and` filter
51
- UsersIndex::User.filter{ name == 'Fred' }.filter{ age < 42 }.filter_mode(:should) # will be wrapped with bool `should` filter
52
- UsersIndex::User.filter{ name == 'Fred' }.filter{ age < 42 }.filter_mode('75%') # will be wrapped with bool `should` filter with `minimum_should_match: '75%'`
53
- ```
54
-
55
- See [query.rb](lib/chewy/query.rb) for more details.
56
-
57
- ## Additional query action.
58
-
59
- You may also perform additional actions on the query scope, such as deleting of all the scope documents:
60
-
61
- ```ruby
62
- UsersIndex.delete_all
63
- UsersIndex::User.delete_all
64
- UsersIndex.filter{ age < 42 }.delete_all
65
- UsersIndex::User.filter{ age < 42 }.delete_all
66
- ```
67
-
68
- ## Filters query DSL
69
-
70
- There is a test version of the filter-creating DSL:
71
-
72
- ```ruby
73
- UsersIndex.filter{ name == 'Fred' } # will produce `term` filter.
74
- UsersIndex.filter{ age <= 42 } # will produce `range` filter.
75
- ```
76
-
77
- The basis of the DSL is the expression. There are 2 types of expressions:
78
-
79
- * Simple function
80
-
81
- ```ruby
82
- UsersIndex.filter{ s('doc["num"] > 1') } # script expression
83
- UsersIndex.filter{ q(query_string: {query: 'lazy fox'}) } # query expression
84
- ```
85
-
86
- * Field-dependent composite expression
87
- Consists of the field name (with or without dot notation), a value, and an action operator between them. The field name might take additional options for passing to the resulting expression.
88
-
89
- ```ruby
90
- UsersIndex.filter{ name == 'Name' } # simple field term filter
91
- UsersIndex.filter{ name(:bool) == ['Name1', 'Name2'] } # terms query with `execution: :bool` option passed
92
- UsersIndex.filter{ answers.title =~ /regexp/ } # regexp filter for `answers.title` field
93
- ```
94
-
95
- You can combine expressions as you wish with the help of combination operators.
96
-
97
- ```ruby
98
- UsersIndex.filter{ (name == 'Name') & (email == 'Email') } # combination produces `and` filter
99
- UsersIndex.filter{
100
- must(
101
- should(name =~ 'Fr').should_not(name == 'Fred') & (age == 42), email =~ /gmail\.com/
102
- ) | ((roles.admin == true) & name?)
103
- } # many of the combination possibilities
104
- ```
105
-
106
- There is also a special syntax for cache enabling:
107
-
108
- ```ruby
109
- UsersIndex.filter{ ~name == 'Name' } # you can apply tilde to the field name
110
- UsersIndex.filter{ ~(name == 'Name') } # or to the whole expression
111
-
112
- # if you are applying cache to the one part of range filter
113
- # the whole filter will be cached:
114
- UsersIndex.filter{ ~(age > 42) & (age <= 50) }
115
-
116
- # You can pass cache options as a field option also.
117
- UsersIndex.filter{ name(cache: true) == 'Name' }
118
- UsersIndex.filter{ name(cache: false) == 'Name' }
119
-
120
- # With regexp filter you can pass _cache_key
121
- UsersIndex.filter{ name(cache: 'name_regexp') =~ /Name/ }
122
- # Or not
123
- UsersIndex.filter{ name(cache: true) =~ /Name/ }
124
- ```
125
-
126
- Compliance cheatsheet for filters and DSL expressions:
127
-
128
- * Term filter
129
-
130
- ```json
131
- {"term": {"name": "Fred"}}
132
- {"not": {"term": {"name": "Johny"}}}
133
- ```
134
-
135
- ```ruby
136
- UsersIndex.filter{ name == 'Fred' }
137
- UsersIndex.filter{ name != 'Johny' }
138
- ```
139
-
140
- * Terms filter
141
-
142
- ```json
143
- {"terms": {"name": ["Fred", "Johny"]}}
144
- {"not": {"terms": {"name": ["Fred", "Johny"]}}}
145
-
146
- {"terms": {"name": ["Fred", "Johny"], "execution": "or"}}
147
-
148
- {"terms": {"name": ["Fred", "Johny"], "execution": "and"}}
149
-
150
- {"terms": {"name": ["Fred", "Johny"], "execution": "bool"}}
151
-
152
- {"terms": {"name": ["Fred", "Johny"], "execution": "fielddata"}}
153
- ```
154
-
155
- ```ruby
156
- UsersIndex.filter{ name == ['Fred', 'Johny'] }
157
- UsersIndex.filter{ name != ['Fred', 'Johny'] }
158
-
159
- UsersIndex.filter{ name(:|) == ['Fred', 'Johny'] }
160
- UsersIndex.filter{ name(:or) == ['Fred', 'Johny'] }
161
- UsersIndex.filter{ name(execution: :or) == ['Fred', 'Johny'] }
162
-
163
- UsersIndex.filter{ name(:&) == ['Fred', 'Johny'] }
164
- UsersIndex.filter{ name(:and) == ['Fred', 'Johny'] }
165
- UsersIndex.filter{ name(execution: :and) == ['Fred', 'Johny'] }
166
-
167
- UsersIndex.filter{ name(:b) == ['Fred', 'Johny'] }
168
- UsersIndex.filter{ name(:bool) == ['Fred', 'Johny'] }
169
- UsersIndex.filter{ name(execution: :bool) == ['Fred', 'Johny'] }
170
-
171
- UsersIndex.filter{ name(:f) == ['Fred', 'Johny'] }
172
- UsersIndex.filter{ name(:fielddata) == ['Fred', 'Johny'] }
173
- UsersIndex.filter{ name(execution: :fielddata) == ['Fred', 'Johny'] }
174
- ```
175
-
176
- * Regexp filter (== and =~ are equivalent)
177
-
178
- ```json
179
- {"regexp": {"name.first": "s.*y"}}
180
-
181
- {"not": {"regexp": {"name.first": "s.*y"}}}
182
-
183
- {"regexp": {"name.first": {"value": "s.*y", "flags": "ANYSTRING|INTERSECTION"}}}
184
- ```
185
-
186
- ```ruby
187
- UsersIndex.filter{ name.first == /s.*y/ }
188
- UsersIndex.filter{ name.first =~ /s.*y/ }
189
-
190
- UsersIndex.filter{ name.first != /s.*y/ }
191
- UsersIndex.filter{ name.first !~ /s.*y/ }
192
-
193
- UsersIndex.filter{ name.first(:anystring, :intersection) == /s.*y/ }
194
- UsersIndex.filter{ name.first(flags: [:anystring, :intersection]) == /s.*y/ }
195
- ```
196
-
197
- * Prefix filter
198
-
199
- ```json
200
- {"prefix": {"name": "Fre"}}
201
- {"not": {"prefix": {"name": "Joh"}}}
202
- ```
203
-
204
- ```ruby
205
- UsersIndex.filter{ name =~ re' }
206
- UsersIndex.filter{ name !~ 'Joh' }
207
- ```
208
-
209
- * Exists filter
210
-
211
- ```json
212
- {"exists": {"field": "name"}}
213
- ```
214
-
215
- ```ruby
216
- UsersIndex.filter{ name? }
217
- UsersIndex.filter{ !!name }
218
- UsersIndex.filter{ !!name? }
219
- UsersIndex.filter{ name != nil }
220
- UsersIndex.filter{ !(name == nil) }
221
- ```
222
-
223
- * Missing filter
224
-
225
- ```json
226
- {"missing": {"field": "name", "existence": true, "null_value": false}}
227
- {"missing": {"field": "name", "existence": true, "null_value": true}}
228
- {"missing": {"field": "name", "existence": false, "null_value": true}}
229
- ```
230
-
231
- ```ruby
232
- UsersIndex.filter{ !name }
233
- UsersIndex.filter{ !name? }
234
- UsersIndex.filter{ name == nil }
235
- ```
236
-
237
- * Range
238
-
239
- ```json
240
- {"range": {"age": {"gt": 42}}}
241
- {"range": {"age": {"gte": 42}}}
242
- {"range": {"age": {"lt": 42}}}
243
- {"range": {"age": {"lte": 42}}}
244
-
245
- {"range": {"age": {"gt": 40, "lt": 50}}}
246
- {"range": {"age": {"gte": 40, "lte": 50}}}
247
-
248
- {"range": {"age": {"gt": 40, "lte": 50}}}
249
- {"range": {"age": {"gte": 40, "lt": 50}}}
250
- ```
251
-
252
- ```ruby
253
- UsersIndex.filter{ age > 42 }
254
- UsersIndex.filter{ age >= 42 }
255
- UsersIndex.filter{ age < 42 }
256
- UsersIndex.filter{ age <= 42 }
257
-
258
- UsersIndex.filter{ age == (40..50) }
259
- UsersIndex.filter{ (age > 40) & (age < 50) }
260
- UsersIndex.filter{ age == [40..50] }
261
- UsersIndex.filter{ (age >= 40) & (age <= 50) }
262
-
263
- UsersIndex.filter{ (age > 40) & (age <= 50) }
264
- UsersIndex.filter{ (age >= 40) & (age < 50) }
265
- ```
266
-
267
- * Bool filter
268
-
269
- ```json
270
- {"bool": {
271
- "must": [{"term": {"name": "Name"}}],
272
- "should": [{"term": {"age": 42}}, {"term": {"age": 45}}]
273
- }}
274
- ```
275
-
276
- ```ruby
277
- UsersIndex.filter{ must(name == 'Name').should(age == 42, age == 45) }
278
- ```
279
-
280
- * And filter
281
-
282
- ```json
283
- {"and": [{"term": {"name": "Name"}}, {"range": {"age": {"lt": 42}}}]}
284
- ```
285
-
286
- ```ruby
287
- UsersIndex.filter{ (name == 'Name') & (age < 42) }
288
- ```
289
-
290
- * Or filter
291
-
292
- ```json
293
- {"or": [{"term": {"name": "Name"}}, {"range": {"age": {"lt": 42}}}]}
294
- ```
295
-
296
- ```ruby
297
- UsersIndex.filter{ (name == 'Name') | (age < 42) }
298
- ```
299
-
300
- ```json
301
- {"not": {"term": {"name": "Name"}}}
302
- {"not": {"range": {"age": {"lt": 42}}}}
303
- ```
304
-
305
- ```ruby
306
- UsersIndex.filter{ !(name == 'Name') } # or UsersIndex.filter{ name != 'Name' }
307
- UsersIndex.filter{ !(age < 42) }
308
- ```
309
-
310
- * Match all filter
311
-
312
- ```json
313
- {"match_all": {}}
314
- ```
315
-
316
- ```ruby
317
- UsersIndex.filter{ match_all }
318
- ```
319
-
320
- * Has child filter
321
-
322
- ```json
323
- {"has_child": {"type": "blog_tag", "query": {"term": {"tag": "something"}}}
324
- {"has_child": {"type": "comment", "filter": {"term": {"user": "john"}}}
325
- ```
326
-
327
- ```ruby
328
- UsersIndex.filter{ has_child(:blog_tag).query(term: {tag: 'something'}) }
329
- UsersIndex.filter{ has_child(:comment).filter{ user == 'john' } }
330
- ```
331
-
332
- * Has parent filter
333
-
334
- ```json
335
- {"has_parent": {"type": "blog", "query": {"term": {"tag": "something"}}}}
336
- {"has_parent": {"type": "blog", "filter": {"term": {"text": "bonsai three"}}}}
337
- ```
338
-
339
- ```ruby
340
- UsersIndex.filter{ has_parent(:blog).query(term: {tag: 'something'}) }
341
- UsersIndex.filter{ has_parent(:blog).filter{ text == 'bonsai three' } }
342
- ```
343
-
344
- See [filters.rb](lib/chewy/query/filters.rb) for more details.
345
-
346
- ## Faceting
347
-
348
- Facets are an optional sidechannel you can request from Elasticsearch describing certain fields of the resulting collection. The most common use for facets is to allow the user to continue filtering specifically within the subset, as opposed to the global index.
349
-
350
- For instance, let's request the `country` field as a facet along with our users collection. We can do this with the #facets method like so:
351
-
352
- ```ruby
353
- UsersIndex.filter{ [...] }.facets({countries: {terms: {field: 'country'}}})
354
- ```
355
-
356
- Let's look at what we asked from Elasticsearch. The facets setter method accepts a hash. You can choose custom/semantic key names for this hash for your own convenience (in this case I used the plural version of the actual field), in our case `countries`. The following nested hash tells ES to grab and aggregate values (terms) from the `country` field on our indexed records.
357
-
358
- The response will include the `:facets` sidechannel:
359
-
360
- ```
361
- < { ... ,"facets":{"countries":{"_type":"terms","missing":?,"total":?,"other":?,"terms":[{"term":"USA","count":?},{"term":"Brazil","count":?}, ...}}
362
- ```
363
-
364
- ## Aggregations
365
-
366
- Aggregations are part of the optional sidechannel that can be requested with a query.
367
-
368
- You interact with aggregations using the composable #aggregations method (or its alias #aggs)
369
-
370
- Let's look at an example.
371
-
372
- ```ruby
373
- class UsersIndex < Chewy::Index
374
- define_type User do
375
- field :name
376
- field :rating
377
- end
378
- end
379
-
380
- all_johns = UsersIndex::User.filter { name == 'john' }.aggs({ avg_rating: { avg: { field: 'rating' } } })
381
-
382
- avg_johns_rating = all_johns.aggs
383
- # => {"avg_rating"=>{"value"=>3.5}}
384
- ```
385
-
386
- It is convenient to name aggregations that you intend to reuse regularly. This is achieve with the .aggregation method,
387
- which is also available under the .agg alias method.
388
-
389
- Here's the same example from before
390
-
391
- ```ruby
392
- class UsersIndex < Chewy::Index
393
- define_type User do
394
- field :name
395
- field :rating, type: "long"
396
- agg :avg_rating do
397
- { avg: { field: 'rating' } }
398
- end
399
- end
400
- end
401
-
402
- all_johns = UsersIndex::User.filter { name == 'john' }.aggs(:avg_rating)
403
-
404
- avg_johns_rating = all_johns.aggs
405
- # => {"avg_rating"=>{"value"=>3.5}}
406
- ```
407
-
408
- It is possible to run into collisions between named aggregations. This occurs when there is more than one aggregation
409
- with the same name. To explicitly reference an aggregation you provide a string to the #aggs method of the form:
410
- `index_name#document_type.aggregation_name`
411
-
412
- Consider this example where there are two separate aggregations named `avg_rating`
413
-
414
- ```ruby
415
- class UsersIndex < Chewy::Index
416
- define_type User do
417
- field :name
418
- field :rating, type: "long"
419
- agg :avg_rating do
420
- { avg: { field: 'rating' } }
421
- end
422
- end
423
- define_type Post do
424
- field :title
425
- field :body
426
- field :comments do
427
- field :message
428
- field :rating, type: "long"
429
- end
430
- agg :avg_rating do
431
- { avg: { field: 'comments.rating' } }
432
- end
433
- end
434
- end
435
-
436
- all_docs = UsersIndex.filter {match_all}.aggs("users#user.avg_rating")
437
- all_docs.aggs
438
- # => {"users#user.avg_rating"=>{"value"=>3.5}}
439
- ```
440
-
441
- ## Script fields
442
-
443
- Script fields allow you to execute Elasticsearch's scripting languages such as groovy and javascript. More about supported languages and what scripting is [here](https://www.elastic.co/guide/en/elasticsearch/reference/0.90/modules-scripting.html). This feature allows you to calculate the distance between geo points, for example. This is how to use the DSL:
444
-
445
- ```ruby
446
- UsersIndex.script_fields(
447
- distance: {
448
- params: {
449
- lat: 37.569976,
450
- lon: -122.351591
451
- },
452
- script: "doc['coordinates'].distanceInMiles(lat, lon)"
453
- }
454
- )
455
- ```
456
- Here, `coordinates` is a field with type `geo_point`. There will be a `distance` field for the index's model in the search result.
457
-
458
- ## Script scoring
459
-
460
- Script scoring is used to score the search results. All scores are added to the search request and combined according to boost mode and score mode. This can be useful if, for example, a score function is computationally expensive and it is sufficient to compute the score on a filtered set of documents. For example, you might want to multiply the score by another numeric field in the doc:
461
-
462
- ```ruby
463
- UsersIndex.script_score("_score * doc['my_numeric_field'].value")
464
- ```
465
-
466
- ## Boost Factor
467
-
468
- Boost factors are a way to add a boost to a query where documents match the filter. If you have some users who are experts and some who are regular users, you might want to give the experts a higher score and boost to the top of the search results. You can accomplish this by using the #boost_factor method and adding a boost score of 5 for an expert user:
469
-
470
- ```ruby
471
- UsersIndex.boost_factor(5, filter: {term: {type: 'Expert'}})
472
- ```
473
-
474
- ## Objects loading
475
-
476
- It is possible to load source objects from the database for every search result:
477
-
478
- ```ruby
479
- scope = UsersIndex.filter(range: {rating: {gte: 100}})
480
-
481
- scope.load # => scope is marked to return User instances array
482
- scope.load.query(...) # => since objects are loaded lazily you can complete scope
483
- scope.load(user: { scope: ->{ includes(:country) }}) # you can also pass loading scopes for each
484
- # possibly returned type
485
- scope.load(user: { scope: User.includes(:country) }) # the second scope passing way.
486
- scope.load(scope: ->{ includes(:country) }) # and more common scope applied to every loaded object type.
487
-
488
- scope.only(:id).load # it is optimal to request ids only if you are not planning to use type objects
489
- ```
490
-
491
- The `preload` method takes the same options as `load` and ORM/ODM objects will be loaded, but the scope will still return an array of Chewy wrappers. To access real objects use the `_object` wrapper method:
492
-
493
- ```ruby
494
- UsersIndex.filter(range: {rating: {gte: 100}}).preload(...).query(...).map(&:_object)
495
- ```
496
-
497
- See [loading.rb](lib/chewy/query/loading.rb) for more details.