chewy 5.0.0 → 7.1.0

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