daedal 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +1 -1
- data/Gemfile.lock +1 -1
- data/README.md +29 -1
- data/daedal.gemspec +1 -1
- data/lib/daedal/attributes.rb +5 -0
- data/lib/daedal/attributes/distance_unit.rb +15 -0
- data/lib/daedal/attributes/filter.rb +13 -0
- data/lib/daedal/attributes/filter_array.rb +24 -0
- data/lib/daedal/attributes/lower_case_string.rb +14 -0
- data/lib/daedal/attributes/query.rb +13 -0
- data/lib/daedal/attributes/query_array.rb +2 -0
- data/lib/daedal/filters.rb +3 -0
- data/lib/daedal/filters/and_filter.rb +21 -0
- data/lib/daedal/filters/base_filter.rb +1 -1
- data/lib/daedal/filters/geo_distance_filter.rb +27 -0
- data/lib/daedal/filters/range_filter.rb +27 -0
- data/lib/daedal/queries.rb +1 -0
- data/lib/daedal/queries/bool_query.rb +5 -4
- data/lib/daedal/queries/constant_score_query.rb +2 -2
- data/lib/daedal/queries/dis_max_query.rb +3 -2
- data/lib/daedal/queries/filtered_query.rb +2 -2
- data/lib/daedal/queries/match_query.rb +2 -1
- data/lib/daedal/queries/nested_query.rb +4 -4
- data/lib/daedal/queries/prefix_query.rb +25 -0
- data/lib/daedal/version.rb +3 -0
- data/spec/unit/daedal/filters/and_filter_spec.rb +87 -0
- data/spec/unit/daedal/filters/geo_distance_filter_spec.rb +104 -0
- data/spec/unit/daedal/filters/range_filter_spec.rb +90 -0
- data/spec/unit/daedal/filters/term_filter_spec.rb +3 -3
- data/spec/unit/daedal/filters/terms_filter_spec.rb +2 -2
- data/spec/unit/daedal/queries/bool_query_spec.rb +42 -4
- data/spec/unit/daedal/queries/constant_score_query_spec.rb +3 -3
- data/spec/unit/daedal/queries/dis_max_query_spec.rb +18 -2
- data/spec/unit/daedal/queries/filtered_query_spec.rb +3 -3
- data/spec/unit/daedal/queries/match_query_spec.rb +36 -8
- data/spec/unit/daedal/queries/multi_match_query_spec.rb +8 -8
- data/spec/unit/daedal/queries/nested_query_spec.rb +4 -4
- data/spec/unit/daedal/queries/prefix_query_spec.rb +85 -0
- metadata +17 -3
@@ -0,0 +1,87 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'daedal/filters'
|
3
|
+
|
4
|
+
describe Daedal::Filters::AndFilter do
|
5
|
+
|
6
|
+
subject do
|
7
|
+
Daedal::Filters::AndFilter
|
8
|
+
end
|
9
|
+
|
10
|
+
let(:term_filter) do
|
11
|
+
Daedal::Filters::TermFilter.new field: :foo, term: :bar
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'without an initial array of filters' do
|
15
|
+
let(:filter) do
|
16
|
+
subject.new
|
17
|
+
end
|
18
|
+
let(:hash_filter) do
|
19
|
+
nil
|
20
|
+
end
|
21
|
+
it 'will have an empty array of filters set' do
|
22
|
+
expect(filter.filters).to eq []
|
23
|
+
end
|
24
|
+
it 'will have the correct hash and json representations' do
|
25
|
+
expect(filter.to_hash).to eq hash_filter
|
26
|
+
expect(filter.to_json).to eq hash_filter.to_json
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'with an initial array of filters' do
|
31
|
+
let(:filter) do
|
32
|
+
subject.new filters: [term_filter]
|
33
|
+
end
|
34
|
+
let(:hash_filter) do
|
35
|
+
{:and => [term_filter.to_hash]}
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'will have the correct filter array set' do
|
39
|
+
expect(filter.filters).to eq [term_filter]
|
40
|
+
end
|
41
|
+
it 'will have the correct hash and json representations' do
|
42
|
+
expect(filter.to_hash).to eq hash_filter
|
43
|
+
expect(filter.to_json).to eq hash_filter.to_json
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'when adding more filters' do
|
48
|
+
let(:filter) do
|
49
|
+
subject.new
|
50
|
+
end
|
51
|
+
let(:hash_filter) do
|
52
|
+
{:and => [term_filter.to_hash]}
|
53
|
+
end
|
54
|
+
|
55
|
+
before do
|
56
|
+
filter.filters << term_filter
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'will append the filter' do
|
60
|
+
expect(filter.filters).to eq [term_filter]
|
61
|
+
end
|
62
|
+
it 'will end up with the correct hash and json representations' do
|
63
|
+
expect(filter.to_hash).to eq hash_filter
|
64
|
+
expect(filter.to_json).to eq hash_filter.to_json
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'twice' do
|
68
|
+
before do
|
69
|
+
filter.filters << term_filter
|
70
|
+
hash_filter[:and] << term_filter.to_hash
|
71
|
+
end
|
72
|
+
it 'will append the second filter' do
|
73
|
+
expect(filter.filters).to eq [term_filter, term_filter]
|
74
|
+
end
|
75
|
+
it 'will still have the correct hasha nd json representations' do
|
76
|
+
expect(filter.to_hash).to eq hash_filter
|
77
|
+
expect(filter.to_json).to eq hash_filter.to_json
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context 'when adding an invalid filter' do
|
82
|
+
it 'will raise an error' do
|
83
|
+
expect{filter.filters << :foo}.to raise_error(Virtus::CoercionError)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'daedal/filters'
|
3
|
+
|
4
|
+
describe Daedal::Filters::GeoDistanceFilter do
|
5
|
+
|
6
|
+
subject do
|
7
|
+
Daedal::Filters::GeoDistanceFilter
|
8
|
+
end
|
9
|
+
|
10
|
+
context 'without a field specified' do
|
11
|
+
it 'will raise an error' do
|
12
|
+
expect{subject.new(distance: 5, lat: 10, lon: 30)}.to raise_error(Virtus::CoercionError)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'without a distance specified' do
|
17
|
+
it 'will raise an error' do
|
18
|
+
expect{subject.new(field: :foo, lat: 10, lon: 30)}.to raise_error(Virtus::CoercionError)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'without a lat specified' do
|
23
|
+
it 'will raise an error' do
|
24
|
+
expect{subject.new(field: :foo, distance: 10, lon: 30)}.to raise_error(Virtus::CoercionError)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'without a lon specified' do
|
29
|
+
it 'will raise an error' do
|
30
|
+
expect{subject.new(field: :foo, lat: 10, distance: 30)}.to raise_error(Virtus::CoercionError)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'with a field, distance, lat, and lon specified' do
|
35
|
+
let(:query) do
|
36
|
+
subject.new(field: :foo, distance: 5, lat: 10, lon: 30)
|
37
|
+
end
|
38
|
+
let(:hash_query) do
|
39
|
+
{geo_distance: {distance: '5.0km', foo: {lat: 10.0, lon: 30.0}}}
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'will set the field and distance appropriately' do
|
43
|
+
expect(query.field).to eq :foo
|
44
|
+
expect(query.distance).to eq 5.0
|
45
|
+
expect(query.lat).to eq 10.0
|
46
|
+
expect(query.lon).to eq 30.0
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'will use km as the default unit' do
|
50
|
+
expect(query.unit).to eq 'km'
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'will have the correct hash and json representations' do
|
54
|
+
expect(query.to_hash).to eq hash_query
|
55
|
+
expect(query.to_json).to eq hash_query.to_json
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'with a distance unit specified' do
|
60
|
+
let(:query) do
|
61
|
+
subject.new(field: :foo, distance: 5, lat: 10, lon: 30, unit: 'mi')
|
62
|
+
end
|
63
|
+
let(:hash_query) do
|
64
|
+
{geo_distance: {distance: '5.0mi', foo: {lat: 10.0, lon: 30.0}}}
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'will set the field and distance appropriately' do
|
68
|
+
expect(query.field).to eq :foo
|
69
|
+
expect(query.distance).to eq 5.0
|
70
|
+
expect(query.lat).to eq 10.0
|
71
|
+
expect(query.lon).to eq 30.0
|
72
|
+
expect(query.unit).to eq 'mi'
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'will have the correct hash and json representations' do
|
76
|
+
expect(query.to_hash).to eq hash_query
|
77
|
+
expect(query.to_json).to eq hash_query.to_json
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
context 'with an invalid unit specified' do
|
82
|
+
it 'will raise an error' do
|
83
|
+
expect{subject.new(field: :foo, distance: 5, lat: 10, lon: 30, unit: 'foo')}.to raise_error(Virtus::CoercionError)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context 'with an invalid lat specified' do
|
88
|
+
it 'will raise an error' do
|
89
|
+
expect{subject.new(field: :foo, distance: 5, lat: 'foo', lon: 30)}.to raise_error(Virtus::CoercionError)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
context 'with an invalid lon specified' do
|
94
|
+
it 'will raise an error' do
|
95
|
+
expect{subject.new(field: :foo, distance: 5, lat: 10, lon: 'foo')}.to raise_error(Virtus::CoercionError)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
context 'with an invalid distance specified' do
|
100
|
+
it 'will raise an error' do
|
101
|
+
expect{subject.new(field: :foo, distance: 'foo', lat: 10, lon: 30)}.to raise_error(Virtus::CoercionError)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'daedal/filters'
|
3
|
+
|
4
|
+
describe Daedal::Filters::RangeFilter do
|
5
|
+
|
6
|
+
subject do
|
7
|
+
Daedal::Filters::RangeFilter
|
8
|
+
end
|
9
|
+
|
10
|
+
let(:hash_filter) do
|
11
|
+
{range: {foo: {gte: 1, lte: 2}}}
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'without a field specified' do
|
15
|
+
it 'will raise an error' do
|
16
|
+
expect{subject.new(gte: 1, lte: 2)}.to raise_error(Virtus::CoercionError)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'without a gte, lte, lt, or gt specified' do
|
21
|
+
it 'will raise an error' do
|
22
|
+
expect{subject.new(field: :foo)}.to raise_error
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'with number gte and lte' do
|
27
|
+
let(:filter) do
|
28
|
+
subject.new(field: :foo, gte: 1, lte: 2)
|
29
|
+
end
|
30
|
+
it 'will create a range filter with the appropriate fields' do
|
31
|
+
expect(filter.field).to eq :foo
|
32
|
+
expect(filter.gte).to eq 1
|
33
|
+
expect(filter.lte).to eq 2
|
34
|
+
end
|
35
|
+
it 'will have the correct hash and json representations' do
|
36
|
+
expect(filter.to_hash).to eq hash_filter
|
37
|
+
expect(filter.to_json).to eq hash_filter.to_json
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'with string gte and lte' do
|
42
|
+
let(:filter) do
|
43
|
+
subject.new(field: :foo, gte: 'a', lte: 'b')
|
44
|
+
end
|
45
|
+
before do
|
46
|
+
hash_filter[:range][:foo][:gte] = 'a'
|
47
|
+
hash_filter[:range][:foo][:lte] = 'b'
|
48
|
+
end
|
49
|
+
it 'will create a range filter with the appropriate fields' do
|
50
|
+
expect(filter.field).to eq :foo
|
51
|
+
expect(filter.gte).to eq 'a'
|
52
|
+
expect(filter.lte).to eq 'b'
|
53
|
+
end
|
54
|
+
it 'will have the correct hash and json representations' do
|
55
|
+
expect(filter.to_hash).to eq hash_filter
|
56
|
+
expect(filter.to_json).to eq hash_filter.to_json
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context 'with number gt and lt' do
|
61
|
+
let(:filter) do
|
62
|
+
subject.new(field: :foo, gt: 1, lt: 2)
|
63
|
+
end
|
64
|
+
let(:hash_filter) do
|
65
|
+
{range: {foo: {lt: 2, gt: 1}}}
|
66
|
+
end
|
67
|
+
it 'will have the correct hash and json representations' do
|
68
|
+
expect(filter.to_hash).to eq hash_filter
|
69
|
+
expect(filter.to_json).to eq hash_filter.to_json
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'with string gt and lt' do
|
74
|
+
let(:filter) do
|
75
|
+
subject.new(field: :foo, gt: 'a', lt: 'b')
|
76
|
+
end
|
77
|
+
let(:hash_filter) do
|
78
|
+
{range: {foo: {lt: 'b', gt: 'a'}}}
|
79
|
+
end
|
80
|
+
it 'will create a range filter with the appropriate fields' do
|
81
|
+
expect(filter.field).to eq :foo
|
82
|
+
expect(filter.gt).to eq 'a'
|
83
|
+
expect(filter.lt).to eq 'b'
|
84
|
+
end
|
85
|
+
it 'will have the correct hash and json representations' do
|
86
|
+
expect(filter.to_hash).to eq hash_filter
|
87
|
+
expect(filter.to_json).to eq hash_filter.to_json
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -21,19 +21,19 @@ describe Daedal::Filters::TermFilter do
|
|
21
21
|
|
22
22
|
context 'without a field or a term specified' do
|
23
23
|
it 'will raise an error' do
|
24
|
-
expect {subject.new}.to raise_error
|
24
|
+
expect {subject.new}.to raise_error(Virtus::CoercionError)
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
28
|
context 'without a field specified' do
|
29
29
|
it 'will raise an error' do
|
30
|
-
expect {subject.new(term: term)}.to raise_error
|
30
|
+
expect {subject.new(term: term)}.to raise_error(Virtus::CoercionError)
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
34
|
context 'without a term specified' do
|
35
35
|
it 'will raise an error' do
|
36
|
-
expect {subject.new(field: field)}.to raise_error
|
36
|
+
expect {subject.new(field: field)}.to raise_error(Virtus::CoercionError)
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
@@ -21,13 +21,13 @@ describe Daedal::Filters::TermsFilter do
|
|
21
21
|
|
22
22
|
context 'without a field or a list of terms specified' do
|
23
23
|
it 'will raise an error' do
|
24
|
-
expect {subject.new}.to raise_error
|
24
|
+
expect {subject.new}.to raise_error(Virtus::CoercionError)
|
25
25
|
end
|
26
26
|
end
|
27
27
|
|
28
28
|
context 'without a field specified' do
|
29
29
|
it 'will raise an error' do
|
30
|
-
expect {subject.new(terms: terms)}.to raise_error
|
30
|
+
expect {subject.new(terms: terms)}.to raise_error(Virtus::CoercionError)
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
@@ -111,7 +111,45 @@ describe Daedal::Queries::BoolQuery do
|
|
111
111
|
|
112
112
|
context 'with an initial array of non queries specified' do
|
113
113
|
it 'will raise an error' do
|
114
|
-
expect {subject.new(should: [:foo])}.to raise_error
|
114
|
+
expect {subject.new(should: [:foo])}.to raise_error(Virtus::CoercionError)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
context 'with disable_coord specified' do
|
119
|
+
let(:query) do
|
120
|
+
subject.new(disable_coord: false)
|
121
|
+
end
|
122
|
+
before do
|
123
|
+
hash_query[:bool][:disable_coord] = false
|
124
|
+
end
|
125
|
+
it 'will set disable_coord properly' do
|
126
|
+
expect(query.disable_coord).to eq false
|
127
|
+
end
|
128
|
+
it 'will have the correct hash and json representations' do
|
129
|
+
expect(query.to_hash).to eq hash_query
|
130
|
+
expect(query.to_json).to eq hash_query.to_json
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
context 'with name specified' do
|
135
|
+
let(:query) do
|
136
|
+
subject.new(name: :foo)
|
137
|
+
end
|
138
|
+
before do
|
139
|
+
hash_query[:bool][:_name] = :foo
|
140
|
+
end
|
141
|
+
it 'will set name properly' do
|
142
|
+
expect(query.name).to eq :foo
|
143
|
+
end
|
144
|
+
it 'will have the correct hash and json representations' do
|
145
|
+
expect(query.to_hash).to eq hash_query
|
146
|
+
expect(query.to_json).to eq hash_query.to_json
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
context 'with a non boolean name specified' do
|
151
|
+
it 'will raise an error' do
|
152
|
+
expect{subject.new(name: [])}.to raise_error(Virtus::CoercionError)
|
115
153
|
end
|
116
154
|
end
|
117
155
|
|
@@ -156,7 +194,7 @@ describe Daedal::Queries::BoolQuery do
|
|
156
194
|
|
157
195
|
context 'with a non-valid query' do
|
158
196
|
it 'will raise an error' do
|
159
|
-
expect{query.should << :foo}.to raise_error
|
197
|
+
expect{query.should << :foo}.to raise_error(Virtus::CoercionError)
|
160
198
|
end
|
161
199
|
end
|
162
200
|
end
|
@@ -181,7 +219,7 @@ describe Daedal::Queries::BoolQuery do
|
|
181
219
|
|
182
220
|
context 'with a non-valid query' do
|
183
221
|
it 'will raise an error' do
|
184
|
-
expect {query.must << :foo}.to raise_error
|
222
|
+
expect {query.must << :foo}.to raise_error(Virtus::CoercionError)
|
185
223
|
end
|
186
224
|
end
|
187
225
|
|
@@ -207,7 +245,7 @@ describe Daedal::Queries::BoolQuery do
|
|
207
245
|
|
208
246
|
context 'with a non-valid query' do
|
209
247
|
it 'will raise an error' do
|
210
|
-
expect {query.must_not << :foo}.to raise_error
|
248
|
+
expect {query.must_not << :foo}.to raise_error(Virtus::CoercionError)
|
211
249
|
end
|
212
250
|
end
|
213
251
|
end
|
@@ -32,19 +32,19 @@ describe Daedal::Queries::ConstantScoreQuery do
|
|
32
32
|
|
33
33
|
context 'without a boost specified' do
|
34
34
|
it 'will raise an error' do
|
35
|
-
expect{subject.new(query: match_query)}.to raise_error
|
35
|
+
expect{subject.new(query: match_query)}.to raise_error(Virtus::CoercionError)
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
39
|
context 'with a boost but an invalid query specified' do
|
40
40
|
it 'will raise an error' do
|
41
|
-
expect {subject.new(boost: 5, query: :foo)}.to raise_error
|
41
|
+
expect {subject.new(boost: 5, query: :foo)}.to raise_error(Virtus::CoercionError)
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
45
45
|
context 'with a boost but an invalid filter specified' do
|
46
46
|
it 'will raise an error' do
|
47
|
-
expect {subject.new(boost: 5, filter: :foo)}.to raise_error
|
47
|
+
expect {subject.new(boost: 5, filter: :foo)}.to raise_error(Virtus::CoercionError)
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
@@ -98,7 +98,7 @@ describe Daedal::Queries::DisMaxQuery do
|
|
98
98
|
|
99
99
|
context 'with an initial array of non queries specified' do
|
100
100
|
it 'will raise an error' do
|
101
|
-
expect {subject.new(queries: [:foo])}.to raise_error
|
101
|
+
expect {subject.new(queries: [:foo])}.to raise_error(Virtus::CoercionError)
|
102
102
|
end
|
103
103
|
end
|
104
104
|
|
@@ -116,6 +116,22 @@ describe Daedal::Queries::DisMaxQuery do
|
|
116
116
|
end
|
117
117
|
end
|
118
118
|
|
119
|
+
context 'with name specified' do
|
120
|
+
let(:query) do
|
121
|
+
subject.new(name: :foo)
|
122
|
+
end
|
123
|
+
before do
|
124
|
+
hash_query[:dis_max][:_name] = :foo
|
125
|
+
end
|
126
|
+
it 'will set name properly' do
|
127
|
+
expect(query.name).to eq :foo
|
128
|
+
end
|
129
|
+
it 'will have the correct hash and json representations' do
|
130
|
+
expect(query.to_hash).to eq hash_query
|
131
|
+
expect(query.to_json).to eq hash_query.to_json
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
119
135
|
context 'when adding more queries' do
|
120
136
|
let(:query) do
|
121
137
|
subject.new
|
@@ -143,7 +159,7 @@ describe Daedal::Queries::DisMaxQuery do
|
|
143
159
|
|
144
160
|
context 'with a non-valid query' do
|
145
161
|
it 'will raise an error' do
|
146
|
-
expect{query.queries << :foo}.to raise_error
|
162
|
+
expect{query.queries << :foo}.to raise_error(Virtus::CoercionError)
|
147
163
|
end
|
148
164
|
end
|
149
165
|
end
|