chewy 0.10.0 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (157) hide show
  1. checksums.yaml +5 -5
  2. data/.circleci/config.yml +240 -0
  3. data/.rubocop.yml +25 -25
  4. data/Appraisals +12 -10
  5. data/CHANGELOG.md +252 -263
  6. data/Gemfile +5 -1
  7. data/LICENSE.txt +1 -1
  8. data/README.md +142 -78
  9. data/chewy.gemspec +10 -12
  10. data/gemfiles/{rails.4.2.mongoid.5.1.gemfile → rails.5.2.activerecord.gemfile} +6 -4
  11. data/gemfiles/{rails.4.2.activerecord.gemfile → rails.5.2.mongoid.6.4.gemfile} +6 -4
  12. data/gemfiles/{rails.4.0.activerecord.gemfile → rails.6.0.activerecord.gemfile} +6 -3
  13. data/gemfiles/rails.6.1.activerecord.gemfile +19 -0
  14. data/gemfiles/sequel.4.45.gemfile +2 -2
  15. data/lib/chewy.rb +2 -1
  16. data/lib/chewy/backports/duplicable.rb +1 -1
  17. data/lib/chewy/config.rb +10 -39
  18. data/lib/chewy/fields/base.rb +40 -28
  19. data/lib/chewy/fields/root.rb +18 -11
  20. data/lib/chewy/index.rb +3 -1
  21. data/lib/chewy/index/actions.rb +27 -15
  22. data/lib/chewy/index/settings.rb +2 -0
  23. data/lib/chewy/index/specification.rb +12 -10
  24. data/lib/chewy/minitest/helpers.rb +6 -6
  25. data/lib/chewy/minitest/search_index_receiver.rb +17 -17
  26. data/lib/chewy/multi_search.rb +62 -0
  27. data/lib/chewy/railtie.rb +4 -4
  28. data/lib/chewy/rake_helper.rb +5 -5
  29. data/lib/chewy/rspec/update_index.rb +3 -5
  30. data/lib/chewy/search.rb +4 -11
  31. data/lib/chewy/search/loader.rb +1 -1
  32. data/lib/chewy/search/pagination/will_paginate.rb +4 -2
  33. data/lib/chewy/search/parameters.rb +24 -6
  34. data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
  35. data/lib/chewy/search/parameters/concerns/query_storage.rb +4 -3
  36. data/lib/chewy/search/parameters/indices.rb +123 -0
  37. data/lib/chewy/search/parameters/none.rb +1 -3
  38. data/lib/chewy/search/request.rb +100 -74
  39. data/lib/chewy/search/scrolling.rb +7 -6
  40. data/lib/chewy/stash.rb +30 -21
  41. data/lib/chewy/strategy/active_job.rb +1 -1
  42. data/lib/chewy/strategy/atomic.rb +1 -1
  43. data/lib/chewy/strategy/sidekiq.rb +1 -1
  44. data/lib/chewy/type.rb +5 -2
  45. data/lib/chewy/type/adapter/active_record.rb +1 -1
  46. data/lib/chewy/type/adapter/base.rb +9 -9
  47. data/lib/chewy/type/adapter/mongoid.rb +2 -4
  48. data/lib/chewy/type/adapter/orm.rb +7 -4
  49. data/lib/chewy/type/adapter/sequel.rb +5 -7
  50. data/lib/chewy/type/crutch.rb +1 -1
  51. data/lib/chewy/type/import.rb +13 -11
  52. data/lib/chewy/type/import/bulk_builder.rb +1 -1
  53. data/lib/chewy/type/import/bulk_request.rb +4 -2
  54. data/lib/chewy/type/import/journal_builder.rb +3 -3
  55. data/lib/chewy/type/import/routine.rb +3 -3
  56. data/lib/chewy/type/mapping.rb +42 -36
  57. data/lib/chewy/type/observe.rb +16 -12
  58. data/lib/chewy/type/syncer.rb +15 -14
  59. data/lib/chewy/type/witchcraft.rb +11 -7
  60. data/lib/chewy/type/wrapper.rb +14 -4
  61. data/lib/chewy/version.rb +1 -1
  62. data/lib/sequel/plugins/chewy_observe.rb +4 -19
  63. data/migration_guide.md +18 -0
  64. data/spec/chewy/config_spec.rb +16 -21
  65. data/spec/chewy/fields/base_spec.rb +70 -70
  66. data/spec/chewy/fields/root_spec.rb +56 -9
  67. data/spec/chewy/index/actions_spec.rb +63 -7
  68. data/spec/chewy/index/specification_spec.rb +25 -16
  69. data/spec/chewy/index_spec.rb +75 -45
  70. data/spec/chewy/journal_spec.rb +33 -29
  71. data/spec/chewy/minitest/search_index_receiver_spec.rb +11 -9
  72. data/spec/chewy/multi_search_spec.rb +85 -0
  73. data/spec/chewy/rake_helper_spec.rb +123 -95
  74. data/spec/chewy/rspec/update_index_spec.rb +47 -46
  75. data/spec/chewy/runtime_spec.rb +2 -2
  76. data/spec/chewy/search/pagination/kaminari_spec.rb +7 -3
  77. data/spec/chewy/search/pagination/will_paginate_spec.rb +9 -3
  78. data/spec/chewy/search/parameters/indices_spec.rb +190 -0
  79. data/spec/chewy/search/parameters/none_spec.rb +1 -1
  80. data/spec/chewy/search/parameters_spec.rb +21 -4
  81. data/spec/chewy/search/request_spec.rb +101 -70
  82. data/spec/chewy/search/response_spec.rb +27 -17
  83. data/spec/chewy/search/scrolling_spec.rb +25 -16
  84. data/spec/chewy/search_spec.rb +49 -35
  85. data/spec/chewy/stash_spec.rb +15 -13
  86. data/spec/chewy/strategy/active_job_spec.rb +15 -2
  87. data/spec/chewy/strategy/shoryuken_spec.rb +8 -2
  88. data/spec/chewy/strategy/sidekiq_spec.rb +6 -2
  89. data/spec/chewy/type/adapter/active_record_spec.rb +16 -4
  90. data/spec/chewy/type/import/bulk_builder_spec.rb +9 -94
  91. data/spec/chewy/type/import/journal_builder_spec.rb +17 -15
  92. data/spec/chewy/type/import_spec.rb +6 -0
  93. data/spec/chewy/type/mapping_spec.rb +51 -18
  94. data/spec/chewy/type/observe_spec.rb +4 -4
  95. data/spec/chewy/type/witchcraft_spec.rb +31 -0
  96. data/spec/chewy/type/wrapper_spec.rb +3 -1
  97. data/spec/chewy_spec.rb +0 -7
  98. data/spec/spec_helper.rb +5 -1
  99. data/spec/support/active_record.rb +20 -0
  100. metadata +46 -116
  101. data/.travis.yml +0 -53
  102. data/LEGACY_DSL.md +0 -497
  103. data/gemfiles/rails.4.1.activerecord.gemfile +0 -14
  104. data/gemfiles/rails.5.0.activerecord.gemfile +0 -15
  105. data/gemfiles/rails.5.0.mongoid.6.0.gemfile +0 -15
  106. data/gemfiles/rails.5.1.activerecord.gemfile +0 -15
  107. data/gemfiles/rails.5.1.mongoid.6.1.gemfile +0 -15
  108. data/lib/chewy/query.rb +0 -1098
  109. data/lib/chewy/query/compose.rb +0 -68
  110. data/lib/chewy/query/criteria.rb +0 -191
  111. data/lib/chewy/query/filters.rb +0 -227
  112. data/lib/chewy/query/loading.rb +0 -111
  113. data/lib/chewy/query/nodes/and.rb +0 -25
  114. data/lib/chewy/query/nodes/base.rb +0 -17
  115. data/lib/chewy/query/nodes/bool.rb +0 -34
  116. data/lib/chewy/query/nodes/equal.rb +0 -34
  117. data/lib/chewy/query/nodes/exists.rb +0 -20
  118. data/lib/chewy/query/nodes/expr.rb +0 -28
  119. data/lib/chewy/query/nodes/field.rb +0 -110
  120. data/lib/chewy/query/nodes/has_child.rb +0 -15
  121. data/lib/chewy/query/nodes/has_parent.rb +0 -15
  122. data/lib/chewy/query/nodes/has_relation.rb +0 -59
  123. data/lib/chewy/query/nodes/match_all.rb +0 -11
  124. data/lib/chewy/query/nodes/missing.rb +0 -20
  125. data/lib/chewy/query/nodes/not.rb +0 -25
  126. data/lib/chewy/query/nodes/or.rb +0 -25
  127. data/lib/chewy/query/nodes/prefix.rb +0 -19
  128. data/lib/chewy/query/nodes/query.rb +0 -20
  129. data/lib/chewy/query/nodes/range.rb +0 -63
  130. data/lib/chewy/query/nodes/raw.rb +0 -15
  131. data/lib/chewy/query/nodes/regexp.rb +0 -35
  132. data/lib/chewy/query/nodes/script.rb +0 -20
  133. data/lib/chewy/query/pagination.rb +0 -25
  134. data/spec/chewy/query/criteria_spec.rb +0 -700
  135. data/spec/chewy/query/filters_spec.rb +0 -201
  136. data/spec/chewy/query/loading_spec.rb +0 -124
  137. data/spec/chewy/query/nodes/and_spec.rb +0 -12
  138. data/spec/chewy/query/nodes/bool_spec.rb +0 -14
  139. data/spec/chewy/query/nodes/equal_spec.rb +0 -32
  140. data/spec/chewy/query/nodes/exists_spec.rb +0 -18
  141. data/spec/chewy/query/nodes/has_child_spec.rb +0 -59
  142. data/spec/chewy/query/nodes/has_parent_spec.rb +0 -59
  143. data/spec/chewy/query/nodes/match_all_spec.rb +0 -11
  144. data/spec/chewy/query/nodes/missing_spec.rb +0 -16
  145. data/spec/chewy/query/nodes/not_spec.rb +0 -13
  146. data/spec/chewy/query/nodes/or_spec.rb +0 -12
  147. data/spec/chewy/query/nodes/prefix_spec.rb +0 -16
  148. data/spec/chewy/query/nodes/query_spec.rb +0 -12
  149. data/spec/chewy/query/nodes/range_spec.rb +0 -32
  150. data/spec/chewy/query/nodes/raw_spec.rb +0 -11
  151. data/spec/chewy/query/nodes/regexp_spec.rb +0 -43
  152. data/spec/chewy/query/nodes/script_spec.rb +0 -15
  153. data/spec/chewy/query/pagination/kaminari_spec.rb +0 -5
  154. data/spec/chewy/query/pagination/will_paginate_spec.rb +0 -5
  155. data/spec/chewy/query/pagination_spec.rb +0 -39
  156. data/spec/chewy/query_spec.rb +0 -636
  157. data/spec/chewy/search/parameters/indices_boost_spec.rb +0 -83
