chewy 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. checksums.yaml +13 -5
  2. data/.gitignore +1 -0
  3. data/.travis.yml +5 -3
  4. data/CHANGELOG.md +75 -0
  5. data/README.md +487 -92
  6. data/Rakefile +3 -2
  7. data/chewy.gemspec +2 -2
  8. data/filters +76 -0
  9. data/lib/chewy.rb +5 -3
  10. data/lib/chewy/config.rb +36 -19
  11. data/lib/chewy/fields/base.rb +5 -1
  12. data/lib/chewy/index.rb +22 -10
  13. data/lib/chewy/index/actions.rb +13 -13
  14. data/lib/chewy/index/search.rb +7 -2
  15. data/lib/chewy/query.rb +382 -64
  16. data/lib/chewy/query/context.rb +174 -0
  17. data/lib/chewy/query/criteria.rb +127 -34
  18. data/lib/chewy/query/loading.rb +9 -9
  19. data/lib/chewy/query/nodes/and.rb +25 -0
  20. data/lib/chewy/query/nodes/base.rb +17 -0
  21. data/lib/chewy/query/nodes/bool.rb +32 -0
  22. data/lib/chewy/query/nodes/equal.rb +34 -0
  23. data/lib/chewy/query/nodes/exists.rb +20 -0
  24. data/lib/chewy/query/nodes/expr.rb +28 -0
  25. data/lib/chewy/query/nodes/field.rb +106 -0
  26. data/lib/chewy/query/nodes/missing.rb +20 -0
  27. data/lib/chewy/query/nodes/not.rb +25 -0
  28. data/lib/chewy/query/nodes/or.rb +25 -0
  29. data/lib/chewy/query/nodes/prefix.rb +18 -0
  30. data/lib/chewy/query/nodes/query.rb +20 -0
  31. data/lib/chewy/query/nodes/range.rb +63 -0
  32. data/lib/chewy/query/nodes/raw.rb +15 -0
  33. data/lib/chewy/query/nodes/regexp.rb +31 -0
  34. data/lib/chewy/query/nodes/script.rb +20 -0
  35. data/lib/chewy/query/pagination.rb +28 -22
  36. data/lib/chewy/railtie.rb +23 -0
  37. data/lib/chewy/rspec/update_index.rb +20 -3
  38. data/lib/chewy/type/adapter/active_record.rb +78 -5
  39. data/lib/chewy/type/adapter/base.rb +46 -0
  40. data/lib/chewy/type/adapter/object.rb +40 -8
  41. data/lib/chewy/type/base.rb +1 -1
  42. data/lib/chewy/type/import.rb +18 -44
  43. data/lib/chewy/type/observe.rb +24 -14
  44. data/lib/chewy/version.rb +1 -1
  45. data/lib/tasks/chewy.rake +27 -0
  46. data/spec/chewy/config_spec.rb +30 -12
  47. data/spec/chewy/fields/base_spec.rb +11 -5
  48. data/spec/chewy/index/actions_spec.rb +20 -20
  49. data/spec/chewy/index/search_spec.rb +5 -5
  50. data/spec/chewy/index_spec.rb +28 -8
  51. data/spec/chewy/query/context_spec.rb +173 -0
  52. data/spec/chewy/query/criteria_spec.rb +219 -12
  53. data/spec/chewy/query/loading_spec.rb +6 -4
  54. data/spec/chewy/query/nodes/and_spec.rb +16 -0
  55. data/spec/chewy/query/nodes/bool_spec.rb +22 -0
  56. data/spec/chewy/query/nodes/equal_spec.rb +32 -0
  57. data/spec/chewy/query/nodes/exists_spec.rb +18 -0
  58. data/spec/chewy/query/nodes/missing_spec.rb +15 -0
  59. data/spec/chewy/query/nodes/not_spec.rb +16 -0
  60. data/spec/chewy/query/nodes/or_spec.rb +16 -0
  61. data/spec/chewy/query/nodes/prefix_spec.rb +16 -0
  62. data/spec/chewy/query/nodes/query_spec.rb +12 -0
  63. data/spec/chewy/query/nodes/range_spec.rb +32 -0
  64. data/spec/chewy/query/nodes/raw_spec.rb +11 -0
  65. data/spec/chewy/query/nodes/regexp_spec.rb +31 -0
  66. data/spec/chewy/query/nodes/script_spec.rb +15 -0
  67. data/spec/chewy/query/pagination_spec.rb +3 -2
  68. data/spec/chewy/query_spec.rb +83 -26
  69. data/spec/chewy/rspec/update_index_spec.rb +20 -0
  70. data/spec/chewy/type/adapter/active_record_spec.rb +102 -0
  71. data/spec/chewy/type/adapter/object_spec.rb +82 -0
  72. data/spec/chewy/type/import_spec.rb +30 -1
  73. data/spec/chewy/type/mapping_spec.rb +1 -1
  74. data/spec/chewy/type/observe_spec.rb +46 -12
  75. data/spec/spec_helper.rb +7 -6
  76. data/spec/support/class_helpers.rb +2 -2
  77. metadata +98 -48
  78. data/.rvmrc +0 -1
  79. data/lib/chewy/index/client.rb +0 -13
  80. data/spec/chewy/index/client_spec.rb +0 -18
