chewy 0.8.4 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +34 -0
  3. data/.rubocop_todo.yml +44 -0
  4. data/.travis.yml +20 -60
  5. data/Appraisals +15 -40
  6. data/CHANGELOG.md +42 -0
  7. data/Gemfile +1 -0
  8. data/Guardfile +5 -5
  9. data/README.md +155 -6
  10. data/Rakefile +11 -1
  11. data/chewy.gemspec +5 -7
  12. data/gemfiles/rails.3.2.activerecord.gemfile +1 -0
  13. data/gemfiles/rails.3.2.activerecord.kaminari.gemfile +1 -0
  14. data/gemfiles/rails.3.2.activerecord.will_paginate.gemfile +1 -0
  15. data/gemfiles/rails.4.2.activerecord.gemfile +1 -0
  16. data/gemfiles/rails.4.2.activerecord.kaminari.gemfile +1 -0
  17. data/gemfiles/rails.4.2.activerecord.will_paginate.gemfile +1 -0
  18. data/gemfiles/{rails.4.2.mongoid.4.0.0.gemfile → rails.4.2.mongoid.4.0.gemfile} +2 -1
  19. data/gemfiles/{rails.4.2.mongoid.4.0.0.kaminari.gemfile → rails.4.2.mongoid.4.0.kaminari.gemfile} +2 -1
  20. data/gemfiles/{rails.4.2.mongoid.4.0.0.will_paginate.gemfile → rails.4.2.mongoid.4.0.will_paginate.gemfile} +2 -1
  21. data/gemfiles/{rails.4.2.mongoid.5.1.0.gemfile → rails.4.2.mongoid.5.1.gemfile} +2 -1
  22. data/gemfiles/{rails.4.2.mongoid.5.1.0.kaminari.gemfile → rails.4.2.mongoid.5.1.kaminari.gemfile} +2 -1
  23. data/gemfiles/{rails.4.2.mongoid.5.1.0.will_paginate.gemfile → rails.4.2.mongoid.5.1.will_paginate.gemfile} +2 -1
  24. data/gemfiles/{rails.4.0.activerecord.gemfile → rails.5.0.activerecord.gemfile} +4 -2
  25. data/gemfiles/{rails.4.0.mongoid.4.0.0.kaminari.gemfile → rails.5.0.activerecord.kaminari.gemfile} +4 -2
  26. data/gemfiles/{rails.4.0.mongoid.4.0.0.will_paginate.gemfile → rails.5.0.activerecord.will_paginate.gemfile} +4 -2
  27. data/gemfiles/{sequel.4.31.gemfile → sequel.4.38.gemfile} +3 -2
  28. data/lib/chewy.rb +24 -16
  29. data/lib/chewy/backports/deep_dup.rb +1 -1
  30. data/lib/chewy/backports/duplicable.rb +1 -0
  31. data/lib/chewy/config.rb +13 -7
  32. data/lib/chewy/errors.rb +4 -4
  33. data/lib/chewy/fields/base.rb +19 -14
  34. data/lib/chewy/fields/root.rb +11 -9
  35. data/lib/chewy/index.rb +38 -25
  36. data/lib/chewy/index/actions.rb +17 -17
  37. data/lib/chewy/index/settings.rb +3 -4
  38. data/lib/chewy/journal.rb +107 -0
  39. data/lib/chewy/journal/apply.rb +31 -0
  40. data/lib/chewy/journal/clean.rb +24 -0
  41. data/lib/chewy/journal/entry.rb +83 -0
  42. data/lib/chewy/journal/query.rb +87 -0
  43. data/lib/chewy/log_subscriber.rb +8 -8
  44. data/lib/chewy/minitest.rb +1 -0
  45. data/lib/chewy/minitest/helpers.rb +77 -0
  46. data/lib/chewy/minitest/search_index_receiver.rb +80 -0
  47. data/lib/chewy/query.rb +116 -60
  48. data/lib/chewy/query/compose.rb +5 -6
  49. data/lib/chewy/query/criteria.rb +26 -16
  50. data/lib/chewy/query/filters.rb +9 -9
  51. data/lib/chewy/query/loading.rb +2 -2
  52. data/lib/chewy/query/nodes/and.rb +3 -3
  53. data/lib/chewy/query/nodes/base.rb +1 -1
  54. data/lib/chewy/query/nodes/bool.rb +6 -4
  55. data/lib/chewy/query/nodes/equal.rb +6 -6
  56. data/lib/chewy/query/nodes/exists.rb +2 -2
  57. data/lib/chewy/query/nodes/expr.rb +2 -2
  58. data/lib/chewy/query/nodes/field.rb +35 -31
  59. data/lib/chewy/query/nodes/has_child.rb +1 -0
  60. data/lib/chewy/query/nodes/has_parent.rb +1 -0
  61. data/lib/chewy/query/nodes/has_relation.rb +11 -13
  62. data/lib/chewy/query/nodes/match_all.rb +1 -1
  63. data/lib/chewy/query/nodes/missing.rb +2 -2
  64. data/lib/chewy/query/nodes/not.rb +3 -3
  65. data/lib/chewy/query/nodes/or.rb +3 -3
  66. data/lib/chewy/query/nodes/prefix.rb +4 -3
  67. data/lib/chewy/query/nodes/query.rb +3 -3
  68. data/lib/chewy/query/nodes/range.rb +11 -11
  69. data/lib/chewy/query/nodes/raw.rb +1 -1
  70. data/lib/chewy/query/nodes/regexp.rb +15 -11
  71. data/lib/chewy/query/nodes/script.rb +6 -6
  72. data/lib/chewy/query/pagination/will_paginate.rb +2 -2
  73. data/lib/chewy/railtie.rb +3 -3
  74. data/lib/chewy/rake_helper.rb +51 -30
  75. data/lib/chewy/repository.rb +2 -2
  76. data/lib/chewy/rspec.rb +1 -1
  77. data/lib/chewy/rspec/update_index.rb +46 -47
  78. data/lib/chewy/runtime/version.rb +4 -4
  79. data/lib/chewy/search.rb +7 -5
  80. data/lib/chewy/strategy.rb +10 -8
  81. data/lib/chewy/strategy/atomic.rb +2 -2
  82. data/lib/chewy/strategy/base.rb +4 -4
  83. data/lib/chewy/strategy/bypass.rb +1 -2
  84. data/lib/chewy/strategy/sidekiq.rb +2 -0
  85. data/lib/chewy/strategy/urgent.rb +1 -1
  86. data/lib/chewy/type.rb +51 -45
  87. data/lib/chewy/type/adapter/active_record.rb +23 -12
  88. data/lib/chewy/type/adapter/base.rb +4 -4
  89. data/lib/chewy/type/adapter/mongoid.rb +6 -6
  90. data/lib/chewy/type/adapter/object.rb +15 -12
  91. data/lib/chewy/type/adapter/orm.rb +24 -15
  92. data/lib/chewy/type/adapter/sequel.rb +11 -7
  93. data/lib/chewy/type/crutch.rb +4 -3
  94. data/lib/chewy/type/import.rb +51 -32
  95. data/lib/chewy/type/mapping.rb +17 -17
  96. data/lib/chewy/type/observe.rb +9 -7
  97. data/lib/chewy/type/witchcraft.rb +62 -23
  98. data/lib/chewy/type/wrapper.rb +20 -14
  99. data/lib/chewy/version.rb +1 -1
  100. data/lib/generators/chewy/install_generator.rb +3 -3
  101. data/lib/tasks/chewy.rake +28 -23
  102. data/spec/chewy/config_spec.rb +33 -12
  103. data/spec/chewy/fields/base_spec.rb +143 -154
  104. data/spec/chewy/fields/root_spec.rb +22 -20
  105. data/spec/chewy/fields/time_fields_spec.rb +11 -9
  106. data/spec/chewy/index/actions_spec.rb +27 -4
  107. data/spec/chewy/index/aliases_spec.rb +2 -2
  108. data/spec/chewy/index/settings_spec.rb +72 -50
  109. data/spec/chewy/index_spec.rb +95 -43
  110. data/spec/chewy/journal/apply_spec.rb +120 -0
  111. data/spec/chewy/journal/entry_spec.rb +237 -0
  112. data/spec/chewy/journal_spec.rb +173 -0
  113. data/spec/chewy/minitest/helpers_spec.rb +90 -0
  114. data/spec/chewy/minitest/search_index_receiver_spec.rb +120 -0
  115. data/spec/chewy/query/criteria_spec.rb +504 -237
  116. data/spec/chewy/query/filters_spec.rb +94 -66
  117. data/spec/chewy/query/loading_spec.rb +76 -40
  118. data/spec/chewy/query/nodes/and_spec.rb +3 -7
  119. data/spec/chewy/query/nodes/bool_spec.rb +5 -13
  120. data/spec/chewy/query/nodes/equal_spec.rb +20 -20
  121. data/spec/chewy/query/nodes/exists_spec.rb +7 -7
  122. data/spec/chewy/query/nodes/has_child_spec.rb +42 -23
  123. data/spec/chewy/query/nodes/has_parent_spec.rb +42 -23
  124. data/spec/chewy/query/nodes/match_all_spec.rb +2 -2
  125. data/spec/chewy/query/nodes/missing_spec.rb +6 -5
  126. data/spec/chewy/query/nodes/not_spec.rb +3 -7
  127. data/spec/chewy/query/nodes/or_spec.rb +3 -7
  128. data/spec/chewy/query/nodes/prefix_spec.rb +6 -6
  129. data/spec/chewy/query/nodes/query_spec.rb +3 -3
  130. data/spec/chewy/query/nodes/range_spec.rb +19 -19
  131. data/spec/chewy/query/nodes/raw_spec.rb +2 -2
  132. data/spec/chewy/query/nodes/regexp_spec.rb +31 -19
  133. data/spec/chewy/query/nodes/script_spec.rb +5 -5
  134. data/spec/chewy/query/pagination/kaminari_spec.rb +2 -2
  135. data/spec/chewy/query/pagination/will_paginage_spec.rb +6 -7
  136. data/spec/chewy/query/pagination_spec.rb +2 -3
  137. data/spec/chewy/query_spec.rb +208 -145
  138. data/spec/chewy/repository_spec.rb +8 -8
  139. data/spec/chewy/rspec/update_index_spec.rb +180 -111
  140. data/spec/chewy/search_spec.rb +8 -8
  141. data/spec/chewy/strategy/active_job_spec.rb +2 -2
  142. data/spec/chewy/strategy/atomic_spec.rb +4 -1
  143. data/spec/chewy/strategy/resque_spec.rb +2 -2
  144. data/spec/chewy/strategy/sidekiq_spec.rb +2 -2
  145. data/spec/chewy/type/actions_spec.rb +1 -1
  146. data/spec/chewy/type/adapter/active_record_spec.rb +255 -149
  147. data/spec/chewy/type/adapter/mongoid_spec.rb +169 -108
  148. data/spec/chewy/type/adapter/object_spec.rb +56 -40
  149. data/spec/chewy/type/adapter/sequel_spec.rb +248 -163
  150. data/spec/chewy/type/import_spec.rb +78 -47
  151. data/spec/chewy/type/mapping_spec.rb +6 -6
  152. data/spec/chewy/type/observe_spec.rb +20 -14
  153. data/spec/chewy/type/witchcraft_spec.rb +89 -43
  154. data/spec/chewy/type_spec.rb +4 -3
  155. data/spec/chewy_spec.rb +10 -8
  156. data/spec/spec_helper.rb +3 -0
  157. data/spec/support/active_record.rb +1 -1
  158. data/spec/support/class_helpers.rb +10 -11
  159. data/spec/support/mongoid.rb +2 -2
  160. data/spec/support/sequel.rb +1 -1
  161. metadata +65 -35
  162. data/gemfiles/rails.4.0.activerecord.kaminari.gemfile +0 -14
  163. data/gemfiles/rails.4.0.activerecord.will_paginate.gemfile +0 -14
  164. data/gemfiles/rails.4.0.mongoid.4.0.0.gemfile +0 -15
  165. data/gemfiles/rails.4.0.mongoid.5.1.0.gemfile +0 -15
  166. data/gemfiles/rails.4.0.mongoid.5.1.0.kaminari.gemfile +0 -14
  167. data/gemfiles/rails.4.0.mongoid.5.1.0.will_paginate.gemfile +0 -14
  168. data/gemfiles/rails.4.1.activerecord.gemfile +0 -15
  169. data/gemfiles/rails.4.1.activerecord.kaminari.gemfile +0 -14
  170. data/gemfiles/rails.4.1.activerecord.will_paginate.gemfile +0 -14
  171. data/gemfiles/rails.4.1.mongoid.4.0.0.gemfile +0 -15
  172. data/gemfiles/rails.4.1.mongoid.4.0.0.kaminari.gemfile +0 -14
  173. data/gemfiles/rails.4.1.mongoid.4.0.0.will_paginate.gemfile +0 -14
  174. data/gemfiles/rails.4.1.mongoid.5.1.0.gemfile +0 -15
  175. data/gemfiles/rails.4.1.mongoid.5.1.0.kaminari.gemfile +0 -14
  176. data/gemfiles/rails.4.1.mongoid.5.1.0.will_paginate.gemfile +0 -14
  177. data/gemfiles/rails.5.0.0.beta3.activerecord.gemfile +0 -16
  178. data/gemfiles/rails.5.0.0.beta3.activerecord.kaminari.gemfile +0 -16
  179. data/gemfiles/rails.5.0.0.beta3.activerecord.will_paginate.gemfile +0 -15