@@ -1,59 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Chewy::Query::Nodes::HasParent do
4
- describe '#__render__' do
5
- def render(&block)
6
- Chewy::Query::Filters.new(&block).__render__
7
- end
8
-
9
- specify { expect(render { has_parent('child') }).to eq(has_parent: {type: 'child'}) }
10
-
11
- specify do
12
- expect(render { has_parent('child').filter(term: {name: 'name'}) })
13
- .to eq(has_parent: {type: 'child', filter: {term: {name: 'name'}}})
14
- end
15
- specify do
16
- expect(render { has_parent('child').filter { name == 'name' } })
17
- .to eq(has_parent: {type: 'child', filter: {term: {'name' => 'name'}}})
18
- end
19
- specify do
20
- expect(render { has_parent('child').filter(term: {name: 'name'}).filter { age < 42 } })
21
- .to eq(has_parent: {type: 'child', filter: {and: [{term: {name: 'name'}}, range: {'age' => {lt: 42}}]}})
22
- end
23
- specify do
24
- expect(render { has_parent('child').filter(term: {name: 'name'}).filter { age < 42 }.filter_mode(:or) })
25
- .to eq(has_parent: {type: 'child', filter: {or: [{term: {name: 'name'}}, range: {'age' => {lt: 42}}]}})
26
- end
27
-
28
- specify do
29
- expect(render { has_parent('child').query(match: {name: 'name'}) })
30
- .to eq(has_parent: {type: 'child', query: {match: {name: 'name'}}})
31
- end
32
- specify do
33
- expect(render { has_parent('child').query(match: {name: 'name'}).query(match: {surname: 'surname'}) })
34
- .to eq(has_parent: {type: 'child', query: {bool: {must: [{match: {name: 'name'}}, {match: {surname: 'surname'}}]}}})
35
- end
36
- specify do
37
- expect(render { has_parent('child').query(match: {name: 'name'}).query(match: {surname: 'surname'}).query_mode(:should) })
38
- .to eq(has_parent: {type: 'child', query: {bool: {should: [{match: {name: 'name'}}, {match: {surname: 'surname'}}]}}})
39
- end
40
-
41
- specify do
42
- expect(render { has_parent('child').filter { name == 'name' }.query(match: {name: 'name'}) })
43
- .to eq(has_parent: {type: 'child', query: {filtered: {query: {match: {name: 'name'}}, filter: {term: {'name' => 'name'}}}}})
44
- end
45
- specify do
46
- expect(render { has_parent('child').filter { name == 'name' }.query(match: {name: 'name'}).filter { age < 42 } })
47
- .to eq(has_parent: {type: 'child', query: {filtered: {query: {match: {name: 'name'}}, filter: {and: [{term: {'name' => 'name'}}, range: {'age' => {lt: 42}}]}}}})
48
- end
49
-
50
- context do
51
- let(:name) { 'Name' }
52
-
53
- specify do
54
- expect(render { has_parent('child').filter { name == o { name } } })
55
- .to eq(has_parent: {type: 'child', filter: {term: {'name' => 'Name'}}})
56
- end
57
- end
58
- end
59
- end
@@ -1,11 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Chewy::Query::Nodes::MatchAll do
4
- describe '#__render__' do
5
- def render(&block)
6
- Chewy::Query::Filters.new(&block).__render__
7
- end
8
-
9
- specify { expect(render { match_all }).to eq(match_all: {}) }
10
- end
11
- end
@@ -1,16 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Chewy::Query::Nodes::Missing do
4
- describe '#__render__' do
5
- def render(&block)
6
- Chewy::Query::Filters.new(&block).__render__
7
- end
8
-
9
- specify { expect(render { !name }).to eq(missing: {field: 'name', existence: true, null_value: false}) }
10
- specify { expect(render { !name? }).to eq(missing: {field: 'name', existence: true, null_value: true}) }
11
- specify { expect(render { name == nil }).to eq(missing: {field: 'name', existence: false, null_value: true}) } # rubocop:disable Style/NilComparison
12
- specify { expect(render { name.nil? }).to eq(missing: {field: 'name', existence: false, null_value: true}) }
13
-
14
- specify { expect(render { ~!name }).to eq(missing: {field: 'name', existence: true, null_value: false}) }
15
- end
16
- end
@@ -1,13 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Chewy::Query::Nodes::Not do
4
- describe '#__render__' do
5
- def render(&block)
6
- Chewy::Query::Filters.new(&block).__render__
7
- end
8
-
9
- # rubocop:disable Style/InverseMethods
10
- specify { expect(render { !(email == 'email') }).to eq(not: {term: {'email' => 'email'}}) }
11
- specify { expect(render { ~!(email == 'email') }).to eq(not: {filter: {term: {'email' => 'email'}}, _cache: true}) }
12
- end
13
- end
@@ -1,12 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Chewy::Query::Nodes::Or do
4
- describe '#__render__' do
5
- def render(&block)
6
- Chewy::Query::Filters.new(&block).__render__
7
- end
8
-
9
- specify { expect(render { name? | (email == 'email') }).to eq(or: [{exists: {field: 'name'}}, {term: {'email' => 'email'}}]) }
10
- specify { expect(render { ~(name? | (email == 'email')) }).to eq(or: {filters: [{exists: {field: 'name'}}, {term: {'email' => 'email'}}], _cache: true}) }
11
- end
12
- end
@@ -1,16 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Chewy::Query::Nodes::Prefix do
4
- describe '#__render__' do
5
- def render(&block)
6
- Chewy::Query::Filters.new(&block).__render__
7
- end
8
-
9
- specify { expect(render { name =~ 'nam' }).to eq(prefix: {'name' => 'nam'}) }
10
- specify { expect(render { name !~ 'nam' }).to eq(not: {prefix: {'name' => 'nam'}}) }
11
-
12
- specify { expect(render { ~name =~ 'nam' }).to eq(prefix: {'name' => 'nam', _cache: true}) }
13
- specify { expect(render { ~name !~ 'nam' }).to eq(not: {prefix: {'name' => 'nam', _cache: true}}) }
14
- specify { expect(render { name(cache: false) =~ 'nam' }).to eq(prefix: {'name' => 'nam', _cache: false}) }
15
- end
16
- end
@@ -1,12 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Chewy::Query::Nodes::Query do
4
- describe '#__render__' do
5
- def render(&block)
6
- Chewy::Query::Filters.new(&block).__render__
7
- end
8
-
9
- specify { expect(render { q(query_string: {query: 'name: hello'}) }).to eq(query: {query_string: {query: 'name: hello'}}) }
10
- specify { expect(render { ~q(query_string: {query: 'name: hello'}) }).to eq(fquery: {query: {query_string: {query: 'name: hello'}}, _cache: true}) }
11
- end
12
- end
@@ -1,32 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Chewy::Query::Nodes::Range do
4
- describe '#__render__' do
5
- def render(&block)
6
- Chewy::Query::Filters.new(&block).__render__
7
- end
8
-
9
- specify { expect(render { age > nil }).to eq(range: {'age' => {gt: nil}}) }
10
- specify { expect(render { age == (nil..nil) }).to eq(range: {'age' => {gt: nil, lt: nil}}) }
11
-
12
- specify { expect(render { age > 42 }).to eq(range: {'age' => {gt: 42}}) }
13
- specify { expect(render { age == (42..45) }).to eq(range: {'age' => {gt: 42, lt: 45}}) }
14
- specify { expect(render { age == [42..45] }).to eq(range: {'age' => {gte: 42, lte: 45}}) }
15
- specify { expect(render { (age > 42) & (age <= 45) }).to eq(range: {'age' => {gt: 42, lte: 45}}) }
16
-
17
- specify { expect(render { ~age > 42 }).to eq(range: {'age' => {gt: 42}, _cache: true}) }
18
- specify { expect(render { ~age == (42..45) }).to eq(range: {'age' => {gt: 42, lt: 45}, _cache: true}) }
19
- specify { expect(render { ~age == [42..45] }).to eq(range: {'age' => {gte: 42, lte: 45}, _cache: true}) }
20
- specify { expect(render { (age > 42) & ~(age <= 45) }).to eq(range: {'age' => {gt: 42, lte: 45}, _cache: true}) }
21
- specify { expect(render { (~age > 42) & (age <= 45) }).to eq(range: {'age' => {gt: 42, lte: 45}, _cache: true}) }
22
-
23
- specify { expect(render { age(:i) > 42 }).to eq(range: {'age' => {gt: 42}, execution: :index}) }
24
- specify { expect(render { age(:index) > 42 }).to eq(range: {'age' => {gt: 42}, execution: :index}) }
25
- specify { expect(render { age(:f) > 42 }).to eq(range: {'age' => {gt: 42}, execution: :fielddata}) }
26
- specify { expect(render { age(:fielddata) > 42 }).to eq(range: {'age' => {gt: 42}, execution: :fielddata}) }
27
- specify { expect(render { (age(:f) > 42) & (age <= 45) }).to eq(range: {'age' => {gt: 42, lte: 45}, execution: :fielddata}) }
28
-
29
- specify { expect(render { ~age(:f) > 42 }).to eq(range: {'age' => {gt: 42}, execution: :fielddata, _cache: true}) }
30
- specify { expect(render { (age(:f) > 42) & (~age <= 45) }).to eq(range: {'age' => {gt: 42, lte: 45}, execution: :fielddata, _cache: true}) }
31
- end
32
- end
@@ -1,11 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Chewy::Query::Nodes::Raw do
4
- describe '#__render__' do
5
- def render(&block)
6
- Chewy::Query::Filters.new(&block).__render__
7
- end
8
-
9
- specify { expect(render { r(term: {name: 'name'}) }).to eq(term: {name: 'name'}) }
10
- end
11
- end
@@ -1,43 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Chewy::Query::Nodes::Regexp do
4
- describe '#__render__' do
5
- def render(&block)
6
- Chewy::Query::Filters.new(&block).__render__
7
- end
8
-
9
- specify { expect(render { names.first == /nam.*/ }).to eq(regexp: {'names.first' => 'nam.*'}) }
10
- specify { expect(render { names.first =~ /nam.*/ }).to eq(regexp: {'names.first' => 'nam.*'}) }
11
- specify { expect(render { name != /nam.*/ }).to eq(not: {regexp: {'name' => 'nam.*'}}) }
12
- specify { expect(render { name !~ /nam.*/ }).to eq(not: {regexp: {'name' => 'nam.*'}}) }
13
-
14
- specify do
15
- expect(render { names.first(flags: %i[anystring intersection borogoves]) == /nam.*/ })
16
- .to eq(regexp: {'names.first' => {value: 'nam.*', flags: 'ANYSTRING|INTERSECTION'}})
17
- end
18
- specify do
19
- expect(render { names.first(:anystring, :intersection, :borogoves) == /nam.*/ })
20
- .to eq(regexp: {'names.first' => {value: 'nam.*', flags: 'ANYSTRING|INTERSECTION'}})
21
- end
22
-
23
- specify do
24
- expect(render { names.first(flags: %i[anystring intersection borogoves]) =~ /nam.*/ })
25
- .to eq(regexp: {'names.first' => {value: 'nam.*', flags: 'ANYSTRING|INTERSECTION'}})
26
- end
27
- specify do
28
- expect(render { names.first(:anystring, :intersection, :borogoves) =~ /nam.*/ })
29
- .to eq(regexp: {'names.first' => {value: 'nam.*', flags: 'ANYSTRING|INTERSECTION'}})
30
- end
31
-
32
- specify { expect(render { ~names.first == /nam.*/ }).to eq(regexp: {'names.first' => 'nam.*', _cache: true, _cache_key: 'nam.*'}) }
33
- specify { expect(render { names.first(cache: 'name') == /nam.*/ }).to eq(regexp: {'names.first' => 'nam.*', _cache: true, _cache_key: 'name'}) }
34
- specify do
35
- expect(render { ~names.first(:anystring) =~ /nam.*/ })
36
- .to eq(regexp: {'names.first' => {value: 'nam.*', flags: 'ANYSTRING'}, _cache: true, _cache_key: 'nam.*'})
37
- end
38
- specify do
39
- expect(render { names.first(:anystring, cache: 'name') =~ /nam.*/ })
40
- .to eq(regexp: {'names.first' => {value: 'nam.*', flags: 'ANYSTRING'}, _cache: true, _cache_key: 'name'})
41
- end
42
- end
43
- end
@@ -1,15 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Chewy::Query::Nodes::Script do
4
- describe '#__render__' do
5
- def render(&block)
6
- Chewy::Query::Filters.new(&block).__render__
7
- end
8
-
9
- specify { expect(render { s('var = val') }).to eq(script: {script: 'var = val'}) }
10
- specify { expect(render { s('var = val', val: 42) }).to eq(script: {script: 'var = val', params: {val: 42}}) }
11
-
12
- specify { expect(render { ~s('var = val') }).to eq(script: {script: 'var = val', _cache: true}) }
13
- specify { expect(render { ~s('var = val', val: 42) }).to eq(script: {script: 'var = val', params: {val: 42}, _cache: true}) }
14
- end
15
- end
@@ -1,5 +0,0 @@
1
- require 'chewy/search/pagination/kaminari_examples'
2
-
3
- describe Chewy::Search::Pagination::Kaminari do
4
- it_behaves_like :kaminari, Chewy::Query
5
- end
@@ -1,5 +0,0 @@
1
- require 'chewy/search/pagination/will_paginate_examples'
2
-
3
- describe Chewy::Search::Pagination::WillPaginate do
4
- it_behaves_like :will_paginate, Chewy::Query
5
- end
@@ -1,39 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Chewy::Query::Pagination do
4
- if Chewy::Runtime.version < '5.0'
5
- before { Chewy.massacre }
6
-
7
- before do
8
- stub_index(:products) do
9
- define_type(:product) do
10
- field :name
11
- field :age, type: 'integer'
12
- end
13
- end
14
- end
15
-
16
- let(:search) { Chewy::Query.new(ProductsIndex).order(:age) }
17
-
18
- specify { expect(search.total_count).to eq(0) }
19
-
20
- context do
21
- let(:data) { Array.new(10) { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
22
-
23
- before { ProductsIndex::Product.import!(data.map { |h| double(h) }) }
24
-
25
- describe '#total_count' do
26
- specify { expect(search.total_count).to eq(10) }
27
- specify { expect(search.limit(5).total_count).to eq(10) }
28
- specify { expect(search.filter(range: {age: {gt: 20}}).limit(3).total_count).to eq(8) }
29
- end
30
-
31
- describe '#load' do
32
- specify { expect(search.load.total_count).to eq(10) }
33
- specify { expect(search.limit(5).load.total_count).to eq(10) }
34
- end
35
- end
36
- else
37
- xspecify 'Skip Chewy::Query specs for 5.0'
38
- end
39
- end
@@ -1,636 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Chewy::Query do
4
- if Chewy::Runtime.version < '5.0'
5
- before { Chewy.massacre }
6
-
7
- before do
8
- stub_index(:products) do
9
- define_type :product do
10
- field :name, :age
11
- end
12
- define_type :city
13
- define_type :country
14
- end
15
- end
16
-
17
- subject { described_class.new(ProductsIndex) }
18
-
19
- context 'unexistent index' do
20
- specify { expect(subject.to_a).to eq([]) }
21
- end
22
-
23
- context 'integration' do
24
- let(:products) { Array.new(3) { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
25
- let(:cities) { Array.new(3) { |i| {id: i.next.to_s}.stringify_keys! } }
26
- let(:countries) { Array.new(3) { |i| {id: i.next.to_s}.stringify_keys! } }
27
- before do
28
- ProductsIndex::Product.import!(products.map { |h| double(h) })
29
- ProductsIndex::City.import!(cities.map { |h| double(h) })
30
- ProductsIndex::Country.import!(countries.map { |h| double(h) })
31
- end
32
-
33
- specify { expect(subject.count).to eq(9) }
34
- specify { expect(subject.first._data).to be_a Hash }
35
- specify { expect(subject.limit(6).count).to eq(6) }
36
- specify { expect(subject.offset(6).count).to eq(3) }
37
- specify { expect(subject.query(match: {name: 'name3'}).highlight(fields: {name: {}}).first.name).to eq('Name3') }
38
- specify { expect(subject.query(match: {name: 'name3'}).highlight(fields: {name: {}}).first.name_highlight).to eq('<em>Name3</em>') }
39
- specify { expect(subject.query(match: {name: 'name3'}).highlight(fields: {name: {}}).first._data['_source']['name']).to eq('Name3') }
40
- specify { expect(subject.types(:product).count).to eq(3) }
41
- specify { expect(subject.types(:product, :country).count).to eq(6) }
42
- specify { expect(subject.filter(term: {age: 10}).count).to eq(1) }
43
- specify { expect(subject.query(term: {age: 10}).count).to eq(1) }
44
- specify { expect(subject.order(nil).count).to eq(9) }
45
- specify { expect(subject.search_type(:count).count).to eq(0) }
46
- specify { expect(subject.search_type(:count).total).to eq(9) }
47
- end
48
-
49
- describe '#==' do
50
- let(:data) { Array.new(3) { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
51
- before { ProductsIndex::Product.import!(data.map { |h| double(h) }) }
52
-
53
- specify { expect(subject.query(match: 'hello')).to eq(subject.query(match: 'hello')) }
54
- specify { expect(subject.query(match: 'hello')).not_to eq(subject.query(match: 'world')) }
55
- specify { expect(subject.limit(10)).to eq(subject.limit(10)) }
56
- specify { expect(subject.limit(10)).not_to eq(subject.limit(11)) }
57
- specify { expect(subject.limit(2)).to eq(subject.limit(2).to_a) }
58
- end
59
-
60
- describe '#query_mode' do
61
- specify { expect(subject.query_mode(:should)).to be_a described_class }
62
- specify { expect(subject.query_mode(:should)).not_to eq(subject) }
63
- specify { expect(subject.query_mode(:should).criteria.options).to include(query_mode: :should) }
64
- specify { expect { subject.query_mode(:should) }.not_to change { subject.criteria.options } }
65
- end
66
-
67
- describe '#filter_mode' do
68
- specify { expect(subject.filter_mode(:or)).to be_a described_class }
69
- specify { expect(subject.filter_mode(:or)).not_to eq(subject) }
70
- specify { expect(subject.filter_mode(:or).criteria.options).to include(filter_mode: :or) }
71
- specify { expect { subject.filter_mode(:or) }.not_to change { subject.criteria.options } }
72
- end
73
-
74
- describe '#post_filter_mode' do
75
- specify { expect(subject.post_filter_mode(:or)).to be_a described_class }
76
- specify { expect(subject.post_filter_mode(:or)).not_to eq(subject) }
77
- specify { expect(subject.post_filter_mode(:or).criteria.options).to include(post_filter_mode: :or) }
78
- specify { expect { subject.post_filter_mode(:or) }.not_to change { subject.criteria.options } }
79
- end
80
-
81
- describe '#boost_mode' do
82
- specify { expect(subject.boost_mode(:replace)).to be_a described_class }
83
- specify { expect(subject.boost_mode(:replace)).not_to eq(subject) }
84
- specify { expect(subject.boost_mode(:replace).criteria.options).to include(boost_mode: :replace) }
85
- specify { expect { subject.boost_mode(:replace) }.not_to change { subject.criteria.options } }
86
- end
87
-
88
- describe '#score_mode' do
89
- specify { expect(subject.score_mode(:first)).to be_a described_class }
90
- specify { expect(subject.score_mode(:first)).not_to eq(subject) }
91
- specify { expect(subject.score_mode(:first).criteria.options).to include(score_mode: :first) }
92
- specify { expect { subject.score_mode(:first) }.not_to change { subject.criteria.options } }
93
- end
94
-
95
- describe '#limit' do
96
- specify { expect(subject.limit(10)).to be_a described_class }
97
- specify { expect(subject.limit(10)).not_to eq(subject) }
98
- specify { expect(subject.limit(10).criteria.request_options).to include(size: 10) }
99
- specify { expect { subject.limit(10) }.not_to change { subject.criteria.request_options } }
100
- specify { expect(subject.limit { 20 / 2 }.criteria.request_body[:body]).to include(size: 10) }
101
- end
102
-
103
- describe '#offset' do
104
- specify { expect(subject.offset(10)).to be_a described_class }
105
- specify { expect(subject.offset(10)).not_to eq(subject) }
106
- specify { expect(subject.offset(10).criteria.request_options).to include(from: 10) }
107
- specify { expect { subject.offset(10) }.not_to change { subject.criteria.request_options } }
108
- specify { expect(subject.offset { 20 / 2 }.criteria.request_body[:body]).to include(from: 10) }
109
- end
110
-
111
- describe '#track_scores' do
112
- specify { expect(subject.track_scores(true)).to be_a described_class }
113
- specify { expect(subject.track_scores(true)).not_to eq(subject) }
114
- specify { expect(subject.track_scores(true).criteria.request_options).to include(track_scores: true) }
115
- specify { expect { subject.track_scores(true) }.not_to change { subject.criteria.request_options } }
116
- specify { expect(subject.track_scores(false).criteria.request_body[:body]).to include(track_scores: false) }
117
- end
118
-
119
- describe '#script_fields' do
120
- specify { expect(subject.script_fields(distance: 'test()')).to be_a described_class }
121
- specify { expect(subject.script_fields(distance: 'test()')).not_to eq(subject) }
122
- specify { expect(subject.script_fields(distance: 'test()').criteria.script_fields).to include(distance: 'test()') }
123
- specify { expect { subject.script_fields(distance: 'test()') }.not_to change { subject.criteria.script_fields } }
124
- end
125
-
126
- describe '#script_score' do
127
- specify { expect(subject.script_score('23')).to be_a described_class }
128
- specify { expect(subject.script_score('23')).not_to eq(subject) }
129
- specify { expect(subject.script_score('23').criteria.scores).to eq([{script_score: {script: '23'}}]) }
130
- specify { expect { subject.script_score('23') }.not_to change { subject.criteria.scores } }
131
- specify { expect(subject.script_score('23 * factor', params: {factor: 0.5}).criteria.scores).to eq([{script_score: {script: '23 * factor', params: {factor: 0.5}}}]) }
132
- end
133
-
134
- describe '#boost_factor' do
135
- specify { expect(subject.boost_factor('23')).to be_a described_class }
136
- specify { expect(subject.boost_factor('23')).not_to eq(subject) }
137
- specify { expect(subject.boost_factor('23').criteria.scores).to eq([{boost_factor: 23}]) }
138
- specify { expect { subject.boost_factor('23') }.not_to change { subject.criteria.scores } }
139
- specify { expect(subject.boost_factor('23', filter: {foo: :bar}).criteria.scores).to eq([{boost_factor: 23, filter: {foo: :bar}}]) }
140
- end
141
-
142
- describe '#weight' do
143
- specify { expect(subject.weight('23')).to be_a described_class }
144
- specify { expect(subject.weight('23')).not_to eq(subject) }
145
- specify { expect(subject.weight('23').criteria.scores).to eq([{weight: 23}]) }
146
- specify { expect { subject.weight('23') }.not_to change { subject.criteria.scores } }
147
- specify { expect(subject.weight('23', filter: {foo: :bar}).criteria.scores).to eq([{weight: 23, filter: {foo: :bar}}]) }
148
- end
149
-
150
- describe '#random_score' do
151
- specify { expect(subject.random_score('23')).to be_a described_class }
152
- specify { expect(subject.random_score('23')).not_to eq(subject) }
153
- specify { expect(subject.random_score('23').criteria.scores).to eq([{random_score: {seed: 23}}]) }
154
- specify { expect { subject.random_score('23') }.not_to change { subject.criteria.scores } }
155
- specify { expect(subject.random_score('23', filter: {foo: :bar}).criteria.scores).to eq([{random_score: {seed: 23}, filter: {foo: :bar}}]) }
156
- end
157
-
158
- describe '#field_value_score' do
159
- specify { expect(subject.field_value_factor(field: :boost)).to be_a described_class }
160
- specify { expect(subject.field_value_factor(field: :boost)).not_to eq(subject) }
161
- specify { expect(subject.field_value_factor(field: :boost).criteria.scores).to eq([{field_value_factor: {field: :boost}}]) }
162
- specify { expect { subject.field_value_factor(field: :boost) }.not_to change { subject.criteria.scores } }
163
- specify { expect(subject.field_value_factor({field: :boost}, filter: {foo: :bar}).criteria.scores).to eq([{field_value_factor: {field: :boost}, filter: {foo: :bar}}]) }
164
- end
165
-
166
- describe '#decay' do
167
- specify { expect(subject.decay(:gauss, :field)).to be_a described_class }
168
- specify { expect(subject.decay(:gauss, :field)).not_to eq(subject) }
169
- specify do
170
- expect(subject.decay(:gauss, :field).criteria.scores).to eq([{
171
- gauss: {
172
- field: {}
173
- }
174
- }])
175
- end
176
- specify { expect { subject.decay(:gauss, :field) }.not_to change { subject.criteria.scores } }
177
- specify do
178
- expect(subject.decay(:gauss, :field,
179
- origin: '11, 12',
180
- scale: '2km',
181
- offset: '5km',
182
- decay: 0.4,
183
- filter: {foo: :bar}).criteria.scores).to eq([
184
- {
185
- gauss: {
186
- field: {
187
- origin: '11, 12',
188
- scale: '2km',
189
- offset: '5km',
190
- decay: 0.4
191
- }
192
- },
193
- filter: {foo: :bar}
194
- }
195
- ])
196
- end
197
- end
198
-
199
- describe '#preference' do
200
- specify { expect(subject.preference(:_primary)).to be_a described_class }
201
- specify { expect(subject.preference(:_primary)).not_to eq(subject) }
202
- specify { expect(subject.preference(:_primary).criteria.search_options).to include(preference: :_primary) }
203
- specify { expect { subject.preference(:_primary) }.not_to change { subject.criteria.search_options } }
204
- specify { expect(subject.preference(:_primary).criteria.request_body).to include(preference: :_primary) }
205
- end
206
-
207
- describe '#aggregations' do
208
- specify { expect(subject.aggregations(aggregation1: {field: 'hello'})).to be_a described_class }
209
- specify { expect(subject.aggregations(aggregation1: {field: 'hello'})).not_to eq(subject) }
210
- specify { expect(subject.aggregations(aggregation1: {field: 'hello'}).criteria.aggregations).to include(aggregation1: {field: 'hello'}) }
211
- specify { expect { subject.aggregations(aggregation1: {field: 'hello'}) }.not_to change { subject.criteria.aggregations } }
212
-
213
- context 'when requesting a named aggregation' do
214
- before do
215
- stub_index(:products) do
216
- define_type :product do
217
- root do
218
- field :name, 'surname'
219
- field :title, type: 'string' do
220
- field :subfield1
221
- end
222
- field 'price', type: 'float' do
223
- field :subfield2
224
- end
225
- agg :uniquely_named_agg do
226
- {min: {field: 'title.subfield1'}}
227
- end
228
- end
229
- end
230
- end
231
- end
232
- specify { expect(subject.aggregations(:uniquely_named_agg).criteria.aggregations).to include(uniquely_named_agg: {min: {field: 'title.subfield1'}}) }
233
-
234
- context 'when more than one aggregation of the same name exists' do
235
- before do
236
- stub_index(:products) do
237
- define_type :product do
238
- root do
239
- field :name, 'surname'
240
- field :title, type: 'string' do
241
- field :subfield1
242
- end
243
- field 'price', type: 'float' do
244
- field :subfield2
245
- end
246
- agg :uniquely_named_agg do
247
- {min: {field: 'title.subfield1'}}
248
- end
249
- agg :named_agg do
250
- {avg: {field: 'title.subfield1'}}
251
- end
252
- end
253
- end
254
- define_type :review do
255
- field :title, :body
256
- field :comments do
257
- field :message
258
- field :rating, type: 'long'
259
- end
260
- agg :named_agg do
261
- {avg: {field: 'comments.rating'}}
262
- end
263
- end
264
- end
265
- end
266
-
267
- it 'is the aggregation definition that was last defined' do
268
- expect(subject.aggregations(:named_agg).criteria.aggregations).to include(named_agg: {avg: {field: 'comments.rating'}})
269
- end
270
-
271
- context 'when the fully qualified aggregation name is provided' do
272
- specify do
273
- expect(subject
274
- .aggregations('products#product.named_agg')
275
- .criteria
276
- .aggregations)
277
- .to include('products#product.named_agg' => {avg: {field: 'title.subfield1'}})
278
- end
279
- end
280
- end
281
- end
282
-
283
- context 'results', :orm do
284
- before { stub_model(:city) }
285
- let(:cities) { Array.new(10) { |i| City.create! id: i + 1, name: "name#{i}", rating: i % 3 } }
286
-
287
- context do
288
- before do
289
- stub_index(:cities) do
290
- define_type :city do
291
- field :rating, type: 'integer'
292
- end
293
- end
294
- end
295
-
296
- before { CitiesIndex::City.import! cities }
297
- subject { described_class.new(CitiesIndex) }
298
-
299
- specify { expect(subject.aggregations).to eq({}) }
300
- specify do
301
- expect(subject.aggregations(ratings: {terms: {field: 'rating'}})
302
- .aggregations['ratings']['buckets'].map { |h| h.slice('key', 'doc_count') }).to eq([
303
- {'key' => 0, 'doc_count' => 4},
304
- {'key' => 1, 'doc_count' => 3},
305
- {'key' => 2, 'doc_count' => 3}
306
- ])
307
- end
308
- end
309
- end
310
- end
311
-
312
- describe '#suggest' do
313
- specify { subject.suggest(name1: {text: 'hello', term: {field: 'name'}}) }
314
- specify { expect(subject.suggest(name1: {text: 'hello'})).not_to eq(subject) }
315
- specify { expect(subject.suggest(name1: {text: 'hello'}).criteria.suggest).to include(name1: {text: 'hello'}) }
316
- specify { expect { subject.suggest(name1: {text: 'hello'}) }.not_to change { subject.criteria.suggest } }
317
-
318
- context 'results', :orm do
319
- before { stub_model(:city) }
320
- let(:cities) { Array.new(10) { |i| City.create! id: i + 1, name: "name#{i}" } }
321
-
322
- context do
323
- before do
324
- stub_index(:cities) do
325
- define_type :city do
326
- field :name
327
- end
328
- end
329
- end
330
-
331
- before { CitiesIndex::City.import! cities }
332
- subject { described_class.new(CitiesIndex) }
333
-
334
- specify { expect(subject.suggest).to eq({}) }
335
- specify do
336
- expect(subject.suggest(name: {text: 'name', term: {field: 'name'}}).suggest).to eq('name' => [
337
- {'text' => 'name', 'offset' => 0, 'length' => 4, 'options' => [
338
- {'text' => 'name0', 'score' => 0.75, 'freq' => 1},
339
- {'text' => 'name1', 'score' => 0.75, 'freq' => 1},
340
- {'text' => 'name2', 'score' => 0.75, 'freq' => 1},
341
- {'text' => 'name3', 'score' => 0.75, 'freq' => 1},
342
- {'text' => 'name4', 'score' => 0.75, 'freq' => 1}
343
- ]}
344
- ])
345
- end
346
- end
347
- end
348
- end
349
-
350
- describe '#delete_all' do
351
- let(:products) { Array.new(3) { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
352
- let(:cities) { Array.new(3) { |i| {id: i.next.to_s}.stringify_keys! } }
353
- let(:countries) { Array.new(3) { |i| {id: i.next.to_s}.stringify_keys! } }
354
-
355
- before do
356
- ProductsIndex::Product.import!(products.map { |h| double(h) })
357
- ProductsIndex::City.import!(cities.map { |h| double(h) })
358
- ProductsIndex::Country.import!(countries.map { |h| double(h) })
359
- end
360
-
361
- specify do
362
- expect do
363
- subject.query(match: {name: 'name3'}).delete_all
364
- Chewy.client.indices.refresh(index: 'products')
365
- end.to change { described_class.new(ProductsIndex).total }.from(9).to(8)
366
- end
367
- specify do
368
- expect do
369
- subject.filter { age == [10, 20] }.delete_all
370
- Chewy.client.indices.refresh(index: 'products')
371
- end.to change { described_class.new(ProductsIndex).total_count }.from(9).to(7)
372
- end
373
- specify do
374
- expect do
375
- subject.types(:product).delete_all
376
- Chewy.client.indices.refresh(index: 'products')
377
- end.to change { described_class.new(ProductsIndex::Product).total_entries }.from(3).to(0)
378
- end
379
- specify do
380
- expect do
381
- subject.delete_all
382
- Chewy.client.indices.refresh(index: 'products')
383
- end.to change { described_class.new(ProductsIndex).total }.from(9).to(0)
384
- end
385
- specify do
386
- expect do
387
- described_class.new(ProductsIndex::City).delete_all
388
- Chewy.client.indices.refresh(index: 'products')
389
- end.to change { described_class.new(ProductsIndex).total }.from(9).to(6)
390
- end
391
- end
392
-
393
- describe '#find' do
394
- let(:products) { Array.new(3) { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next}.stringify_keys! } }
395
- let(:cities) { Array.new(1) { {id: '4'}.stringify_keys! } }
396
- let(:countries) { Array.new(1) { {id: '4'}.stringify_keys! } }
397
-
398
- before do
399
- ProductsIndex::Product.import!(products.map { |h| double(h) })
400
- ProductsIndex::City.import!(cities.map { |h| double(h) })
401
- ProductsIndex::Country.import!(countries.map { |h| double(h) })
402
- end
403
-
404
- specify { expect(subject.find(1)).to be_a(ProductsIndex::Product) }
405
- specify { expect(subject.find(1).id).to eq('1') }
406
- specify { expect(subject.find(4).id).to eq('4') }
407
- specify { expect(subject.find([1]).map(&:id)).to match_array(%w[1]) }
408
- specify { expect(subject.find([4]).map(&:id)).to match_array(%w[4 4]) }
409
- specify { expect(subject.find([1, 3]).map(&:id)).to match_array(%w[1 3]) }
410
- specify { expect(subject.find(1, 3).map(&:id)).to match_array(%w[1 3]) }
411
- specify { expect(subject.find(1, 10).map(&:id)).to match_array(%w[1]) }
412
-
413
- specify { expect { subject.find(10) }.to raise_error Chewy::DocumentNotFound }
414
- specify { expect { subject.find([10]) }.to raise_error Chewy::DocumentNotFound }
415
- specify { expect { subject.find([10, 20]) }.to raise_error Chewy::DocumentNotFound }
416
- end
417
-
418
- describe '#exists?' do
419
- let(:data) { Array.new(10) { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next} } }
420
-
421
- before { ProductsIndex::Product.import!(data.map { |h| double(h) }) }
422
-
423
- specify { expect(subject.exists?).to eq true }
424
- specify { expect(subject.limit(5).exists?).to eq true }
425
- specify { expect(subject.filter(range: {age: {gt: 20}}).limit(3).exists?).to eq true }
426
- specify { expect(subject.filter(range: {age: {lt: 0}}).exists?).to eq false }
427
- end
428
-
429
- describe '#unlimited' do
430
- let(:data_length) { 10 }
431
- let(:data) { Array.new(data_length) { |i| {id: i.next.to_s, name: "Name#{i.next}", age: 10 * i.next} } }
432
-
433
- before { ProductsIndex::Product.import!(data.map { |h| double(h) }) }
434
-
435
- specify { expect(subject.unlimited.count).to eq data_length }
436
- specify { expect(subject.offset(5).unlimited.count).to eq data_length }
437
- specify { expect(subject.limit(1).unlimited.count).to eq data_length }
438
- specify { expect(subject.unlimited.limit(1).count).to eq 1 }
439
- end
440
-
441
- describe '#none' do
442
- specify { expect(subject.none).to be_a described_class }
443
- specify { expect(subject.none).not_to eq(subject) }
444
- specify { expect(subject.none.criteria).to be_none }
445
-
446
- context do
447
- before { expect_any_instance_of(described_class).not_to receive(:_response) }
448
-
449
- specify { expect(subject.none.to_a).to eq([]) }
450
- specify { expect(subject.query(match: 'hello').none.to_a).to eq([]) }
451
- specify { expect(subject.none.query(match: 'hello').to_a).to eq([]) }
452
- end
453
- end
454
-
455
- describe '#strategy' do
456
- specify { expect(subject.strategy('query_first')).to be_a described_class }
457
- specify { expect(subject.strategy('query_first')).not_to eq(subject) }
458
- specify { expect(subject.strategy('query_first').criteria.options).to include(strategy: 'query_first') }
459
- specify { expect { subject.strategy('query_first') }.not_to change { subject.criteria.options } }
460
- end
461
-
462
- describe '#query' do
463
- specify { expect(subject.query(match: 'hello')).to be_a described_class }
464
- specify { expect(subject.query(match: 'hello')).not_to eq(subject) }
465
- specify { expect(subject.query(match: 'hello').criteria.queries).to include(match: 'hello') }
466
- specify { expect { subject.query(match: 'hello') }.not_to change { subject.criteria.queries } }
467
- end
468
-
469
- describe '#filter' do
470
- specify { expect(subject.filter(term: {field: 'hello'})).to be_a described_class }
471
- specify { expect(subject.filter(term: {field: 'hello'})).not_to eq(subject) }
472
- specify { expect { subject.filter(term: {field: 'hello'}) }.not_to change { subject.criteria.filters } }
473
- specify do
474
- expect(subject.filter([{term: {field: 'hello'}}, {term: {field: 'world'}}]).criteria.filters)
475
- .to eq([{term: {field: 'hello'}}, {term: {field: 'world'}}])
476
- end
477
-
478
- specify { expect { subject.filter { name == 'John' } }.not_to change { subject.criteria.filters } }
479
- specify { expect(subject.filter { name == 'John' }.criteria.filters).to eq([{term: {'name' => 'John'}}]) }
480
- end
481
-
482
- describe '#post_filter' do
483
- specify { expect(subject.post_filter(term: {field: 'hello'})).to be_a described_class }
484
- specify { expect(subject.post_filter(term: {field: 'hello'})).not_to eq(subject) }
485
- specify { expect { subject.post_filter(term: {field: 'hello'}) }.not_to change { subject.criteria.post_filters } }
486
- specify do
487
- expect(subject.post_filter([{term: {field: 'hello'}}, {term: {field: 'world'}}]).criteria.post_filters)
488
- .to eq([{term: {field: 'hello'}}, {term: {field: 'world'}}])
489
- end
490
-
491
- specify { expect { subject.post_filter { name == 'John' } }.not_to change { subject.criteria.post_filters } }
492
- specify { expect(subject.post_filter { name == 'John' }.criteria.post_filters).to eq([{term: {'name' => 'John'}}]) }
493
- end
494
-
495
- describe '#order' do
496
- specify { expect(subject.order(field: 'hello')).to be_a described_class }
497
- specify { expect(subject.order(field: 'hello')).not_to eq(subject) }
498
- specify { expect { subject.order(field: 'hello') }.not_to change { subject.criteria.sort } }
499
-
500
- specify { expect(subject.order(:field).criteria.sort).to eq([:field]) }
501
- specify { expect(subject.order(%i[field1 field2]).criteria.sort).to eq(%i[field1 field2]) }
502
- specify { expect(subject.order(field: :asc).criteria.sort).to eq([{field: :asc}]) }
503
- specify { expect(subject.order(field1: :asc, field2: :desc).criteria.sort).to eq([{field1: :asc}, {field2: :desc}]) }
504
- specify { expect(subject.order(field1: {order: :asc}, field2: :desc).order([:field3], :field4).criteria.sort).to eq([{field1: {order: :asc}}, {field2: :desc}, :field3, :field4]) }
505
- end
506
-
507
- describe '#reorder' do
508
- specify { expect(subject.reorder(field: 'hello')).to be_a described_class }
509
- specify { expect(subject.reorder(field: 'hello')).not_to eq(subject) }
510
- specify { expect { subject.reorder(field: 'hello') }.not_to change { subject.criteria.sort } }
511
-
512
- specify { expect(subject.order(:field1).reorder(:field2).criteria.sort).to eq([:field2]) }
513
- specify { expect(subject.order(:field1).reorder(:field2).order(:field3).criteria.sort).to eq(%i[field2 field3]) }
514
- specify { expect(subject.order(:field1).reorder(:field2).reorder(:field3).criteria.sort).to eq([:field3]) }
515
- end
516
-
517
- describe '#only' do
518
- specify { expect(subject.only(:field)).to be_a described_class }
519
- specify { expect(subject.only(:field)).not_to eq(subject) }
520
- specify { expect { subject.only(:field) }.not_to change { subject.criteria.fields } }
521
-
522
- specify { expect(subject.only(:field1, :field2).criteria.fields).to match_array(%w[field1 field2]) }
523
- specify { expect(subject.only(%i[field1 field2]).only(:field3).criteria.fields).to match_array(%w[field1 field2 field3]) }
524
- end
525
-
526
- describe '#only!' do
527
- specify { expect(subject.only!(:field)).to be_a described_class }
528
- specify { expect(subject.only!(:field)).not_to eq(subject) }
529
- specify { expect { subject.only!(:field) }.not_to change { subject.criteria.fields } }
530
-
531
- specify { expect(subject.only!(:field1, :field2).criteria.fields).to match_array(%w[field1 field2]) }
532
- specify { expect(subject.only!(%i[field1 field2]).only!(:field3).criteria.fields).to match_array(['field3']) }
533
- specify { expect(subject.only(%i[field1 field2]).only!(:field3).criteria.fields).to match_array(['field3']) }
534
- end
535
-
536
- describe '#types' do
537
- specify { expect(subject.types(:product)).to be_a described_class }
538
- specify { expect(subject.types(:product)).not_to eq(subject) }
539
- specify { expect { subject.types(:product) }.not_to change { subject.criteria.types } }
540
-
541
- specify { expect(subject.types(:user).criteria.types).to eq(['user']) }
542
- specify { expect(subject.types(:product, :city).criteria.types).to match_array(%w[product city]) }
543
- specify { expect(subject.types(%i[product city]).types(:country).criteria.types).to match_array(%w[product city country]) }
544
- end
545
-
546
- describe '#types!' do
547
- specify { expect(subject.types!(:product)).to be_a described_class }
548
- specify { expect(subject.types!(:product)).not_to eq(subject) }
549
- specify { expect { subject.types!(:product) }.not_to change { subject.criteria.types } }
550
-
551
- specify { expect(subject.types!(:user).criteria.types).to eq(['user']) }
552
- specify { expect(subject.types!(:product, :city).criteria.types).to match_array(%w[product city]) }
553
- specify { expect(subject.types!(%i[product city]).types!(:country).criteria.types).to match_array(['country']) }
554
- specify { expect(subject.types(%i[product city]).types!(:country).criteria.types).to match_array(['country']) }
555
- end
556
-
557
- describe '#search_type' do
558
- specify { expect(subject.search_type(:count).criteria.search_options).to include(search_type: :count) }
559
- end
560
-
561
- describe '#aggregations' do
562
- specify { expect(subject.aggregations(attribute: {terms: {field: 'attribute'}})).to be_a described_class }
563
- specify { expect(subject.aggregations(attribute: {terms: {field: 'attribute'}})).not_to eq(subject) }
564
- specify { expect(subject.aggregations(attribute: {terms: {field: 'attribute'}}).criteria.request_body[:body]).to include(aggregations: {attribute: {terms: {field: 'attribute'}}}) }
565
- end
566
-
567
- describe '#merge' do
568
- let(:query) { described_class.new(ProductsIndex) }
569
-
570
- specify do
571
- expect(subject.filter { name == 'name' }.merge(query.filter { age == 42 }).criteria.filters)
572
- .to eq([{term: {'name' => 'name'}}, {term: {'age' => 42}}])
573
- end
574
- end
575
-
576
- describe '#to_a', :orm do
577
- before { stub_model(:city) }
578
- let(:cities) { Array.new(3) { |i| City.create! id: i + 1, name: "name#{i}", rating: i } }
579
- subject { described_class.new(CitiesIndex) }
580
-
581
- context do
582
- before do
583
- stub_index(:cities) do
584
- define_type :city do
585
- field :name
586
- field :rating, type: 'integer'
587
- field :nested, type: 'object', value: -> { {name: name} }
588
- end
589
- end
590
- end
591
-
592
- before { CitiesIndex::City.import! cities }
593
-
594
- specify { expect(subject.order(:rating).first).to be_a CitiesIndex::City }
595
- specify { expect(subject.order(:rating).first.name).to eq('name0') }
596
- specify { expect(subject.order(:rating).first.rating).to eq(0) }
597
- specify { expect(subject.order(:rating).first.nested).to eq('name' => 'name0') }
598
- specify { expect(subject.order(:rating).first.id).to eq(cities.first.id.to_s) }
599
-
600
- specify { expect(subject.order(:rating).only(:name).first.name).to eq('name0') }
601
- specify { expect(subject.order(:rating).only(:name).first.rating).to be_nil }
602
- specify { expect(subject.order(:rating).only(:nested).first.nested).to eq('name' => 'name0') }
603
-
604
- specify { expect(subject.order(:rating).first._score).to be_nil }
605
- specify { expect(subject.first._score).to be > 0 }
606
- specify { expect(subject.query(match: {name: 'name0'}).first._score).to be > 0 }
607
- specify { expect(subject.query(match: {name: 'name0'}).took).to be >= 0 }
608
-
609
- specify { expect(subject.order(:rating).first._explanation).to be_nil }
610
- specify { expect(subject.order(:rating).explain.first._explanation).to be_present }
611
- end
612
-
613
- context 'sourceless' do
614
- before do
615
- stub_index(:cities) do
616
- define_type :city do
617
- root _source: {enabled: false} do
618
- field :name
619
- field :rating, type: 'integer'
620
- field :nested, type: 'object', value: -> { {name: name} }
621
- end
622
- end
623
- end
624
- end
625
- before { CitiesIndex::City.import! cities }
626
-
627
- specify { expect(subject.order(:rating).first).to be_a CitiesIndex::City }
628
- specify { expect(subject.order(:rating).first.name).to be_nil }
629
- specify { expect(subject.order(:rating).first.rating).to be_nil }
630
- specify { expect(subject.order(:rating).first.nested).to be_nil }
631
- end
632
- end
633
- else
634
- xspecify 'Skip Chewy::Query specs for 5.0'
635
- end
636
- end