@@ -5,28 +5,23 @@ describe Chewy::Query::Criteria do
5
5
 
6
6
  subject { described_class.new }
7
7
 
8
- its(:search) { should == {} }
9
- its(:query) { should == {} }
8
+ its(:options) { should be_a Hash }
10
9
  its(:facets) { should == {} }
10
+ its(:queries) { should == [] }
11
11
  its(:filters) { should == [] }
12
12
  its(:sort) { should == [] }
13
13
  its(:fields) { should == [] }
14
+ its(:types) { should == [] }
14
15
 
15
- its(:search?) { should be_false }
16
- its(:query?) { should be_false }
17
16
  its(:facets?) { should be_false }
17
+ its(:queries?) { should be_false }
18
18
  its(:filters?) { should be_false }
19
19
  its(:sort?) { should be_false }
20
20
  its(:fields?) { should be_false }
21
+ its(:types?) { should be_false }
21
22
 
22
- describe '#update_search' do
23
- specify { expect { subject.update_search(field: 'hello') }.to change { subject.search? }.to(true) }
24
- specify { expect { subject.update_search(field: 'hello') }.to change { subject.search }.to(field: 'hello') }
25
- end
26
-
27
- describe '#update_query' do
28
- specify { expect { subject.update_query(field: 'hello') }.to change { subject.query? }.to(true) }
29
- specify { expect { subject.update_query(field: 'hello') }.to change { subject.query }.to(field: 'hello') }
23
+ describe '#update_options' do
24
+ specify { expect { subject.update_options(field: 'hello') }.to change { subject.options }.to(hash_including(field: 'hello')) }
30
25
  end
31
26
 
32
27
  describe '#update_facets' do
@@ -34,6 +29,15 @@ describe Chewy::Query::Criteria do
34
29
  specify { expect { subject.update_facets(field: 'hello') }.to change { subject.facets }.to(field: 'hello') }
35
30
  end
36
31
 
32
+ describe '#update_queries' do
33
+ specify { expect { subject.update_queries(field: 'hello') }.to change { subject.queries? }.to(true) }
34
+ specify { expect { subject.update_queries(field: 'hello') }.to change { subject.queries }.to([field: 'hello']) }
35
+ specify { expect { subject.update_queries(field: 'hello'); subject.update_queries(field: 'world') }
36
+ .to change { subject.queries }.to([{field: 'hello'}, {field: 'world'}]) }
37
+ specify { expect { subject.update_queries([{field: 'hello'}, {field: 'world'}, nil]) }
38
+ .to change { subject.queries }.to([{field: 'hello'}, {field: 'world'}]) }
39
+ end
40
+
37
41
  describe '#update_filters' do
38
42
  specify { expect { subject.update_filters(field: 'hello') }.to change { subject.filters? }.to(true) }
39
43
  specify { expect { subject.update_filters(field: 'hello') }.to change { subject.filters }.to([{field: 'hello'}]) }
@@ -70,4 +74,207 @@ describe Chewy::Query::Criteria do
70
74
  specify { expect { subject.tap { |s| s.update_fields(:field1) }.update_fields([:field2, :field3], purge: true) }
71
75
  .to change { subject.fields }.to(['field2', 'field3']) }
72
76
  end