@@ -18,7 +18,7 @@ if defined?(::Kaminari)
18
18
  specify { expect(search.total_pages).to eq(0) }
19
19
 
20
20
  context do
21
- let(:data) { 10.times.map { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
21
+ let(:data) { Array.new(10) { |i| { id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next }.stringify_keys! } }
22
22
 
23
23
  before { ProductsIndex::Product.import!(data.map { |h| double(h) }) }
24
24
  before { allow(::Kaminari.config).to receive_messages(default_per_page: 3) }
@@ -41,7 +41,7 @@ if defined?(::Kaminari)
41
41
 
42
42
  describe '#total_count' do
43
43
  specify { expect(search.per(4).page(1).total_count).to eq(10) }
44
- specify { expect(search.filter(numeric_range: {age: {gt: 20}}).limit(3).total_count).to eq(8) }
44
+ specify { expect(search.filter(numeric_range: { age: { gt: 20 } }).limit(3).total_count).to eq(8) }
45
45
  end
46
46
 
47
47
  describe '#load' do
@@ -1,6 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
-
4
3
  if defined?(::WillPaginate)
5
4
  describe Chewy::Query::Pagination::WillPaginate do
6
5
  before { Chewy.massacre }
@@ -16,10 +15,10 @@ if defined?(::WillPaginate)
16
15
 
17
16
  let(:search) { ProductsIndex.order(:age) }
18
17
 
19
- specify { expect(search.total_pages).to eq(1) } #defaults to 1 on will_paginate
18
+ specify { expect(search.total_pages).to eq(1) } # defaults to 1 on will_paginate
20
19
 
21
20
  context do
22
- let(:data) { 10.times.map { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
21
+ let(:data) { Array.new(10) { |i| { id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next }.stringify_keys! } }
23
22
 
24
23
  before { ProductsIndex::Product.import!(data.map { |h| double(h) }) }
25
24
  before { allow(::WillPaginate).to receive_messages(per_page: 3) }
@@ -30,7 +29,7 @@ if defined?(::WillPaginate)
30
29
  specify { expect(search.page(2).map { |e| e.attributes.except('_score', '_explanation') }).to eq(data[3..5]) }
31
30
  end
32
31
 
33
- describe "#paginate" do
32
+ describe '#paginate' do
34
33
  specify { expect(search.paginate(page: 2, per_page: 4).map { |e| e.attributes.except('_score', '_explanation') }).to eq(data[4..7]) }
35
34
  specify { expect(search.paginate(per_page: 2, page: 3).page(3).map { |e| e.attributes.except('_score', '_explanation') }).to eq(data[4..5]) }
36
35
  specify { expect(search.paginate(per_page: 5).map { |e| e.attributes.except('_score', '_explanation') }).to eq(data[0..4]) }
@@ -44,7 +43,7 @@ if defined?(::WillPaginate)
44
43
 
45
44
  describe '#total_entries' do
46
45
  specify { expect(search.paginate(page: 1, per_page: 4).total_entries).to eq(10) }
47
- specify { expect(search.filter(numeric_range: {age: {gt: 20}}).limit(3).total_entries).to eq(8) }
46
+ specify { expect(search.filter(numeric_range: { age: { gt: 20 } }).limit(3).total_entries).to eq(8) }
48
47
  end
49
48
 
50
49
  describe '#load' do
@@ -52,8 +51,8 @@ if defined?(::WillPaginate)
52
51
  specify { expect(search.paginate(per_page: 2, page: 3).load.first.age).to eq(50) }
53
52
  specify { expect(search.paginate(per_page: 2, page: 3).load.page(2).load.first.age).to eq(30) }
54
53
 
55
- specify { expect(search.paginate(per_page:4, page:1).load.total_count).to eq(10) }
56
- specify { expect(search.paginate(per_page:2, page:3).load.total_pages).to eq(5) }
54
+ specify { expect(search.paginate(per_page: 4, page: 1).load.total_count).to eq(10) }
55
+ specify { expect(search.paginate(per_page: 2, page: 3).load.total_pages).to eq(5) }
57
56
  end
58
57
  end
59
58
  end
@@ -17,14 +17,14 @@ describe Chewy::Query::Pagination do
17
17
  specify { expect(search.total_count).to eq(0) }
18
18
 
19
19
  context do
20
- let(:data) { 10.times.map { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
20
+ let(:data) { Array.new(10) { |i| { id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next }.stringify_keys! } }
21
21
 
22
22
  before { ProductsIndex::Product.import!(data.map { |h| double(h) }) }
23
23
 
24
24
  describe '#total_count' do
25
25
  specify { expect(search.total_count).to eq(10) }
26
26
  specify { expect(search.limit(5).total_count).to eq(10) }
27
- specify { expect(search.filter(range: {age: {gt: 20}}).limit(3).total_count).to eq(8) }
27
+ specify { expect(search.filter(range: { age: { gt: 20 } }).limit(3).total_count).to eq(8) }
28
28
  end
29
29
 
30
30
  describe '#load' do
@@ -32,5 +32,4 @@ describe Chewy::Query::Pagination do
32
32
  specify { expect(search.limit(5).load.total_count).to eq(10) }
33
33
  end
34
34
  end
35
-
36
35
  end
@@ -20,9 +20,9 @@ describe Chewy::Query do
20
20
  end
21
21
 
22
22
  context 'integration' do
23
- let(:products) { 3.times.map { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
24
- let(:cities) { 3.times.map { |i| {id: i.next.to_s}.stringify_keys! } }
25
- let(:countries) { 3.times.map { |i| {id: i.next.to_s}.stringify_keys! } }
23
+ let(:products) { Array.new(3) { |i| { id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next }.stringify_keys! } }
24
+ let(:cities) { Array.new(3) { |i| { id: i.next.to_s }.stringify_keys! } }
25
+ let(:countries) { Array.new(3) { |i| { id: i.next.to_s }.stringify_keys! } }
26
26
  before do
27
27
  ProductsIndex::Product.import!(products.map { |h| double(h) })
28
28
  ProductsIndex::City.import!(cities.map { |h| double(h) })
@@ -33,19 +33,20 @@ describe Chewy::Query do
33
33
  specify { expect(subject.first._data).to be_a Hash }
34
34
  specify { expect(subject.limit(6).count).to eq(6) }
35
35
  specify { expect(subject.offset(6).count).to eq(3) }
36
- specify { expect(subject.query(match: {name: 'name3'}).highlight(fields: {name: {}}).first.name).to eq('Name3') }
37
- specify { expect(subject.query(match: {name: 'name3'}).highlight(fields: {name: {}}).first.name_highlight).to eq('<em>Name3</em>') }
38
- specify { expect(subject.query(match: {name: 'name3'}).highlight(fields: {name: {}}).first._data['_source']['name']).to eq('Name3') }
36
+ specify { expect(subject.query(match: { name: 'name3' }).highlight(fields: { name: {} }).first.name).to eq('Name3') }
37
+ specify { expect(subject.query(match: { name: 'name3' }).highlight(fields: { name: {} }).first.name_highlight).to eq('<em>Name3</em>') }
38
+ specify { expect(subject.query(match: { name: 'name3' }).highlight(fields: { name: {} }).first._data['_source']['name']).to eq('Name3') }
39
39
  specify { expect(subject.types(:product).count).to eq(3) }
40
40
  specify { expect(subject.types(:product, :country).count).to eq(6) }
41
- specify { expect(subject.filter(term: {age: 10}).count).to eq(1) }
42
- specify { expect(subject.query(term: {age: 10}).count).to eq(1) }
41
+ specify { expect(subject.filter(term: { age: 10 }).count).to eq(1) }
42
+ specify { expect(subject.query(term: { age: 10 }).count).to eq(1) }
43
+ specify { expect(subject.order(nil).count).to eq(9) }
43
44
  specify { expect(subject.search_type(:count).count).to eq(0) }
44
45
  specify { expect(subject.search_type(:count).total).to eq(9) }
45
46
  end
46
47
 
47
48
  describe '#==' do
48
- let(:data) { 3.times.map { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
49
+ let(:data) { Array.new(3) { |i| { id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next }.stringify_keys! } }
49
50
  before { ProductsIndex::Product.import!(data.map { |h| double(h) }) }
50
51
 
51
52
  specify { expect(subject.query(match: 'hello')).to eq(subject.query(match: 'hello')) }
@@ -95,6 +96,7 @@ describe Chewy::Query do
95
96
  specify { expect(subject.limit(10)).not_to eq(subject) }
96
97
  specify { expect(subject.limit(10).criteria.request_options).to include(size: 10) }
97
98
  specify { expect { subject.limit(10) }.not_to change { subject.criteria.request_options } }
99
+ specify { expect(subject.limit { 20 / 2 }.criteria.request_body[:body]).to include(size: 10) }
98
100
  end
99
101
 
100
102
  describe '#offset' do
@@ -102,6 +104,15 @@ describe Chewy::Query do
102
104
  specify { expect(subject.offset(10)).not_to eq(subject) }
103
105
  specify { expect(subject.offset(10).criteria.request_options).to include(from: 10) }
104
106
  specify { expect { subject.offset(10) }.not_to change { subject.criteria.request_options } }
107
+ specify { expect(subject.offset { 20 / 2 }.criteria.request_body[:body]).to include(from: 10) }
108
+ end
109
+
110
+ describe '#track_scores' do
111
+ specify { expect(subject.track_scores(true)).to be_a described_class }
112
+ specify { expect(subject.track_scores(true)).not_to eq(subject) }
113
+ specify { expect(subject.track_scores(true).criteria.request_options).to include(track_scores: true) }
114
+ specify { expect { subject.track_scores(true) }.not_to change { subject.criteria.request_options } }
115
+ specify { expect(subject.track_scores(false).criteria.request_body[:body]).to include(track_scores: false) }
105
116
  end
106
117
 
107
118
  describe '#script_fields' do
@@ -114,64 +125,82 @@ describe Chewy::Query do
114
125
  describe '#script_score' do
115
126
  specify { expect(subject.script_score('23')).to be_a described_class }
116
127
  specify { expect(subject.script_score('23')).not_to eq(subject) }
117
- specify { expect(subject.script_score('23').criteria.scores).to eq([ { script_score: { script: '23' } } ]) }
128
+ specify { expect(subject.script_score('23').criteria.scores).to eq([{ script_score: { script: '23' } }]) }
118
129
  specify { expect { subject.script_score('23') }.not_to change { subject.criteria.scores } }
119
- specify { expect(subject.script_score('23 * factor', params: { factor: 0.5}).criteria.scores).to eq([{ script_score: { script: '23 * factor', params: { factor: 0.5} } }]) }
130
+ specify { expect(subject.script_score('23 * factor', params: { factor: 0.5 }).criteria.scores).to eq([{ script_score: { script: '23 * factor', params: { factor: 0.5 } } }]) }
120
131
  end
121
132
 
122
133
  describe '#boost_factor' do
123
134
  specify { expect(subject.boost_factor('23')).to be_a described_class }
124
135
  specify { expect(subject.boost_factor('23')).not_to eq(subject) }
125
- specify { expect(subject.boost_factor('23').criteria.scores).to eq([ { boost_factor: 23 } ]) }
136
+ specify { expect(subject.boost_factor('23').criteria.scores).to eq([{ boost_factor: 23 }]) }
126
137
  specify { expect { subject.boost_factor('23') }.not_to change { subject.criteria.scores } }
127
- specify { expect(subject.boost_factor('23', filter: { foo: :bar}).criteria.scores).to eq([{ boost_factor: 23, filter: { foo: :bar } }]) }
138
+ specify { expect(subject.boost_factor('23', filter: { foo: :bar }).criteria.scores).to eq([{ boost_factor: 23, filter: { foo: :bar } }]) }
139
+ end
140
+
141
+ describe '#weight' do
142
+ specify { expect(subject.weight('23')).to be_a described_class }
143
+ specify { expect(subject.weight('23')).not_to eq(subject) }
144
+ specify { expect(subject.weight('23').criteria.scores).to eq([{ weight: 23 }]) }
145
+ specify { expect { subject.weight('23') }.not_to change { subject.criteria.scores } }
146
+ specify { expect(subject.weight('23', filter: { foo: :bar }).criteria.scores).to eq([{ weight: 23, filter: { foo: :bar } }]) }
128
147
  end
129
148
 
130
149
  describe '#random_score' do
131
150
  specify { expect(subject.random_score('23')).to be_a described_class }
132
151
  specify { expect(subject.random_score('23')).not_to eq(subject) }
133
- specify { expect(subject.random_score('23').criteria.scores).to eq([ { random_score: { seed: 23 } } ]) }
152
+ specify { expect(subject.random_score('23').criteria.scores).to eq([{ random_score: { seed: 23 } }]) }
134
153
  specify { expect { subject.random_score('23') }.not_to change { subject.criteria.scores } }
135
- specify { expect(subject.random_score('23', filter: { foo: :bar}).criteria.scores).to eq([{ random_score: { seed: 23 }, filter: { foo: :bar } }]) }
154
+ specify { expect(subject.random_score('23', filter: { foo: :bar }).criteria.scores).to eq([{ random_score: { seed: 23 }, filter: { foo: :bar } }]) }
136
155
  end
137
156
 
138
157
  describe '#field_value_score' do
139
158
  specify { expect(subject.field_value_factor(field: :boost)).to be_a described_class }
140
159
  specify { expect(subject.field_value_factor(field: :boost)).not_to eq(subject) }
141
- specify { expect(subject.field_value_factor(field: :boost).criteria.scores).to eq([ { field_value_factor: { field: :boost } } ]) }
160
+ specify { expect(subject.field_value_factor(field: :boost).criteria.scores).to eq([{ field_value_factor: { field: :boost } }]) }
142
161
  specify { expect { subject.field_value_factor(field: :boost) }.not_to change { subject.criteria.scores } }
143
- specify { expect(subject.field_value_factor({ field: :boost }, filter: { foo: :bar}).criteria.scores).to eq([{ field_value_factor: { field: :boost }, filter: { foo: :bar } }]) }
162
+ specify { expect(subject.field_value_factor({ field: :boost }, filter: { foo: :bar }).criteria.scores).to eq([{ field_value_factor: { field: :boost }, filter: { foo: :bar } }]) }
144
163
  end
145
164
 
146
165
  describe '#decay' do
147
166
  specify { expect(subject.decay(:gauss, :field)).to be_a described_class }
148
167
  specify { expect(subject.decay(:gauss, :field)).not_to eq(subject) }
149
- specify { expect(subject.decay(:gauss, :field).criteria.scores).to eq([ {
150
- gauss: {
151
- field: {}
152
- }
153
- }]) }
168
+ specify do
169
+ expect(subject.decay(:gauss, :field).criteria.scores).to eq([{
170
+ gauss: {
171
+ field: {}
172
+ }
173
+ }])
174
+ end
154
175
  specify { expect { subject.decay(:gauss, :field) }.not_to change { subject.criteria.scores } }
155
- specify {
176
+ specify do
156
177
  expect(subject.decay(:gauss, :field,
157
- origin: '11, 12',
158
- scale: '2km',
159
- offset: '5km',
160
- decay: 0.4,
161
- filter: { foo: :bar }).criteria.scores).to eq([
162
- {
163
- gauss: {
164
- field: {
165
- origin: '11, 12',
166
- scale: '2km',
167
- offset: '5km',
168
- decay: 0.4
169
- }
170
- },
171
- filter: { foo: :bar }
172
- }
173
- ])
174
- }
178
+ origin: '11, 12',
179
+ scale: '2km',
180
+ offset: '5km',
181
+ decay: 0.4,
182
+ filter: { foo: :bar }).criteria.scores).to eq([
183
+ {
184
+ gauss: {
185
+ field: {
186
+ origin: '11, 12',
187
+ scale: '2km',
188
+ offset: '5km',
189
+ decay: 0.4
190
+ }
191
+ },
192
+ filter: { foo: :bar }
193
+ }
194
+ ])
195
+ end
196
+ end
197
+
198
+ describe '#preference' do
199
+ specify { expect(subject.preference(:_primary)).to be_a described_class }
200
+ specify { expect(subject.preference(:_primary)).not_to eq(subject) }
201
+ specify { expect(subject.preference(:_primary).criteria.search_options).to include(preference: :_primary) }
202
+ specify { expect { subject.preference(:_primary) }.not_to change { subject.criteria.search_options } }
203
+ specify { expect(subject.preference(:_primary).criteria.request_body).to include(preference: :_primary) }
175
204
  end
176
205
 
177
206
  describe '#facets' do
@@ -182,24 +211,24 @@ describe Chewy::Query do
182
211
 
183
212
  specify do
184
213
  skip_on_version_gte('2.0')
185
- expect(subject.facets(term: {field: 'hello'})).to be_a described_class
214
+ expect(subject.facets(term: { field: 'hello' })).to be_a described_class
186
215
  end
187
216
  specify do
188
217
  skip_on_version_gte('2.0')
189
- expect(subject.facets(term: {field: 'hello'})).not_to eq(subject)
218
+ expect(subject.facets(term: { field: 'hello' })).not_to eq(subject)
190
219
  end
191
220
  specify do
192
221
  skip_on_version_gte('2.0')
193
- expect(subject.facets(term: {field: 'hello'}).criteria.facets).to include(term: {field: 'hello'})
222
+ expect(subject.facets(term: { field: 'hello' }).criteria.facets).to include(term: { field: 'hello' })
194
223
  end
195
224
  specify do
196
225
  skip_on_version_gte('2.0')
197
- expect { subject.facets(term: {field: 'hello'}) }.not_to change { subject.criteria.facets }
226
+ expect { subject.facets(term: { field: 'hello' }) }.not_to change { subject.criteria.facets }
198
227
  end
199
228
 
200
229
  context 'results', :orm do
201
230
  before { stub_model(:city) }
202
- let(:cities) { 10.times.map { |i| City.create! id: i + 1, name: "name#{i}", rating: i % 3 } }
231
+ let(:cities) { Array.new(10) { |i| City.create! id: i + 1, name: "name#{i}", rating: i % 3 } }
203
232
 
204
233
  before do
205
234
  stub_index(:cities) do
@@ -217,28 +246,25 @@ describe Chewy::Query do
217
246
  end
218
247
  specify do
219
248
  skip_on_version_gte('2.0')
220
- expect(CitiesIndex.facets(ratings: {terms: {field: 'rating'}}).facets).to eq({
221
- 'ratings' => {
222
- '_type' => 'terms', 'missing' => 0, 'total' => 10, 'other' => 0,
223
- 'terms' => [
224
- {'term' => 0, 'count' => 4},
225
- {'term' => 2, 'count' => 3},
226
- {'term' => 1, 'count' => 3}
227
- ]
228
- }
229
- })
249
+ expect(CitiesIndex.facets(ratings: { terms: { field: 'rating' } }).facets).to eq('ratings' => {
250
+ '_type' => 'terms', 'missing' => 0, 'total' => 10, 'other' => 0,
251
+ 'terms' => [
252
+ { 'term' => 0, 'count' => 4 },
253
+ { 'term' => 2, 'count' => 3 },
254
+ { 'term' => 1, 'count' => 3 }
255
+ ]
256
+ })
230
257
  end
231
258
  end
232
259
  end
233
260
 
234
261
  describe '#aggregations' do
235
- specify { expect(subject.aggregations(aggregation1: {field: 'hello'})).to be_a described_class }
236
- specify { expect(subject.aggregations(aggregation1: {field: 'hello'})).not_to eq(subject) }
237
- specify { expect(subject.aggregations(aggregation1: {field: 'hello'}).criteria.aggregations).to include(aggregation1: {field: 'hello'}) }
238
- specify { expect { subject.aggregations(aggregation1: {field: 'hello'}) }.not_to change { subject.criteria.aggregations } }
262
+ specify { expect(subject.aggregations(aggregation1: { field: 'hello' })).to be_a described_class }
263
+ specify { expect(subject.aggregations(aggregation1: { field: 'hello' })).not_to eq(subject) }
264
+ specify { expect(subject.aggregations(aggregation1: { field: 'hello' }).criteria.aggregations).to include(aggregation1: { field: 'hello' }) }
265
+ specify { expect { subject.aggregations(aggregation1: { field: 'hello' }) }.not_to change { subject.criteria.aggregations } }
239
266
 
240
267
  context 'when requesting a named aggregation' do
241
-
242
268
  before do
243
269
  stub_index(:products) do
244
270
  define_type :product do
@@ -292,23 +318,25 @@ describe Chewy::Query do
292
318
  end
293
319
  end
294
320
 
295
- it "is the aggregation definition that was last defined" do
321
+ it 'is the aggregation definition that was last defined' do
296
322
  expect(subject.aggregations(:named_agg).criteria.aggregations).to include(named_agg: { avg: { field: 'comments.rating' } })
297
323
  end
298
324
 
299
- context "when the fully qualified aggregation name is provided" do
300
- specify { expect(subject
301
- .aggregations("products#product.named_agg")
325
+ context 'when the fully qualified aggregation name is provided' do
326
+ specify do
327
+ expect(subject
328
+ .aggregations('products#product.named_agg')
302
329
  .criteria
303
330
  .aggregations)
304
- .to include({ "products#product.named_agg" => { avg: { field: 'title.subfield1' } } }) }
331
+ .to include('products#product.named_agg' => { avg: { field: 'title.subfield1' } })
332
+ end
305
333
  end
306
334
  end
307
335
  end
308
336
 
309
337
  context 'results', :orm do
310
338
  before { stub_model(:city) }
311
- let(:cities) { 10.times.map { |i| City.create! id: i + 1, name: "name#{i}", rating: i % 3 } }
339
+ let(:cities) { Array.new(10) { |i| City.create! id: i + 1, name: "name#{i}", rating: i % 3 } }
312
340
 
313
341
  context do
314
342
  before do
@@ -322,25 +350,27 @@ describe Chewy::Query do
322
350
  before { CitiesIndex::City.import! cities }
323
351
 
324
352
  specify { expect(CitiesIndex.aggregations).to eq({}) }
325
- specify { expect(CitiesIndex.aggregations(ratings: {terms: {field: 'rating'}})
353
+ specify do
354
+ expect(CitiesIndex.aggregations(ratings: { terms: { field: 'rating' } })
326
355
  .aggregations['ratings']['buckets'].map { |h| h.slice('key', 'doc_count') }).to eq([
327
- { 'key' => 0, 'doc_count' => 4 },
328
- { 'key' => 1, 'doc_count' => 3 },
329
- { 'key' => 2, 'doc_count' => 3 }
330
- ]) }
356
+ { 'key' => 0, 'doc_count' => 4 },
357
+ { 'key' => 1, 'doc_count' => 3 },
358
+ { 'key' => 2, 'doc_count' => 3 }
359
+ ])
360
+ end
331
361
  end
332
362
  end
333
363
  end
334
364
 
335
365
  describe '#suggest' do
336
- specify { subject.suggest(name1: {text: 'hello', term: {field: 'name'}}) }
337
- specify { expect(subject.suggest(name1: {text: 'hello'})).not_to eq(subject) }
338
- specify { expect(subject.suggest(name1: {text: 'hello'}).criteria.suggest).to include(name1: {text: 'hello'}) }
339
- specify { expect { subject.suggest(name1: {text: 'hello'}) }.not_to change { subject.criteria.suggest } }
366
+ specify { subject.suggest(name1: { text: 'hello', term: { field: 'name' } }) }
367
+ specify { expect(subject.suggest(name1: { text: 'hello' })).not_to eq(subject) }
368
+ specify { expect(subject.suggest(name1: { text: 'hello' }).criteria.suggest).to include(name1: { text: 'hello' }) }
369
+ specify { expect { subject.suggest(name1: { text: 'hello' }) }.not_to change { subject.criteria.suggest } }
340
370
 
341
371
  context 'results', :orm do
342
372
  before { stub_model(:city) }
343
- let(:cities) { 10.times.map { |i| City.create! id: i + 1, name: "name#{i}" } }
373
+ let(:cities) { Array.new(10) { |i| City.create! id: i + 1, name: "name#{i}" } }
344
374
 
345
375
  context do
346
376
  before do
@@ -354,26 +384,25 @@ describe Chewy::Query do
354
384
  before { CitiesIndex::City.import! cities }
355
385
 
356
386
  specify { expect(CitiesIndex.suggest).to eq({}) }
357
- specify { expect(CitiesIndex.suggest(name: {text: 'name', term: {field: 'name'}}).suggest).to eq({
358
- 'name' => [
359
- {'text' => 'name', 'offset' => 0, 'length' => 4, 'options' => [
360
- {'text' => 'name0', 'score' => 0.75, 'freq' => 1},
361
- {'text' => 'name1', 'score' => 0.75, 'freq' => 1},
362
- {'text' => 'name2', 'score' => 0.75, 'freq' => 1},
363
- {'text' => 'name3', 'score' => 0.75, 'freq' => 1},
364
- {'text' => 'name4', 'score' => 0.75, 'freq' => 1}
365
- ]
366
- }
367
- ] })
368
- }
387
+ specify do
388
+ expect(CitiesIndex.suggest(name: { text: 'name', term: { field: 'name' } }).suggest).to eq('name' => [
389
+ { 'text' => 'name', 'offset' => 0, 'length' => 4, 'options' => [
390
+ { 'text' => 'name0', 'score' => 0.75, 'freq' => 1 },
391
+ { 'text' => 'name1', 'score' => 0.75, 'freq' => 1 },
392
+ { 'text' => 'name2', 'score' => 0.75, 'freq' => 1 },
393
+ { 'text' => 'name3', 'score' => 0.75, 'freq' => 1 },
394
+ { 'text' => 'name4', 'score' => 0.75, 'freq' => 1 }
395
+ ] }
396
+ ])
397
+ end
369
398
  end
370
399
  end
371
400
  end
372
401
 
373
402
  describe '#delete_all' do
374
- let(:products) { 3.times.map { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
375
- let(:cities) { 3.times.map { |i| {id: i.next.to_s}.stringify_keys! } }
376
- let(:countries) { 3.times.map { |i| {id: i.next.to_s}.stringify_keys! } }
403
+ let(:products) { Array.new(3) { |i| { id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next }.stringify_keys! } }
404
+ let(:cities) { Array.new(3) { |i| { id: i.next.to_s }.stringify_keys! } }
405
+ let(:countries) { Array.new(3) { |i| { id: i.next.to_s }.stringify_keys! } }
377
406
 
378
407
  before do
379
408
  ProductsIndex::Product.import!(products.map { |h| double(h) })
@@ -383,46 +412,51 @@ describe Chewy::Query do
383
412
 
384
413
  specify do
385
414
  skip_on_plugin_missing_from_version('delete-by-query', '2.0')
386
- expect {
387
- subject.query(match: {name: 'name3'}).delete_all
388
- Chewy.client.indices.refresh(index: 'products') }.to change { ProductsIndex.total }.from(9).to(8)
415
+ expect do
416
+ subject.query(match: { name: 'name3' }).delete_all
417
+ Chewy.client.indices.refresh(index: 'products')
418
+ end.to change { ProductsIndex.total }.from(9).to(8)
389
419
  end
390
420
  specify do
391
421
  skip_on_plugin_missing_from_version('delete-by-query', '2.0')
392
- expect {
422
+ expect do
393
423
  subject.filter { age == [10, 20] }.delete_all
394
- Chewy.client.indices.refresh(index: 'products') }.to change { ProductsIndex.total_count }.from(9).to(7)
424
+ Chewy.client.indices.refresh(index: 'products')
425
+ end.to change { ProductsIndex.total_count }.from(9).to(7)
395
426
  end
396
427
  specify do
397
428
  skip_on_plugin_missing_from_version('delete-by-query', '2.0')
398
- expect {
429
+ expect do
399
430
  subject.types(:product).delete_all
400
- Chewy.client.indices.refresh(index: 'products') }.to change { ProductsIndex::Product.total_entries }.from(3).to(0)
431
+ Chewy.client.indices.refresh(index: 'products')
432
+ end.to change { ProductsIndex::Product.total_entries }.from(3).to(0)
401
433
  end
402
434
  specify do
403
435
  skip_on_plugin_missing_from_version('delete-by-query', '2.0')
404
- expect {
436
+ expect do
405
437
  ProductsIndex.delete_all
406
- Chewy.client.indices.refresh(index: 'products') }.to change { ProductsIndex.total }.from(9).to(0)
438
+ Chewy.client.indices.refresh(index: 'products')
439
+ end.to change { ProductsIndex.total }.from(9).to(0)
407
440
  end
408
441
  specify do
409
442
  skip_on_plugin_missing_from_version('delete-by-query', '2.0')
410
- expect {
443
+ expect do
411
444
  ProductsIndex::City.delete_all
412
- Chewy.client.indices.refresh(index: 'products') }.to change { ProductsIndex.total }.from(9).to(6)
445
+ Chewy.client.indices.refresh(index: 'products')
446
+ end.to change { ProductsIndex.total }.from(9).to(6)
413
447
  end
414
448
 
415
449
  specify do
416
450
  skip_on_version_lt('2.0')
417
- expect(Chewy.client.nodes).to receive(:info).and_return({"nodes" => {"a" => {"plugins" => {"name" => "hello"}}}})
418
- expect { ProductsIndex.delete_all }.to raise_error(Chewy::PluginMissing).with_message("install delete-by-query plugin")
451
+ expect(Chewy.client.nodes).to receive(:info).and_return('nodes' => { 'a' => { 'plugins' => { 'name' => 'hello' } } })
452
+ expect { ProductsIndex.delete_all }.to raise_error(Chewy::PluginMissing).with_message('install delete-by-query plugin')
419
453
  end
420
454
  end
421
455
 
422
456
  describe '#find' do
423
- let(:products) { 3.times.map { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
424
- let(:cities) { 1.times.map { |i| {id: '4'}.stringify_keys! } }
425
- let(:countries) { 1.times.map { |i| {id: '4'}.stringify_keys! } }
457
+ let(:products) { Array.new(3) { |i| { id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next }.stringify_keys! } }
458
+ let(:cities) { Array.new(1) { { id: '4' }.stringify_keys! } }
459
+ let(:countries) { Array.new(1) { { id: '4' }.stringify_keys! } }
426
460
 
427
461
  before do
428
462
  ProductsIndex::Product.import!(products.map { |h| double(h) })
@@ -444,6 +478,29 @@ describe Chewy::Query do
444
478
  specify { expect { subject.find([10, 20]) }.to raise_error Chewy::DocumentNotFound }
445
479
  end
446
480
 
481
+ describe '#exists?' do
482
+ let(:data) { Array.new(10) { |i| { id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next } } }
483
+
484
+ before { ProductsIndex::Product.import!(data.map { |h| double(h) }) }
485
+
486
+ specify { expect(subject.exists?).to eq true }
487
+ specify { expect(subject.limit(5).exists?).to eq true }
488
+ specify { expect(subject.filter(range: { age: { gt: 20 } }).limit(3).exists?).to eq true }
489
+ specify { expect(subject.filter(range: { age: { lt: 0 } }).exists?).to eq false }
490
+ end
491
+
492
+ describe '#unlimited' do
493
+ let(:data_length) { 10 }
494
+ let(:data) { Array.new(data_length) { |i| { id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next } } }
495
+
496
+ before { ProductsIndex::Product.import!(data.map { |h| double(h) }) }
497
+
498
+ specify { expect(subject.unlimited.count).to eq data_length }
499
+ specify { expect(subject.offset(5).unlimited.count).to eq data_length }
500
+ specify { expect(subject.limit(1).unlimited.count).to eq data_length }
501
+ specify { expect(subject.unlimited.limit(1).count).to eq 1 }
502
+ end
503
+
447
504
  describe '#none' do
448
505
  specify { expect(subject.none).to be_a described_class }
449
506
  specify { expect(subject.none).not_to eq(subject) }
@@ -473,25 +530,29 @@ describe Chewy::Query do
473
530
  end
474
531
 
475
532
  describe '#filter' do
476
- specify { expect(subject.filter(term: {field: 'hello'})).to be_a described_class }
477
- specify { expect(subject.filter(term: {field: 'hello'})).not_to eq(subject) }
478
- specify { expect { subject.filter(term: {field: 'hello'}) }.not_to change { subject.criteria.filters } }
479
- specify { expect(subject.filter([{term: {field: 'hello'}}, {term: {field: 'world'}}]).criteria.filters)
480
- .to eq([{term: {field: 'hello'}}, {term: {field: 'world'}}]) }
533
+ specify { expect(subject.filter(term: { field: 'hello' })).to be_a described_class }
534
+ specify { expect(subject.filter(term: { field: 'hello' })).not_to eq(subject) }
535
+ specify { expect { subject.filter(term: { field: 'hello' }) }.not_to change { subject.criteria.filters } }
536
+ specify do
537
+ expect(subject.filter([{ term: { field: 'hello' } }, { term: { field: 'world' } }]).criteria.filters)
538
+ .to eq([{ term: { field: 'hello' } }, { term: { field: 'world' } }])
539
+ end
481
540
 
482
- specify { expect { subject.filter{ name == 'John' } }.not_to change { subject.criteria.filters } }
483
- specify { expect(subject.filter{ name == 'John' }.criteria.filters).to eq([{term: {'name' => 'John'}}]) }
541
+ specify { expect { subject.filter { name == 'John' } }.not_to change { subject.criteria.filters } }
542
+ specify { expect(subject.filter { name == 'John' }.criteria.filters).to eq([{ term: { 'name' => 'John' } }]) }
484
543
  end
485
544
 
486
545
  describe '#post_filter' do
487
- specify { expect(subject.post_filter(term: {field: 'hello'})).to be_a described_class }
488
- specify { expect(subject.post_filter(term: {field: 'hello'})).not_to eq(subject) }
489
- specify { expect { subject.post_filter(term: {field: 'hello'}) }.not_to change { subject.criteria.post_filters } }
490
- specify { expect(subject.post_filter([{term: {field: 'hello'}}, {term: {field: 'world'}}]).criteria.post_filters)
491
- .to eq([{term: {field: 'hello'}}, {term: {field: 'world'}}]) }
546
+ specify { expect(subject.post_filter(term: { field: 'hello' })).to be_a described_class }
547
+ specify { expect(subject.post_filter(term: { field: 'hello' })).not_to eq(subject) }
548
+ specify { expect { subject.post_filter(term: { field: 'hello' }) }.not_to change { subject.criteria.post_filters } }
549
+ specify do
550
+ expect(subject.post_filter([{ term: { field: 'hello' } }, { term: { field: 'world' } }]).criteria.post_filters)
551
+ .to eq([{ term: { field: 'hello' } }, { term: { field: 'world' } }])
552
+ end
492
553
 
493
- specify { expect { subject.post_filter{ name == 'John' } }.not_to change { subject.criteria.post_filters } }
494
- specify { expect(subject.post_filter{ name == 'John' }.criteria.post_filters).to eq([{term: {'name' => 'John'}}]) }
554
+ specify { expect { subject.post_filter { name == 'John' } }.not_to change { subject.criteria.post_filters } }
555
+ specify { expect(subject.post_filter { name == 'John' }.criteria.post_filters).to eq([{ term: { 'name' => 'John' } }]) }
495
556
  end
496
557
 
497
558
  describe '#order' do
@@ -501,9 +562,9 @@ describe Chewy::Query do
501
562
 
502
563
  specify { expect(subject.order(:field).criteria.sort).to eq([:field]) }
503
564
  specify { expect(subject.order([:field1, :field2]).criteria.sort).to eq([:field1, :field2]) }
504
- specify { expect(subject.order(field: :asc).criteria.sort).to eq([{field: :asc}]) }
505
- specify { expect(subject.order({field1: :asc, field2: :desc}).criteria.sort).to eq([{field1: :asc}, {field2: :desc}]) }
506
- specify { expect(subject.order({field1: {order: :asc}, field2: :desc}).order([:field3], :field4).criteria.sort).to eq([{field1: {order: :asc}}, {field2: :desc}, :field3, :field4]) }
565
+ specify { expect(subject.order(field: :asc).criteria.sort).to eq([{ field: :asc }]) }
566
+ specify { expect(subject.order(field1: :asc, field2: :desc).criteria.sort).to eq([{ field1: :asc }, { field2: :desc }]) }
567
+ specify { expect(subject.order(field1: { order: :asc }, field2: :desc).order([:field3], :field4).criteria.sort).to eq([{ field1: { order: :asc } }, { field2: :desc }, :field3, :field4]) }
507
568
  end
508
569
 
509
570
  describe '#reorder' do
@@ -521,8 +582,8 @@ describe Chewy::Query do
521
582
  specify { expect(subject.only(:field)).not_to eq(subject) }
522
583
  specify { expect { subject.only(:field) }.not_to change { subject.criteria.fields } }
523
584
 
524
- specify { expect(subject.only(:field1, :field2).criteria.fields).to match_array(['field1', 'field2']) }
525
- specify { expect(subject.only([:field1, :field2]).only(:field3).criteria.fields).to match_array(['field1', 'field2', 'field3']) }
585
+ specify { expect(subject.only(:field1, :field2).criteria.fields).to match_array(%w(field1 field2)) }
586
+ specify { expect(subject.only([:field1, :field2]).only(:field3).criteria.fields).to match_array(%w(field1 field2 field3)) }
526
587
  end
527
588
 
528
589
  describe '#only!' do
@@ -530,7 +591,7 @@ describe Chewy::Query do
530
591
  specify { expect(subject.only!(:field)).not_to eq(subject) }
531
592
  specify { expect { subject.only!(:field) }.not_to change { subject.criteria.fields } }
532
593
 
533
- specify { expect(subject.only!(:field1, :field2).criteria.fields).to match_array(['field1', 'field2']) }
594
+ specify { expect(subject.only!(:field1, :field2).criteria.fields).to match_array(%w(field1 field2)) }
534
595
  specify { expect(subject.only!([:field1, :field2]).only!(:field3).criteria.fields).to match_array(['field3']) }
535
596
  specify { expect(subject.only([:field1, :field2]).only!(:field3).criteria.fields).to match_array(['field3']) }
536
597
  end
@@ -541,8 +602,8 @@ describe Chewy::Query do
541
602
  specify { expect { subject.types(:product) }.not_to change { subject.criteria.types } }
542
603
 
543
604
  specify { expect(subject.types(:user).criteria.types).to eq(['user']) }
544
- specify { expect(subject.types(:product, :city).criteria.types).to match_array(['product', 'city']) }
545
- specify { expect(subject.types([:product, :city]).types(:country).criteria.types).to match_array(['product', 'city', 'country']) }
605
+ specify { expect(subject.types(:product, :city).criteria.types).to match_array(%w(product city)) }
606
+ specify { expect(subject.types([:product, :city]).types(:country).criteria.types).to match_array(%w(product city country)) }
546
607
  end
547
608
 
548
609
  describe '#types!' do
@@ -551,31 +612,33 @@ describe Chewy::Query do
551
612
  specify { expect { subject.types!(:product) }.not_to change { subject.criteria.types } }
552
613
 
553
614
  specify { expect(subject.types!(:user).criteria.types).to eq(['user']) }
554
- specify { expect(subject.types!(:product, :city).criteria.types).to match_array(['product', 'city']) }
615
+ specify { expect(subject.types!(:product, :city).criteria.types).to match_array(%w(product city)) }
555
616
  specify { expect(subject.types!([:product, :city]).types!(:country).criteria.types).to match_array(['country']) }
556
617
  specify { expect(subject.types([:product, :city]).types!(:country).criteria.types).to match_array(['country']) }
557
618
  end
558
619
 
559
620
  describe '#search_type' do
560
- specify { expect(subject.search_type(:count).options).to include(search_type: :count) }
621
+ specify { expect(subject.search_type(:count).criteria.search_options).to include(search_type: :count) }
561
622
  end
562
623
 
563
624
  describe '#aggregations' do
564
- specify { expect(subject.aggregations(attribute: {terms: {field: 'attribute'}})).to be_a described_class }
565
- specify { expect(subject.aggregations(attribute: {terms: {field: 'attribute'}})).not_to eq(subject) }
566
- specify { expect(subject.aggregations(attribute: {terms: {field: 'attribute'}}).criteria.request_body[:body]).to include(aggregations: {attribute: {terms: {field: 'attribute'}}}) }
625
+ specify { expect(subject.aggregations(attribute: { terms: { field: 'attribute' } })).to be_a described_class }
626
+ specify { expect(subject.aggregations(attribute: { terms: { field: 'attribute' } })).not_to eq(subject) }
627
+ specify { expect(subject.aggregations(attribute: { terms: { field: 'attribute' } }).criteria.request_body[:body]).to include(aggregations: { attribute: { terms: { field: 'attribute' } } }) }
567
628
  end
568
629
 
569
630
  describe '#merge' do
570
631
  let(:query) { described_class.new(ProductsIndex) }
571
632
 
572
- specify { expect(subject.filter { name == 'name' }.merge(query.filter { age == 42 }).criteria.filters)
573
- .to eq([{term: {'name' => 'name'}}, {term: {'age' => 42}}]) }
633
+ specify do
634
+ expect(subject.filter { name == 'name' }.merge(query.filter { age == 42 }).criteria.filters)
635
+ .to eq([{ term: { 'name' => 'name' } }, { term: { 'age' => 42 } }])
636
+ end
574
637
  end
575
638
 
576
639
  describe '#to_a', :orm do
577
640
  before { stub_model(:city) }
578
- let(:cities) { 3.times.map { |i| City.create! id: i + 1, name: "name#{i}", rating: i } }
641
+ let(:cities) { Array.new(3) { |i| City.create! id: i + 1, name: "name#{i}", rating: i } }
579
642
 
580
643
  context do
581
644
  before do
@@ -583,7 +646,7 @@ describe Chewy::Query do
583
646
  define_type :city do
584
647
  field :name
585
648
  field :rating, type: 'integer'
586
- field :nested, type: 'object', value: ->{ {name: name} }
649
+ field :nested, type: 'object', value: -> { { name: name } }
587
650
  end
588
651
  end
589
652
  end
@@ -593,17 +656,17 @@ describe Chewy::Query do
593
656
  specify { expect(CitiesIndex.order(:rating).first).to be_a CitiesIndex::City }
594
657
  specify { expect(CitiesIndex.order(:rating).first.name).to eq('name0') }
595
658
  specify { expect(CitiesIndex.order(:rating).first.rating).to eq(0) }
596
- specify { expect(CitiesIndex.order(:rating).first.nested).to eq({'name' => 'name0'}) }
659
+ specify { expect(CitiesIndex.order(:rating).first.nested).to eq('name' => 'name0') }
597
660
  specify { expect(CitiesIndex.order(:rating).first.id).to eq(cities.first.id.to_s) }
598
661
 
599
662
  specify { expect(CitiesIndex.order(:rating).only(:name).first.name).to eq('name0') }
600
663
  specify { expect(CitiesIndex.order(:rating).only(:name).first.rating).to be_nil }
601
- specify { expect(CitiesIndex.order(:rating).only(:nested).first.nested).to eq({'name' => 'name0'}) }
664
+ specify { expect(CitiesIndex.order(:rating).only(:nested).first.nested).to eq('name' => 'name0') }
602
665
 
603
666
  specify { expect(CitiesIndex.order(:rating).first._score).to be_nil }
604
667
  specify { expect(CitiesIndex.all.first._score).to be > 0 }
605
- specify { expect(CitiesIndex.query(match: {name: 'name0'}).first._score).to be > 0 }
606
- specify { expect(CitiesIndex.query(match: {name: 'name0'}).took).to be >= 0 }
668
+ specify { expect(CitiesIndex.query(match: { name: 'name0' }).first._score).to be > 0 }
669
+ specify { expect(CitiesIndex.query(match: { name: 'name0' }).took).to be >= 0 }
607
670
 
608
671
  specify { expect(CitiesIndex.order(:rating).first._explanation).to be_nil }
609
672
  specify { expect(CitiesIndex.order(:rating).explain.first._explanation).to be_present }
@@ -613,10 +676,10 @@ describe Chewy::Query do
613
676
  before do
614
677
  stub_index(:cities) do
615
678
  define_type :city do
616
- root _source: {enabled: false} do
679
+ root _source: { enabled: false } do
617
680
  field :name
618
681
  field :rating, type: 'integer'
619
- field :nested, type: 'object', value: ->{ {name: name} }
682
+ field :nested, type: 'object', value: -> { { name: name } }
620
683
  end
621
684
  end
622
685
  end