chewy_query 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/.rspec +3 -0
  4. data/Gemfile +6 -0
  5. data/LICENSE.txt +22 -0
  6. data/README.md +37 -0
  7. data/Rakefile +2 -0
  8. data/chewy_query.gemspec +27 -0
  9. data/lib/chewy_query.rb +12 -0
  10. data/lib/chewy_query/builder.rb +865 -0
  11. data/lib/chewy_query/builder/compose.rb +64 -0
  12. data/lib/chewy_query/builder/criteria.rb +182 -0
  13. data/lib/chewy_query/builder/filters.rb +227 -0
  14. data/lib/chewy_query/builder/nodes/and.rb +26 -0
  15. data/lib/chewy_query/builder/nodes/base.rb +17 -0
  16. data/lib/chewy_query/builder/nodes/bool.rb +33 -0
  17. data/lib/chewy_query/builder/nodes/equal.rb +34 -0
  18. data/lib/chewy_query/builder/nodes/exists.rb +20 -0
  19. data/lib/chewy_query/builder/nodes/expr.rb +28 -0
  20. data/lib/chewy_query/builder/nodes/field.rb +106 -0
  21. data/lib/chewy_query/builder/nodes/has_child.rb +14 -0
  22. data/lib/chewy_query/builder/nodes/has_parent.rb +14 -0
  23. data/lib/chewy_query/builder/nodes/has_relation.rb +61 -0
  24. data/lib/chewy_query/builder/nodes/match_all.rb +11 -0
  25. data/lib/chewy_query/builder/nodes/missing.rb +20 -0
  26. data/lib/chewy_query/builder/nodes/not.rb +26 -0
  27. data/lib/chewy_query/builder/nodes/or.rb +26 -0
  28. data/lib/chewy_query/builder/nodes/prefix.rb +18 -0
  29. data/lib/chewy_query/builder/nodes/query.rb +20 -0
  30. data/lib/chewy_query/builder/nodes/range.rb +63 -0
  31. data/lib/chewy_query/builder/nodes/raw.rb +15 -0
  32. data/lib/chewy_query/builder/nodes/regexp.rb +33 -0
  33. data/lib/chewy_query/builder/nodes/script.rb +20 -0
  34. data/lib/chewy_query/version.rb +3 -0
  35. data/spec/chewy_query/builder/context_spec.rb +529 -0
  36. data/spec/chewy_query/builder/filters_spec.rb +181 -0
  37. data/spec/chewy_query/builder/nodes/and_spec.rb +16 -0
  38. data/spec/chewy_query/builder/nodes/bool_spec.rb +22 -0
  39. data/spec/chewy_query/builder/nodes/equal_spec.rb +58 -0
  40. data/spec/chewy_query/builder/nodes/exists_spec.rb +16 -0
  41. data/spec/chewy_query/builder/nodes/has_child_spec.rb +79 -0
  42. data/spec/chewy_query/builder/nodes/has_parent_spec.rb +84 -0
  43. data/spec/chewy_query/builder/nodes/match_all_spec.rb +11 -0
  44. data/spec/chewy_query/builder/nodes/missing_spec.rb +14 -0
  45. data/spec/chewy_query/builder/nodes/not_spec.rb +14 -0
  46. data/spec/chewy_query/builder/nodes/or_spec.rb +16 -0
  47. data/spec/chewy_query/builder/nodes/prefix_spec.rb +15 -0
  48. data/spec/chewy_query/builder/nodes/query_spec.rb +17 -0
  49. data/spec/chewy_query/builder/nodes/range_spec.rb +36 -0
  50. data/spec/chewy_query/builder/nodes/raw_spec.rb +11 -0
  51. data/spec/chewy_query/builder/nodes/regexp_spec.rb +45 -0
  52. data/spec/chewy_query/builder/nodes/script_spec.rb +16 -0
  53. data/spec/chewy_query/builder_spec.rb +196 -0
  54. data/spec/chewy_query_spec.rb +0 -0
  55. data/spec/spec_helper.rb +8 -0
  56. metadata +191 -0
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChewyQuery::Builder::Nodes::MatchAll do
4
+ describe '#__render__' do
5
+ def render(&block)
6
+ ChewyQuery::Builder::Filters.new(&block).__render__
7
+ end
8
+
9
+ specify{ expect(render{ match_all }).to eq(match_all: {}) }
10
+ end
11
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChewyQuery::Builder::Nodes::Missing do
4
+ describe '#__render__' do
5
+ def render(&block)
6
+ ChewyQuery::Builder::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 }) }
12
+ specify{ expect(render{ ~!name }).to eq(missing: { field: 'name', existence: true, null_value: false }) }
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChewyQuery::Builder::Nodes::Not do
4
+ describe '#__render__' do
5
+ def render(&block)
6
+ ChewyQuery::Builder::Filters.new(&block).__render__
7
+ end
8
+
9
+ specify{ expect(render{ !(email == 'email') }).to eq(not: { term: { 'email' => 'email' } }) }
10
+ specify{ expect(render{ ~!(email == 'email') }).to eq(
11
+ not: { filter: { term: { 'email' => 'email' } }, _cache: true }
12
+ ) }
13
+ end
14
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChewyQuery::Builder::Nodes::Or do
4
+ describe '#__render__' do
5
+ def render(&block)
6
+ ChewyQuery::Builder::Filters.new(&block).__render__
7
+ end
8
+
9
+ specify{ expect(render{ name? | (email == 'email') }).to eq(
10
+ or: [{ exists: { field: 'name' } }, { term: { 'email' => 'email' } }]
11
+ ) }
12
+ specify{ expect(render{ ~(name? | (email == 'email')) }).to eq(
13
+ or: { filters: [{ exists: { field: 'name' } }, { term: { 'email' => 'email' } }], _cache: true }
14
+ ) }
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChewyQuery::Builder::Nodes::Prefix do
4
+ describe '#__render__' do
5
+ def render(&block)
6
+ ChewyQuery::Builder::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
+ specify{ expect(render{ ~name =~ 'nam' }).to eq(prefix: { 'name' => 'nam', _cache: true }) }
12
+ specify{ expect(render{ ~name !~ 'nam' }).to eq(not: { prefix: { 'name' => 'nam', _cache: true } }) }
13
+ specify{ expect(render{ name(cache: false) =~ 'nam' }).to eq(prefix: { 'name' => 'nam', _cache: false }) }
14
+ end
15
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChewyQuery::Builder::Nodes::Query do
4
+ describe '#__render__' do
5
+ def render(&block)
6
+ ChewyQuery::Builder::Filters.new(&block).__render__
7
+ end
8
+
9
+ specify{ expect(render{ q(query_string: { query: 'name: hello' }) }).to eq(
10
+ query: { query_string: { query: 'name: hello' } }
11
+ ) }
12
+
13
+ specify{ expect(render{ ~q(query_string: { query: 'name: hello' }) }).to eq(
14
+ fquery: { query: { query_string: { query: 'name: hello' } }, _cache: true }
15
+ ) }
16
+ end
17
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChewyQuery::Builder::Nodes::Range do
4
+ describe '#__render__' do
5
+ def render(&block)
6
+ ChewyQuery::Builder::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
+ specify{ expect(render{ age > 42 }).to eq(range: { 'age' => { gt: 42 } }) }
12
+ specify{ expect(render{ age == (42..45) }).to eq(range: { 'age' => { gt: 42, lt: 45 } }) }
13
+ specify{ expect(render{ age == [42..45] }).to eq(range: { 'age' => { gte: 42, lte: 45 } }) }
14
+ specify{ expect(render{ (age > 42) & (age <= 45) }).to eq( range: { 'age' => { gt: 42, lte: 45 } }) }
15
+ specify{ expect(render{ ~age > 42 }).to eq(range: { 'age' => { gt: 42 }, _cache: true }) }
16
+ specify{ expect(render{ ~age == (42..45) }).to eq(range: { 'age' => { gt: 42, lt: 45 }, _cache: true }) }
17
+ specify{ expect(render{ ~age == [42..45] }).to eq(range: { 'age' => { gte: 42, lte: 45 }, _cache: true }) }
18
+ specify{ expect(render{ (age > 42) & ~(age <= 45) }).to eq(range: { 'age' => { gt: 42, lte: 45 }, _cache: true }) }
19
+ specify{ expect(render{ (~age > 42) & (age <= 45) }).to eq(range: { 'age' => { gt: 42, lte: 45 }, _cache: true }) }
20
+ specify{ expect(render{ age(:i) > 42 }).to eq(range: { 'age' => { gt: 42 }, execution: :index }) }
21
+ specify{ expect(render{ age(:index) > 42 }).to eq(range: { 'age' => { gt: 42 }, execution: :index }) }
22
+ specify{ expect(render{ age(:f) > 42 }).to eq(range: { 'age' => { gt: 42 }, execution: :fielddata }) }
23
+ specify{ expect(render{ age(:fielddata) > 42 }).to eq(range: { 'age' => { gt: 42 }, execution: :fielddata }) }
24
+ specify{ expect(render{ (age(:f) > 42) & (age <= 45) }).to eq(
25
+ range: { 'age' => { gt: 42, lte: 45 }, execution: :fielddata }
26
+ ) }
27
+
28
+ specify{ expect(render{ ~age(:f) > 42 }).to eq(
29
+ range: { 'age' => { gt: 42 }, execution: :fielddata, _cache: true }
30
+ ) }
31
+
32
+ specify{ expect(render{ (age(:f) > 42) & (~age <= 45) }).to eq(
33
+ range: { 'age' => { gt: 42, lte: 45 }, execution: :fielddata, _cache: true }
34
+ ) }
35
+ end
36
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChewyQuery::Builder::Nodes::Raw do
4
+ describe '#__render__' do
5
+ def render(&block)
6
+ ChewyQuery::Builder::Filters.new(&block).__render__
7
+ end
8
+
9
+ specify{ expect(render{ r(term: { name: 'name' }) }).to eq(term: { name: 'name' }) }
10
+ end
11
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChewyQuery::Builder::Nodes::Regexp do
4
+ describe '#__render__' do
5
+ def render(&block)
6
+ ChewyQuery::Builder::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
+ specify{ expect(render{ names.first(flags: [:anystring, :intersection, :borogoves]) == /nam.*/ }).to eq(
14
+ regexp: { 'names.first' => { value: 'nam.*', flags: 'ANYSTRING|INTERSECTION' } }
15
+ ) }
16
+
17
+ specify{ expect(render{ names.first(:anystring, :intersection, :borogoves) == /nam.*/ }).to eq(
18
+ regexp: { 'names.first' => { value: 'nam.*', flags: 'ANYSTRING|INTERSECTION' } }
19
+ ) }
20
+
21
+ specify{ expect(render{ names.first(flags: [:anystring, :intersection, :borogoves]) =~ /nam.*/ }).to eq(
22
+ regexp: { 'names.first' => { value: 'nam.*', flags: 'ANYSTRING|INTERSECTION' } }
23
+ ) }
24
+
25
+ specify{ expect(render{ names.first(:anystring, :intersection, :borogoves) =~ /nam.*/ }).to eq(
26
+ regexp: { 'names.first' => { value: 'nam.*', flags: 'ANYSTRING|INTERSECTION' } }
27
+ ) }
28
+
29
+ specify{ expect(render{ ~names.first == /nam.*/ }).to eq(
30
+ regexp: { 'names.first' => 'nam.*', _cache: true, _cache_key: 'nam.*' }
31
+ ) }
32
+
33
+ specify{ expect(render{ names.first(cache: 'name') == /nam.*/ }).to eq(
34
+ regexp: { 'names.first' => 'nam.*', _cache: true, _cache_key: 'name' }
35
+ ) }
36
+
37
+ specify{ expect(render{ ~names.first(:anystring) =~ /nam.*/ }).to eq(
38
+ regexp: { 'names.first' => { value: 'nam.*', flags: 'ANYSTRING' }, _cache: true, _cache_key: 'nam.*' }
39
+ ) }
40
+
41
+ specify{ expect(render{ names.first(:anystring, cache: 'name') =~ /nam.*/ }).to eq(
42
+ regexp: { 'names.first' => { value: 'nam.*', flags: 'ANYSTRING' }, _cache: true, _cache_key: 'name' }
43
+ ) }
44
+ end
45
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChewyQuery::Builder::Nodes::Script do
4
+ describe '#__render__' do
5
+ def render(&block)
6
+ ChewyQuery::Builder::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
+ specify{ expect(render{ ~s('var = val') }).to eq(script: { script: 'var = val', _cache: true }) }
12
+ specify{ expect(render{ ~s('var = val', val: 42) }).to eq(
13
+ script: { script: 'var = val', params: { val: 42 }, _cache: true }
14
+ ) }
15
+ end
16
+ end
@@ -0,0 +1,196 @@
1
+ require 'spec_helper'
2
+
3
+ describe ChewyQuery::Builder do
4
+ subject{ ChewyQuery::Builder.new(:products, types: :product) }
5
+
6
+ describe '#==' do
7
+ specify{ expect(subject.query(match: 'hello') == subject.query(match: 'hello')).to be_truthy }
8
+ specify{ expect(subject.query(match: 'hello') == subject.query(match: 'world')).to be_falsy }
9
+ specify{ expect(subject.limit(10) == subject.limit(10)).to be_truthy }
10
+ specify{ expect(subject.limit(10) == subject.limit(11)).to be_falsy }
11
+ specify{ expect(subject.limit(2).limit(10) == subject.limit(10)).to be_truthy }
12
+ end
13
+
14
+ describe '#query_mode' do
15
+ specify{ expect(subject.query_mode(:should)).to be_a(described_class) }
16
+ specify{ expect(subject.query_mode(:should)).not_to eq(subject) }
17
+ specify{ expect(subject.query_mode(:should).criteria.options).to include(query_mode: :should) }
18
+ specify{ expect{ subject.query_mode(:should) }.not_to change{ subject.criteria.options } }
19
+ end
20
+
21
+ describe '#filter_mode' do
22
+ specify{ expect(subject.filter_mode(:or)).to be_a(described_class) }
23
+ specify{ expect(subject.filter_mode(:or)).not_to eq(subject) }
24
+ specify{ expect(subject.filter_mode(:or).criteria.options).to include(filter_mode: :or) }
25
+ specify{ expect{ subject.filter_mode(:or) }.not_to change{ subject.criteria.options } }
26
+ end
27
+
28
+ describe '#post_filter_mode' do
29
+ specify{ expect(subject.post_filter_mode(:or)).to be_a(described_class) }
30
+ specify{ expect(subject.post_filter_mode(:or)).not_to eq(subject) }
31
+ specify{ expect(subject.post_filter_mode(:or).criteria.options).to include(post_filter_mode: :or) }
32
+ specify{ expect{ subject.post_filter_mode(:or) }.not_to change{ subject.criteria.options } }
33
+ end
34
+
35
+ describe '#limit' do
36
+ specify{ expect(subject.limit(10)).to be_a(described_class) }
37
+ specify{ expect(subject.limit(10)).not_to eq(subject) }
38
+ specify{ expect(subject.limit(10).criteria.request_options).to include(size: 10) }
39
+ specify{ expect{ subject.limit(10) }.not_to change{ subject.criteria.request_options } }
40
+ end
41
+
42
+ describe '#offset' do
43
+ specify{ expect(subject.offset(10)).to be_a(described_class) }
44
+ specify{ expect(subject.offset(10)).not_to eq(subject) }
45
+ specify{ expect(subject.offset(10).criteria.request_options).to include(from: 10) }
46
+ specify{ expect{ subject.offset(10) }.not_to change{ subject.criteria.request_options } }
47
+ end
48
+
49
+ describe '#facets' do
50
+ specify{ expect(subject.facets(term: { field: 'hello' })).to be_a(described_class) }
51
+ specify{ expect(subject.facets(term: { field: 'hello' })).not_to eq(subject) }
52
+ specify{ expect(subject.facets(term: { field: 'hello' }).criteria.facets).to include(term: { field: 'hello' }) }
53
+ specify{ expect{ subject.facets(term: { field: 'hello' }) }.not_to change{ subject.criteria.facets } }
54
+ end
55
+
56
+ describe '#aggregations' do
57
+ specify{ expect(subject.aggregations(agg1: { field: 'hello' })).to be_a(described_class) }
58
+ specify{ expect(subject.aggregations(agg1: { field: 'hello' })).not_to eq(subject) }
59
+ specify{
60
+ expect(subject.aggregations(agg1: { field: 'hello' }).criteria.aggregations).to include(agg1: { field: 'hello' })
61
+ }
62
+ specify{ expect{ subject.aggregations(agg1: { field: 'hello' }) }.not_to change { subject.criteria.aggregations } }
63
+ end
64
+
65
+ describe '#suggest' do
66
+ specify{ expect(subject.suggest(name1: { text: 'hello' })).to be_a(described_class) }
67
+ specify{ expect(subject.suggest(name1: { text: 'hello' })).not_to eq(subject) }
68
+ specify{ expect(subject.suggest(name1: { text: 'hello' }).criteria.suggest).to include(name1: { text: 'hello' }) }
69
+ specify{ expect{ subject.suggest(name1: { text: 'hello' }) }.not_to change{ subject.criteria.suggest } }
70
+ end
71
+
72
+ describe '#strategy' do
73
+ specify{ expect(subject.strategy('query_first')).to be_a(described_class) }
74
+ specify{ expect(subject.strategy('query_first')).not_to eq(subject) }
75
+ specify{ expect(subject.strategy('query_first').criteria.options).to include(strategy: 'query_first') }
76
+ specify{ expect{ subject.strategy('query_first') }.not_to change{ subject.criteria.options } }
77
+ end
78
+
79
+ describe '#query' do
80
+ specify{ expect(subject.query(match: 'hello')).to be_a(described_class) }
81
+ specify{ expect(subject.query(match: 'hello')).not_to eq(subject) }
82
+ specify{ expect(subject.query(match: 'hello').criteria.queries).to include(match: 'hello') }
83
+ specify{ expect{ subject.query(match: 'hello') }.not_to change{ subject.criteria.queries } }
84
+ end
85
+
86
+ describe '#filter' do
87
+ specify{ expect(subject.filter(term: { field: 'hello' })).to be_a(described_class) }
88
+ specify{ expect(subject.filter(term: { field: 'hello' })).not_to eq(subject) }
89
+ specify{ expect{ subject.filter(term: { field: 'hello' }) }.not_to change{ subject.criteria.filters } }
90
+ specify{
91
+ expect(subject.filter(
92
+ [{ term: { field: 'hello' } }, { term: { field: 'world' } }]
93
+ ).criteria.filters).to eq([{ term: { field: 'hello' } }, { term: { field: 'world' } }])
94
+ }
95
+
96
+ specify{ expect{ subject.filter{ name == 'John' } }.not_to change{ subject.criteria.filters } }
97
+ specify{ expect(subject.filter{ name == 'John' }.criteria.filters).to eq([{ term: { 'name' => 'John' } }]) }
98
+ end
99
+
100
+ describe '#post_filter' do
101
+ specify{ expect(subject.post_filter(term: { field: 'hello' })).to be_a(described_class) }
102
+ specify{ expect(subject.post_filter(term: { field: 'hello' })).not_to eq(subject) }
103
+ specify{ expect{ subject.post_filter(term: { field: 'hello' }) }.not_to change{ subject.criteria.post_filters } }
104
+ specify{
105
+ expect(subject.post_filter(
106
+ [{ term: { field: 'hello' } }, { term: { field: 'world' } }]
107
+ ).criteria.post_filters).to eq([{ term: { field: 'hello' } }, { term: { field: 'world' } }])
108
+ }
109
+
110
+ specify{ expect{ subject.post_filter{ name == 'John' } }.not_to change{ subject.criteria.post_filters } }
111
+ specify{
112
+ expect(subject.post_filter{ name == 'John' }.criteria.post_filters).to eq([{ term: { 'name' => 'John' } }])
113
+ }
114
+ end
115
+
116
+ describe '#order' do
117
+ specify{ expect(subject.order(field: 'hello')).to be_a(described_class) }
118
+ specify{ expect(subject.order(field: 'hello')).not_to eq(subject) }
119
+ specify{ expect{ subject.order(field: 'hello') }.not_to change{ subject.criteria.sort } }
120
+
121
+ specify{ expect(subject.order(:field).criteria.sort).to eq([:field]) }
122
+ specify{ expect(subject.order([:field1, :field2]).criteria.sort).to eq([:field1, :field2]) }
123
+ specify{ expect(subject.order(field: :asc).criteria.sort).to eq([{ field: :asc }]) }
124
+ specify{ expect(
125
+ subject.order(field1: { order: :asc }, field2: :desc).order([:field3], :field4).criteria.sort
126
+ ).to eq([{field1: {order: :asc}}, {field2: :desc}, :field3, :field4]) }
127
+ end
128
+
129
+ describe '#reorder' do
130
+ specify{ expect(subject.reorder(field: 'hello')).to be_a(described_class) }
131
+ specify{ expect(subject.reorder(field: 'hello')).not_to eq(subject) }
132
+ specify{ expect{ subject.reorder(field: 'hello') }.not_to change{ subject.criteria.sort } }
133
+
134
+ specify{ expect(subject.order(:field1).reorder(:field2).criteria.sort).to eq([:field2]) }
135
+ specify{ expect(subject.order(:field1).reorder(:field2).order(:field3).criteria.sort).to eq([:field2, :field3]) }
136
+ specify{ expect(subject.order(:field1).reorder(:field2).reorder(:field3).criteria.sort).to eq([:field3]) }
137
+ end
138
+
139
+ describe '#only' do
140
+ specify{ expect(subject.only(:field)).to be_a described_class }
141
+ specify{ expect(subject.only(:field)).not_to eq(subject) }
142
+ specify{ expect{ subject.only(:field) }.not_to change{ subject.criteria.fields } }
143
+
144
+ specify{ expect(subject.only(:field1, :field2).criteria.fields).to match_array(['field1', 'field2']) }
145
+ specify{ expect(
146
+ subject.only([:field1, :field2]).only(:field3).criteria.fields
147
+ ).to match_array(['field1', 'field2', 'field3']) }
148
+ end
149
+
150
+ describe '#only!' do
151
+ specify{ expect(subject.only!(:field)).to be_a(described_class) }
152
+ specify{ expect(subject.only!(:field)).not_to eq(subject) }
153
+ specify{ expect{ subject.only!(:field) }.not_to change{ subject.criteria.fields } }
154
+
155
+ specify{ expect(subject.only!(:field1, :field2).criteria.fields).to match_array(['field1', 'field2']) }
156
+ specify{ expect(subject.only!([:field1, :field2]).only!(:field3).criteria.fields).to eq(['field3']) }
157
+ specify{ expect(subject.only([:field1, :field2]).only!(:field3).criteria.fields).to eq(['field3']) }
158
+ end
159
+
160
+ describe '#types' do
161
+ specify{ expect(subject.types(:product)).to be_a(described_class) }
162
+ specify{ expect(subject.types(:product)).not_to eq(subject) }
163
+ specify{ expect{ subject.types(:product) }.not_to change{ subject.criteria.types } }
164
+
165
+ specify{ expect(subject.types(:user).criteria.types).to eq(['user']) }
166
+ specify{ expect(subject.types(:product, :city).criteria.types).to match_array(['product', 'city']) }
167
+ specify{ expect(
168
+ subject.types([:product, :city]).types(:country).criteria.types
169
+ ).to match_array(['product', 'city', 'country']) }
170
+ end
171
+
172
+ describe '#types!' do
173
+ specify{ expect(subject.types!(:product)).to be_a(described_class) }
174
+ specify{ expect(subject.types!(:product)).not_to eq(subject) }
175
+ specify{ expect{ subject.types!(:product) }.not_to change{ subject.criteria.types } }
176
+
177
+ specify{ expect(subject.types!(:user).criteria.types).to eq(['user']) }
178
+ specify{ expect(subject.types!(:product, :city).criteria.types).to match_array(['product', 'city']) }
179
+ specify{ expect(subject.types!([:product, :city]).types!(:country).criteria.types).to eq(['country']) }
180
+ specify{ expect(subject.types([:product, :city]).types!(:country).criteria.types).to eq(['country']) }
181
+ end
182
+
183
+ describe '#none' do
184
+ specify{ expect(subject.none).to be_a(described_class) }
185
+ specify{ expect(subject.none).not_to eq(subject) }
186
+ specify{ expect(subject.none.criteria).to be_none }
187
+ end
188
+
189
+ describe '#merge' do
190
+ let(:query){ ChewyQuery::Builder.new(:products) }
191
+
192
+ specify{ expect(
193
+ subject.filter{ name == 'name' }.merge(query.filter{ age == 42 }).criteria.filters
194
+ ).to eq([{ term: { 'name' => 'name' } }, { term: { 'age' => 42 } }]) }
195
+ end
196
+ end
File without changes
@@ -0,0 +1,8 @@
1
+ require 'its'
2
+ require 'chewy_query'
3
+
4
+ Dir["./spec/support/**/*.rb"].each { |f| require f }
5
+
6
+ RSpec.configure do |config|
7
+ config.mock_with :rspec
8
+ end
metadata ADDED
@@ -0,0 +1,191 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chewy_query
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - pyromaniac
8
+ - undr
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-10-31 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '3.2'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '3.2'
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.6'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.6'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: its
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ description: The query builder for ElasticSearch which was extracted from Chewy.
85
+ email:
86
+ - kinwizard@gmail.com
87
+ - undr@yandex.ru
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - chewy_query.gemspec
99
+ - lib/chewy_query.rb
100
+ - lib/chewy_query/builder.rb
101
+ - lib/chewy_query/builder/compose.rb
102
+ - lib/chewy_query/builder/criteria.rb
103
+ - lib/chewy_query/builder/filters.rb
104
+ - lib/chewy_query/builder/nodes/and.rb
105
+ - lib/chewy_query/builder/nodes/base.rb
106
+ - lib/chewy_query/builder/nodes/bool.rb
107
+ - lib/chewy_query/builder/nodes/equal.rb
108
+ - lib/chewy_query/builder/nodes/exists.rb
109
+ - lib/chewy_query/builder/nodes/expr.rb
110
+ - lib/chewy_query/builder/nodes/field.rb
111
+ - lib/chewy_query/builder/nodes/has_child.rb
112
+ - lib/chewy_query/builder/nodes/has_parent.rb
113
+ - lib/chewy_query/builder/nodes/has_relation.rb
114
+ - lib/chewy_query/builder/nodes/match_all.rb
115
+ - lib/chewy_query/builder/nodes/missing.rb
116
+ - lib/chewy_query/builder/nodes/not.rb
117
+ - lib/chewy_query/builder/nodes/or.rb
118
+ - lib/chewy_query/builder/nodes/prefix.rb
119
+ - lib/chewy_query/builder/nodes/query.rb
120
+ - lib/chewy_query/builder/nodes/range.rb
121
+ - lib/chewy_query/builder/nodes/raw.rb
122
+ - lib/chewy_query/builder/nodes/regexp.rb
123
+ - lib/chewy_query/builder/nodes/script.rb
124
+ - lib/chewy_query/version.rb
125
+ - spec/chewy_query/builder/context_spec.rb
126
+ - spec/chewy_query/builder/filters_spec.rb
127
+ - spec/chewy_query/builder/nodes/and_spec.rb
128
+ - spec/chewy_query/builder/nodes/bool_spec.rb
129
+ - spec/chewy_query/builder/nodes/equal_spec.rb
130
+ - spec/chewy_query/builder/nodes/exists_spec.rb
131
+ - spec/chewy_query/builder/nodes/has_child_spec.rb
132
+ - spec/chewy_query/builder/nodes/has_parent_spec.rb
133
+ - spec/chewy_query/builder/nodes/match_all_spec.rb
134
+ - spec/chewy_query/builder/nodes/missing_spec.rb
135
+ - spec/chewy_query/builder/nodes/not_spec.rb
136
+ - spec/chewy_query/builder/nodes/or_spec.rb
137
+ - spec/chewy_query/builder/nodes/prefix_spec.rb
138
+ - spec/chewy_query/builder/nodes/query_spec.rb
139
+ - spec/chewy_query/builder/nodes/range_spec.rb
140
+ - spec/chewy_query/builder/nodes/raw_spec.rb
141
+ - spec/chewy_query/builder/nodes/regexp_spec.rb
142
+ - spec/chewy_query/builder/nodes/script_spec.rb
143
+ - spec/chewy_query/builder_spec.rb
144
+ - spec/chewy_query_spec.rb
145
+ - spec/spec_helper.rb
146
+ homepage: ''
147
+ licenses:
148
+ - MIT
149
+ metadata: {}
150
+ post_install_message:
151
+ rdoc_options: []
152
+ require_paths:
153
+ - lib
154
+ required_ruby_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ requirements: []
165
+ rubyforge_project:
166
+ rubygems_version: 2.2.2
167
+ signing_key:
168
+ specification_version: 4
169
+ summary: The query builder for ElasticSearch which was extracted from Chewy.
170
+ test_files:
171
+ - spec/chewy_query/builder/context_spec.rb
172
+ - spec/chewy_query/builder/filters_spec.rb
173
+ - spec/chewy_query/builder/nodes/and_spec.rb
174
+ - spec/chewy_query/builder/nodes/bool_spec.rb
175
+ - spec/chewy_query/builder/nodes/equal_spec.rb
176
+ - spec/chewy_query/builder/nodes/exists_spec.rb
177
+ - spec/chewy_query/builder/nodes/has_child_spec.rb
178
+ - spec/chewy_query/builder/nodes/has_parent_spec.rb
179
+ - spec/chewy_query/builder/nodes/match_all_spec.rb
180
+ - spec/chewy_query/builder/nodes/missing_spec.rb
181
+ - spec/chewy_query/builder/nodes/not_spec.rb
182
+ - spec/chewy_query/builder/nodes/or_spec.rb
183
+ - spec/chewy_query/builder/nodes/prefix_spec.rb
184
+ - spec/chewy_query/builder/nodes/query_spec.rb
185
+ - spec/chewy_query/builder/nodes/range_spec.rb
186
+ - spec/chewy_query/builder/nodes/raw_spec.rb
187
+ - spec/chewy_query/builder/nodes/regexp_spec.rb
188
+ - spec/chewy_query/builder/nodes/script_spec.rb
189
+ - spec/chewy_query/builder_spec.rb
190
+ - spec/chewy_query_spec.rb
191
+ - spec/spec_helper.rb