77
+
78
+ describe '#update_types' do
79
+ specify { expect { subject.update_types(:type) }.to change { subject.types? }.to(true) }
80
+ specify { expect { subject.update_types(:type) }.to change { subject.types }.to(['type']) }
81
+ specify { expect { subject.update_types([:type, :type]) }.to change { subject.types }.to(['type']) }
82
+ specify { expect { subject.update_types([:type1, :type2]) }.to change { subject.types }.to(['type1', 'type2']) }
83
+ specify { expect { subject.tap { |s| s.update_types(:type1) }.update_types([:type2, :type3]) }
84
+ .to change { subject.types }.to(['type1', 'type2', 'type3']) }
85
+ specify { expect { subject.tap { |s| s.update_types(:type1) }.update_types([:type2, :type3], purge: true) }
86
+ .to change { subject.types }.to(['type2', 'type3']) }
87
+ end
88
+
89
+ describe '#merge' do
90
+ let(:criteria) { described_class.new }
91
+
92
+ specify { subject.merge(criteria).should_not be_equal subject }
93
+ specify { subject.merge(criteria).should_not be_equal criteria }
94
+
95
+ specify { subject.tap { |c| c.update_options(opt1: 'hello') }
96
+ .merge(criteria.tap { |c| c.update_options(opt2: 'hello') }).options.should include(opt1: 'hello', opt2: 'hello') }
97
+ specify { subject.tap { |c| c.update_facets(field1: 'hello') }
98
+ .merge(criteria.tap { |c| c.update_facets(field1: 'hello') }).facets.should == {field1: 'hello', field1: 'hello'} }
99
+ specify { subject.tap { |c| c.update_queries(field1: 'hello') }
100
+ .merge(criteria.tap { |c| c.update_queries(field2: 'hello') }).queries.should == [{field1: 'hello'}, {field2: 'hello'}] }
101
+ specify { subject.tap { |c| c.update_filters(field1: 'hello') }
102
+ .merge(criteria.tap { |c| c.update_filters(field2: 'hello') }).filters.should == [{field1: 'hello'}, {field2: 'hello'}] }
103
+ specify { subject.tap { |c| c.update_sort(:field1) }
104
+ .merge(criteria.tap { |c| c.update_sort(:field2) }).sort.should == [:field1, :field2] }
105
+ specify { subject.tap { |c| c.update_fields(:field1) }
106
+ .merge(criteria.tap { |c| c.update_fields(:field2) }).fields.should == ['field1', 'field2'] }
107
+ specify { subject.tap { |c| c.update_types(:type1) }
108
+ .merge(criteria.tap { |c| c.update_types(:type2) }).types.should == ['type1', 'type2'] }
109
+ end
110
+
111
+ describe '#merge!' do
112
+ let(:criteria) { described_class.new }
113
+
114
+ specify { subject.merge!(criteria).should be_equal subject }
115
+ specify { subject.merge!(criteria).should_not be_equal criteria }
116
+
117
+ specify { subject.tap { |c| c.update_options(opt1: 'hello') }
118
+ .merge!(criteria.tap { |c| c.update_options(opt2: 'hello') }).options.should include(opt1: 'hello', opt2: 'hello') }
119
+ specify { subject.tap { |c| c.update_facets(field1: 'hello') }
120
+ .merge!(criteria.tap { |c| c.update_facets(field1: 'hello') }).facets.should == {field1: 'hello', field1: 'hello'} }
121
+ specify { subject.tap { |c| c.update_queries(field1: 'hello') }
122
+ .merge!(criteria.tap { |c| c.update_queries(field2: 'hello') }).queries.should == [{field1: 'hello'}, {field2: 'hello'}] }
123
+ specify { subject.tap { |c| c.update_filters(field1: 'hello') }
124
+ .merge!(criteria.tap { |c| c.update_filters(field2: 'hello') }).filters.should == [{field1: 'hello'}, {field2: 'hello'}] }
125
+ specify { subject.tap { |c| c.update_sort(:field1) }
126
+ .merge!(criteria.tap { |c| c.update_sort(:field2) }).sort.should == [:field1, :field2] }
127
+ specify { subject.tap { |c| c.update_fields(:field1) }
128
+ .merge!(criteria.tap { |c| c.update_fields(:field2) }).fields.should == ['field1', 'field2'] }
129
+ specify { subject.tap { |c| c.update_types(:type1) }
130
+ .merge!(criteria.tap { |c| c.update_types(:type2) }).types.should == ['type1', 'type2'] }
131
+ end
132
+
133
+ describe '#request_body' do
134
+ def request_body &block
135
+ subject.instance_exec(&block) if block
136
+ subject.request_body
137
+ end
138
+
139
+ specify { request_body.should == {body: {}} }
140
+ specify { request_body { update_options(size: 10) }.should == {body: {size: 10}} }
141
+ specify { request_body { update_options(from: 10) }.should == {body: {from: 10}} }
142
+ specify { request_body { update_options(explain: true) }.should == {body: {explain: true}} }
143
+ specify { request_body { update_queries(:query) }.should == {body: {query: :query}} }
144
+ specify { request_body {
145
+ update_options(from: 10); update_sort(:field); update_fields(:field); update_queries(:query)
146
+ }.should == {body: {query: :query, from: 10, sort: [:field], fields: ['field']}} }
147
+ end
148
+
149
+ describe '#_request_query' do
150
+ def _request_query &block
151
+ subject.instance_exec(&block) if block
152
+ subject.send(:_request_query)
153
+ end
154
+
155
+ specify { _request_query.should be_nil }
156
+ specify { _request_query { update_queries(:query) }.should == {query: :query} }
157
+ specify { _request_query { update_queries([:query1, :query2]) }
158
+ .should == {query: {bool: {must: [:query1, :query2]}}} }
159
+ specify { _request_query { update_options(query_mode: :should); update_queries([:query1, :query2]) }
160
+ .should == {query: {bool: {should: [:query1, :query2]}}} }
161
+ specify { _request_query { update_options(query_mode: :dis_max); update_queries([:query1, :query2]) }
162
+ .should == {query: {dis_max: {queries: [:query1, :query2]}}} }
163
+
164
+ specify { _request_query { update_filters([:filter1, :filter2]) }
165
+ .should == {query: {filtered: {query: {match_all: {}}, filter: {and: [:filter1, :filter2]}}}} }
166
+ specify { _request_query { update_filters([:filter1, :filter2]); update_queries([:query1, :query2]) }
167
+ .should == {query: {filtered: {
168
+ query: {bool: {must: [:query1, :query2]}},
169
+ filter: {and: [:filter1, :filter2]}
170
+ }}}
171
+ }
172
+ specify { _request_query {
173
+ update_options(query_mode: :should); update_options(filter_mode: :or);
174
+ update_filters([:filter1, :filter2]); update_queries([:query1, :query2])
175
+ }.should == {query: {filtered: {
176
+ query: {bool: {should: [:query1, :query2]}},
177
+ filter: {or: [:filter1, :filter2]}
178
+ }}}
179
+ }
180
+ end
181
+
182
+ describe '#_request_filter' do
183
+ def _request_filter &block
184
+ subject.instance_exec(&block) if block
185
+ subject.send(:_request_filter)
186
+ end
187
+
188
+ specify { _request_filter.should be_nil }
189
+
190
+ specify { _request_filter { update_types(:type) }.should == {type: {value: 'type'}} }
191
+ specify { _request_filter { update_types([:type1, :type2]) }
192
+ .should == {or: [{type: {value: 'type1'}}, {type: {value: 'type2'}}]} }
193
+
194
+ specify { _request_filter { update_filters([:filter1, :filter2]) }
195
+ .should == {and: [:filter1, :filter2]} }
196
+ specify { _request_filter { update_options(filter_mode: :or); update_filters([:filter1, :filter2]) }
197
+ .should == {or: [:filter1, :filter2]} }
198
+ specify { _request_filter { update_options(filter_mode: :must); update_filters([:filter1, :filter2]) }
199
+ .should == {bool: {must: [:filter1, :filter2]}} }
200
+ specify { _request_filter { update_options(filter_mode: :should); update_filters([:filter1, :filter2]) }
201
+ .should == {bool: {should: [:filter1, :filter2]}} }
202
+
203
+ specify { _request_filter { update_types([:type1, :type2]); update_filters([:filter1, :filter2]) }
204
+ .should == {and: [{or: [{type: {value: 'type1'}}, {type: {value: 'type2'}}]}, :filter1, :filter2]} }
205
+ specify { _request_filter { update_options(filter_mode: :or); update_types([:type1, :type2]); update_filters([:filter1, :filter2]) }
206
+ .should == {and: [{or: [{type: {value: 'type1'}}, {type: {value: 'type2'}}]}, {or: [:filter1, :filter2]}]} }
207
+ specify { _request_filter { update_options(filter_mode: :must); update_types([:type1, :type2]); update_filters([:filter1, :filter2]) }
208
+ .should == {and: [{or: [{type: {value: 'type1'}}, {type: {value: 'type2'}}]}, {bool: {must: [:filter1, :filter2]}}]} }
209
+ specify { _request_filter { update_options(filter_mode: :should); update_types([:type1, :type2]); update_filters([:filter1, :filter2]) }
210
+ .should == {and: [{or: [{type: {value: 'type1'}}, {type: {value: 'type2'}}]}, {bool: {should: [:filter1, :filter2]}}]} }
211
+ end
212
+
213
+ describe '#_request_types' do
214
+ def _request_types &block
215
+ subject.instance_exec(&block) if block
216
+ subject.send(:_request_types)
217
+ end
218
+
219
+ specify { _request_types.should be_nil }
220
+ specify { _request_types { update_types(:type1) }.should == {type: {value: 'type1'}} }
221
+ specify { _request_types { update_types([:type1, :type2]) }
222
+ .should == {or: [{type: {value: 'type1'}}, {type: {value: 'type2'}}]} }
223
+ end
224
+
225
+ describe '#_queries_join' do
226
+ def _queries_join *args
227
+ subject.send(:_queries_join, *args)
228
+ end
229
+
230
+ let(:query) { {term: {field: 'value'}} }
231
+
232
+ specify { _queries_join([], :dis_max).should be_nil }
233
+ specify { _queries_join([query], :dis_max).should == query }
234
+ specify { _queries_join([query, query], :dis_max).should == {dis_max: {queries: [query, query]}} }
235
+
236
+ specify { _queries_join([], 0.7).should be_nil }
237
+ specify { _queries_join([query], 0.7).should == query }
238
+ specify { _queries_join([query, query], 0.7).should == {dis_max: {queries: [query, query], tie_breaker: 0.7}} }
239
+
240
+ specify { _queries_join([], :must).should be_nil }
241
+ specify { _queries_join([query], :must).should == query }
242
+ specify { _queries_join([query, query], :must).should == {bool: {must: [query, query]}} }
243
+
244
+ specify { _queries_join([], :should).should be_nil }
245
+ specify { _queries_join([query], :should).should == query }
246
+ specify { _queries_join([query, query], :should).should == {bool: {should: [query, query]}} }
247
+
248
+ specify { _queries_join([], '25%').should be_nil }
249
+ specify { _queries_join([query], '25%').should == query }
250
+ specify { _queries_join([query, query], '25%').should == {bool: {should: [query, query], minimum_should_match: '25%'}} }
251
+ end
252
+
253
+ describe '#_filters_join' do
254
+ def _filters_join *args
255
+ subject.send(:_filters_join, *args)
256
+ end
257
+
258
+ let(:filter) { {term: {field: 'value'}} }
259
+
260
+ specify { _filters_join([], :and).should be_nil }
261
+ specify { _filters_join([filter], :and).should == filter }
262
+ specify { _filters_join([filter, filter], :and).should == {and: [filter, filter]} }
263
+
264
+ specify { _filters_join([], :or).should be_nil }
265
+ specify { _filters_join([filter], :or).should == filter }
266
+ specify { _filters_join([filter, filter], :or).should == {or: [filter, filter]} }
267
+
268
+ specify { _filters_join([], :must).should be_nil }
269
+ specify { _filters_join([filter], :must).should == filter }
270
+ specify { _filters_join([filter, filter], :must).should == {bool: {must: [filter, filter]}} }
271
+
272
+ specify { _filters_join([], :should).should be_nil }
273
+ specify { _filters_join([filter], :should).should == filter }
274
+ specify { _filters_join([filter, filter], :should).should == {bool: {should: [filter, filter]}} }
275
+
276
+ specify { _filters_join([], '25%').should be_nil }
277
+ specify { _filters_join([filter], '25%').should == filter }
278
+ specify { _filters_join([filter, filter], '25%').should == {bool: {should: [filter, filter], minimum_should_match: '25%'}} }
279
+ end
73
280
  end
@@ -29,9 +29,11 @@ describe Chewy::Query::Loading do
29
29
  PlacesIndex.country.import(countries)
30
30
  end
31
31
 
32
- specify { PlacesIndex.search.order(:rating).limit(6).load.should =~ cities.first(3) + countries.first(3) }
33
- specify { PlacesIndex.search.order(:rating).limit(6).load(scopes: {city: ->(i){ where('rating < 2') }})
34
- .should =~ cities.first(2) + countries.first(3) }
35
- specify { PlacesIndex.search.order(:rating).limit(6).load.total_count.should == 12 }
32
+ specify { PlacesIndex.order(:rating).limit(6).load.total_count.should == 12 }
33
+ specify { PlacesIndex.order(:rating).limit(6).load.should =~ cities.first(3) + countries.first(3) }
34
+ specify { PlacesIndex.order(:rating).limit(6).load(city: { scope: ->(i) { where('rating < 2') } })
35
+ .should =~ cities.first(2) + countries.first(3) + [nil] }
36
+ specify { PlacesIndex.order(:rating).limit(6).load(city: { scope: City.where('rating < 2') })
37
+ .should =~ cities.first(2) + countries.first(3) + [nil] }
36
38
  end
37
39
  end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chewy::Query::Nodes::And do
4
+ describe '#__render__' do
5
+ def render &block
6
+ Chewy::Query::Context.new(&block).__render__
7
+ end
8
+
9
+ specify { render { name? & (email == 'email') }.should == {
10
+ and: [{exists: {term: 'name'}}, {term: {'email' => 'email'}}]
11
+ } }
12
+ specify { render { ~(name? & (email == 'email')) }.should == {
13
+ and: {filters: [{exists: {term: 'name'}}, {term: {'email' => 'email'}}], _cache: true}
14
+ } }
15
+ end
16
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chewy::Query::Nodes::Bool do
4
+ describe '#__render__' do
5
+ def render &block
6
+ Chewy::Query::Context.new(&block).__render__
7
+ end
8
+
9
+ specify { render { must(name == 'name', email == 'email') }.should == {
10
+ bool: {must: [{term: {'name' => 'name'}}, {term: {'email' => 'email'}}]
11
+ } } }
12
+ specify { render { must(name == 'name').must_not(email == 'email') }.should == {
13
+ bool: {must: [{term: {'name' => 'name'}}], must_not: [{term: {'email' => 'email'}}]
14
+ } } }
15
+ specify { render { must(name == 'name').should(email == 'email') }.should == {
16
+ bool: {must: [{term: {'name' => 'name'}}], should: [{ term: {'email' => 'email'}}]
17
+ } } }
18
+ specify { render { ~must(name == 'name').should(email == 'email') }.should == {
19
+ bool: {must: [{term: {'name' => 'name'}}], should: [{ term: {'email' => 'email'}}], _cache: true
20
+ } } }
21
+ end
22
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chewy::Query::Nodes::Equal do
4
+ describe '#__render__' do
5
+ def render &block
6
+ Chewy::Query::Context.new(&block).__render__
7
+ end
8
+
9
+ specify { render { name == 'name' }.should == {term: {'name' => 'name'}} }
10
+ specify { render { name != 'name' }.should == {not: {term: {'name' => 'name'}}} }
11
+ specify { render { name == ['name1', 'name2'] }.should == {terms: {'name' => ['name1', 'name2']}} }
12
+ specify { render { name != ['name1', 'name2'] }.should == {not: {terms: {'name' => ['name1', 'name2']}}} }
13
+
14
+ specify { render { name(:bool) == 'name' }.should == {term: {'name' => 'name'}} }
15
+ specify { render { name(:borogoves) == ['name1', 'name2'] }.should == {terms: {'name' => ['name1', 'name2']}} }
16
+
17
+ specify { render { name(:|) == ['name1', 'name2'] }.should == {terms: {'name' => ['name1', 'name2'], execution: :or}} }
18
+ specify { render { name(:or) == ['name1', 'name2'] }.should == {terms: {'name' => ['name1', 'name2'], execution: :or}} }
19
+ specify { render { name(:&) == ['name1', 'name2'] }.should == {terms: {'name' => ['name1', 'name2'], execution: :and}} }
20
+ specify { render { name(:and) == ['name1', 'name2'] }.should == {terms: {'name' => ['name1', 'name2'], execution: :and}} }
21
+ specify { render { name(:b) == ['name1', 'name2'] }.should == {terms: {'name' => ['name1', 'name2'], execution: :bool}} }
22
+ specify { render { name(:bool) == ['name1', 'name2'] }.should == {terms: {'name' => ['name1', 'name2'], execution: :bool}} }
23
+ specify { render { name(:f) == ['name1', 'name2'] }.should == {terms: {'name' => ['name1', 'name2'], execution: :fielddata}} }
24
+ specify { render { name(:fielddata) == ['name1', 'name2'] }.should == {terms: {'name' => ['name1', 'name2'], execution: :fielddata}} }
25
+
26
+ specify { render { ~name == 'name' }.should == {term: {'name' => 'name', _cache: true}} }
27
+ specify { render { ~(name == 'name') }.should == {term: {'name' => 'name', _cache: true}} }
28
+ specify { render { ~name != 'name' }.should == {not: {term: {'name' => 'name', _cache: true}}} }
29
+ specify { render { ~name(:|) == ['name1', 'name2'] }.should == {terms: {'name' => ['name1', 'name2'], execution: :or, _cache: true}} }
30
+ specify { render { ~name != ['name1', 'name2'] }.should == {not: {terms: {'name' => ['name1', 'name2'], _cache: true}}} }
31
+ end
32
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chewy::Query::Nodes::Exists do
4
+ describe '#__render__' do
5
+ def render &block
6
+ Chewy::Query::Context.new(&block).__render__
7
+ end
8
+
9
+ specify { render { name? }.should == {exists: {term: 'name'}} }
10
+
11
+ specify { render { !!name? }.should == {exists: {term: 'name'}} }
12
+ specify { render { !!name }.should == {exists: {term: 'name'}} }
13
+ specify { render { name != nil }.should == {exists: {term: 'name'}} }
14
+ specify { render { !(name == nil) }.should == {exists: {term: 'name'}} }
15
+
16
+ specify { render { ~name? }.should == {exists: {term: 'name'}} }
17
+ end
18
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chewy::Query::Nodes::Missing do
4
+ describe '#__render__' do
5
+ def render &block
6
+ Chewy::Query::Context.new(&block).__render__
7
+ end
8
+
9
+ specify { render { !name }.should == {missing: {term: 'name', existence: true, null_value: false}} }
10
+ specify { render { !name? }.should == {missing: {term: 'name', existence: true, null_value: true}} }
11
+ specify { render { name == nil }.should == {missing: {term: 'name', existence: false, null_value: true}} }
12
+
13
+ specify { render { ~!name }.should == {missing: {term: 'name', existence: true, null_value: false}} }
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chewy::Query::Nodes::Not do
4
+ describe '#__render__' do
5
+ def render &block
6
+ Chewy::Query::Context.new(&block).__render__
7
+ end
8
+
9
+ specify { render { !(email == 'email') }.should == {
10
+ not: {term: {'email' => 'email'}}
11
+ } }
12
+ specify { render { ~!(email == 'email') }.should == {
13
+ not: {filter: {term: {'email' => 'email'}}, _cache: true}
14
+ } }
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chewy::Query::Nodes::Or do
4
+ describe '#__render__' do
5
+ def render &block
6
+ Chewy::Query::Context.new(&block).__render__
7
+ end
8
+
9
+ specify { render { name? | (email == 'email') }.should == {
10
+ or: [{exists: {term: 'name'}}, {term: {'email' => 'email'}}]
11
+ } }
12
+ specify { render { ~(name? | (email == 'email')) }.should == {
13
+ or: {filters: [{exists: {term: 'name'}}, {term: {'email' => 'email'}}], _cache: true}
14
+ } }
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chewy::Query::Nodes::Prefix do
4
+ describe '#__render__' do
5
+ def render &block
6
+ Chewy::Query::Context.new(&block).__render__
7
+ end
8
+
9
+ specify { render { name =~ 'nam' }.should == {prefix: {'name' => 'nam'}} }
10
+ specify { render { name !~ 'nam' }.should == {not: {prefix: {'name' => 'nam'}}} }
11
+
12
+ specify { render { ~name =~ 'nam' }.should == {prefix: {'name' => 'nam', _cache: true}} }
13
+ specify { render { ~name !~ 'nam' }.should == {not: {prefix: {'name' => 'nam', _cache: true}}} }
14
+ specify { render { name(cache: false) =~ 'nam' }.should == {prefix: {'name' => 'nam', _cache: false}} }
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chewy::Query::Nodes::Query do
4
+ describe '#__render__' do
5
+ def render &block
6
+ Chewy::Query::Context.new(&block).__render__
7
+ end
8
+
9
+ specify { render { q(query_string: {query: 'name: hello'}) }.should == {query: {query_string: {query: 'name: hello'}}} }
10
+ specify { render { ~q(query_string: {query: 'name: hello'}) }.should == {fquery: {query: {query_string: {query: 'name: hello'}}, _cache: true}} }
11
+ end
12
+ end