daedal 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +82 -0
- data/Guardfile +4 -0
- data/LICENSE +20 -0
- data/README.md +4 -0
- data/daedal.gemspec +14 -0
- data/lib/daedal.rb +8 -0
- data/lib/daedal/attributes.rb +94 -0
- data/lib/daedal/filters.rb +7 -0
- data/lib/daedal/filters/base_filter.rb +17 -0
- data/lib/daedal/filters/term_filter.rb +18 -0
- data/lib/daedal/queries.rb +13 -0
- data/lib/daedal/queries/base_query.rb +13 -0
- data/lib/daedal/queries/bool_query.rb +48 -0
- data/lib/daedal/queries/constant_score_query.rb +35 -0
- data/lib/daedal/queries/dis_max_query.rb +37 -0
- data/lib/daedal/queries/filtered_query.rb +20 -0
- data/lib/daedal/queries/match_all_query.rb +12 -0
- data/lib/daedal/queries/match_query.rb +33 -0
- data/lib/daedal/queries/multi_match_query.rb +43 -0
- data/spec/spec_helper.rb +16 -0
- data/spec/unit/daedal/filters/term_filter_spec.rb +55 -0
- data/spec/unit/daedal/queries/bool_query_spec.rb +214 -0
- data/spec/unit/daedal/queries/constant_score_query_spec.rb +72 -0
- data/spec/unit/daedal/queries/dis_max_query_spec.rb +151 -0
- data/spec/unit/daedal/queries/filtered_query_spec.rb +60 -0
- data/spec/unit/daedal/queries/match_all_query_spec.rb +19 -0
- data/spec/unit/daedal/queries/match_query_spec.rb +245 -0
- data/spec/unit/daedal/queries/multi_match_query_spec.rb +253 -0
- metadata +74 -0
@@ -0,0 +1,151 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'daedal/queries'
|
3
|
+
|
4
|
+
describe Daedal::Queries::DisMaxQuery do
|
5
|
+
|
6
|
+
subject do
|
7
|
+
Daedal::Queries::DisMaxQuery
|
8
|
+
end
|
9
|
+
|
10
|
+
let(:match_query) do
|
11
|
+
Daedal::Queries::MatchQuery
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:hash_query) do
|
15
|
+
{dis_max: {queries: []}}
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'with no initial queries specified' do
|
19
|
+
let(:query) do
|
20
|
+
subject.new
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'will give an empty dis max query' do
|
24
|
+
expect(query.queries).to eq Array.new
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'will have the correct hash representation' do
|
28
|
+
expect(query.to_hash).to eq hash_query
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'will have the correct json representation' do
|
32
|
+
expect(query.to_json).to eq hash_query.to_json
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'with a tie breaker specified' do
|
36
|
+
before do
|
37
|
+
hash_query[:dis_max][:tie_breaker] = 2.0
|
38
|
+
end
|
39
|
+
let(:query_with_min) do
|
40
|
+
subject.new(tie_breaker: 2)
|
41
|
+
end
|
42
|
+
it 'will set the tie_breaker parameter' do
|
43
|
+
expect(query_with_min.tie_breaker).to eq 2.0
|
44
|
+
end
|
45
|
+
it 'will have the correct hash representation' do
|
46
|
+
expect(query_with_min.to_hash).to eq hash_query
|
47
|
+
end
|
48
|
+
it 'will have the correct json representation' do
|
49
|
+
expect(query_with_min.to_json).to eq hash_query.to_json
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'with a boost specified' do
|
54
|
+
before do
|
55
|
+
hash_query[:dis_max][:boost] = 2
|
56
|
+
end
|
57
|
+
let(:query_with_boost) do
|
58
|
+
subject.new(boost: 2)
|
59
|
+
end
|
60
|
+
it 'will set the boost parameter' do
|
61
|
+
expect(query_with_boost.boost).to eq 2
|
62
|
+
end
|
63
|
+
it 'will have the correct hash representation' do
|
64
|
+
expect(query_with_boost.to_hash).to eq hash_query
|
65
|
+
end
|
66
|
+
it 'will have the correct json representation' do
|
67
|
+
expect(query_with_boost.to_json).to eq hash_query.to_json
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'with initial array of queries specified' do
|
73
|
+
|
74
|
+
let(:queries) do
|
75
|
+
[match_query.new(field: :a, query: :b), match_query.new(field: :c, query: :d)]
|
76
|
+
end
|
77
|
+
|
78
|
+
let(:query) do
|
79
|
+
subject.new(queries: queries)
|
80
|
+
end
|
81
|
+
|
82
|
+
before do
|
83
|
+
hash_query[:dis_max][:queries] = queries.map {|q| q.to_hash}
|
84
|
+
end
|
85
|
+
|
86
|
+
it 'will create a dis_max query with the appropriate initial array of queries' do
|
87
|
+
expect(query.queries).to eq queries
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'will have the correct hash representation' do
|
91
|
+
expect(query.to_hash).to eq hash_query
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'will have the correct json representation' do
|
95
|
+
expect(query.to_json).to eq hash_query.to_json
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
context 'with an initial array of non queries specified' do
|
100
|
+
it 'will raise an error' do
|
101
|
+
expect {subject.new(queries: [:foo])}.to raise_error
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
context 'with a query (not in an array) specified' do
|
106
|
+
let(:mq) do
|
107
|
+
match_query.new(field: :a, query: :b)
|
108
|
+
end
|
109
|
+
|
110
|
+
let(:query) do
|
111
|
+
subject.new(queries: mq)
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'will convert the input into an array of the single query' do
|
115
|
+
expect(query.queries).to eq([mq])
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
context 'when adding more queries' do
|
120
|
+
let(:query) do
|
121
|
+
subject.new
|
122
|
+
end
|
123
|
+
let(:mq) do
|
124
|
+
match_query.new(field: :a, query: :b)
|
125
|
+
end
|
126
|
+
|
127
|
+
context 'with the #add_query method' do
|
128
|
+
before do
|
129
|
+
query.add_query mq
|
130
|
+
end
|
131
|
+
it 'will add a query' do
|
132
|
+
expect(query.queries).to eq [mq]
|
133
|
+
end
|
134
|
+
|
135
|
+
context 'twice' do
|
136
|
+
before do
|
137
|
+
query.add_query mq
|
138
|
+
end
|
139
|
+
it 'will append the second query' do
|
140
|
+
expect(query.queries).to eq [mq, mq]
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
context 'with a non-valid query' do
|
145
|
+
it 'will raise an error' do
|
146
|
+
expect{query.add_query :foo}.to raise_error
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'daedal/queries'
|
3
|
+
|
4
|
+
describe Daedal::Queries::FilteredQuery do
|
5
|
+
|
6
|
+
subject do
|
7
|
+
Daedal::Queries::FilteredQuery
|
8
|
+
end
|
9
|
+
|
10
|
+
let(:match_query) do
|
11
|
+
Daedal::Queries::MatchQuery.new(field: :foo, query: :bar)
|
12
|
+
end
|
13
|
+
|
14
|
+
let(:term_filter) do
|
15
|
+
Daedal::Filters::TermFilter.new(field: :foo, term: :bar)
|
16
|
+
end
|
17
|
+
|
18
|
+
let(:hash_query) do
|
19
|
+
{filtered: {query: match_query.to_hash, filter: term_filter.to_hash}}
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'with no query or filter specified' do
|
23
|
+
|
24
|
+
let(:base_filter) do
|
25
|
+
Daedal::Filters::BaseFilter.new
|
26
|
+
end
|
27
|
+
let(:match_all_query) do
|
28
|
+
Daedal::Queries::MatchAllQuery.new
|
29
|
+
end
|
30
|
+
let(:query) do
|
31
|
+
subject.new
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'will create an empty filtered query with a match_all query and a blank filter' do
|
35
|
+
expect(query.query.to_hash).to eq match_all_query.to_hash
|
36
|
+
expect(query.filter.to_hash).to eq base_filter.to_hash
|
37
|
+
end
|
38
|
+
it 'will have the correct hash representation' do
|
39
|
+
expect(query.to_hash).to eq({filtered: {query: {match_all: {}}, filter: {}}})
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'with a query and a filter specified' do
|
44
|
+
let(:query) do
|
45
|
+
subject.new(query: match_query, filter: term_filter)
|
46
|
+
end
|
47
|
+
|
48
|
+
it 'will create a filtered query with the correct values' do
|
49
|
+
expect(query.query).to eq match_query
|
50
|
+
expect(query.filter).to eq term_filter
|
51
|
+
end
|
52
|
+
it 'will have the correct hash representation' do
|
53
|
+
expect(query.to_hash).to eq hash_query
|
54
|
+
end
|
55
|
+
it 'will have the correct json representation' do
|
56
|
+
expect(query.to_json).to eq hash_query.to_json
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'daedal/queries'
|
3
|
+
|
4
|
+
describe Daedal::Queries::MatchAllQuery do
|
5
|
+
|
6
|
+
subject do
|
7
|
+
Daedal::Queries::MatchAllQuery.new
|
8
|
+
end
|
9
|
+
|
10
|
+
context 'when calling #new' do
|
11
|
+
it 'will have the correct hash representation' do
|
12
|
+
expect(subject.to_hash).to eq({match_all: {}})
|
13
|
+
end
|
14
|
+
it 'will have the correct json representation' do
|
15
|
+
expect(subject.to_json).to eq({match_all: {}}.to_json)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
@@ -0,0 +1,245 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'daedal/queries'
|
3
|
+
|
4
|
+
describe Daedal::Queries::MatchQuery do
|
5
|
+
|
6
|
+
subject do
|
7
|
+
Daedal::Queries::MatchQuery
|
8
|
+
end
|
9
|
+
|
10
|
+
let(:base_query) do
|
11
|
+
{match: {foo: {query: :bar}}}
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'without a field or term given' do
|
15
|
+
it 'will raise an error' do
|
16
|
+
expect {subject.new}.to raise_error
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'without a term given' 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 a field and term given' do
|
27
|
+
|
28
|
+
let(:match_query) do
|
29
|
+
subject.new(field: :foo, query: :bar)
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'will create a match query object that has the correct field and term' do
|
33
|
+
expect(match_query.field).to eq :foo
|
34
|
+
expect(match_query.query).to eq :bar
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'will have the other options set to nil' do
|
38
|
+
expect(match_query.minimum_should_match).to eq nil
|
39
|
+
expect(match_query.cutoff_frequency).to eq nil
|
40
|
+
expect(match_query.type).to eq nil
|
41
|
+
expect(match_query.analyzer).to eq nil
|
42
|
+
expect(match_query.boost).to eq nil
|
43
|
+
expect(match_query.fuzziness).to eq nil
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'will have the correct hash representation' do
|
47
|
+
expect(match_query.to_hash).to eq base_query
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'will have the correct json representation' do
|
51
|
+
expect(match_query.to_json).to eq base_query.to_json
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context "with an operator of :and specified" do
|
56
|
+
|
57
|
+
let(:match_query) do
|
58
|
+
subject.new(field: :foo, query: :bar, operator: :and)
|
59
|
+
end
|
60
|
+
|
61
|
+
before do
|
62
|
+
base_query[:match][:foo][:operator] = :and
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'will set the operator to :and' do
|
66
|
+
expect(match_query.operator).to eq :and
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'will have the correct hash representation' do
|
70
|
+
expect(match_query.to_hash).to eq base_query
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'will have the correct json representation' do
|
74
|
+
expect(match_query.to_json).to eq base_query.to_json
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'with a non-valid operator specified' do
|
79
|
+
it 'will raise an error' do
|
80
|
+
expect {subject.new(field: :foo, query: :bar, operator: :foo)}.to raise_error
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context 'with a phrase type specified' do
|
85
|
+
let(:match_query) do
|
86
|
+
subject.new(field: :foo, query: :bar, type: :phrase)
|
87
|
+
end
|
88
|
+
|
89
|
+
before do
|
90
|
+
base_query[:match][:foo][:type] = :phrase
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'will set the phrase type to :phrase' do
|
94
|
+
expect(match_query.type).to eq :phrase
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'will have the correct hash representation' do
|
98
|
+
expect(match_query.to_hash).to eq base_query
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'will have the correct json representation' do
|
102
|
+
expect(match_query.to_json).to eq base_query.to_json
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context 'with a non-valid type specified' do
|
107
|
+
it 'will raise an error' do
|
108
|
+
expect {subject.new(field: :foo, query: :bar, type: :foo)}.to raise_error
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
context 'with a minimum should match of 2 specified' do
|
113
|
+
let(:match_query) do
|
114
|
+
subject.new(field: :foo, query: :bar, minimum_should_match: 2)
|
115
|
+
end
|
116
|
+
|
117
|
+
before do
|
118
|
+
base_query[:match][:foo][:minimum_should_match] = 2
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'will set the phrase type to :phrase' do
|
122
|
+
expect(match_query.minimum_should_match).to eq 2
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'will have the correct hash representation' do
|
126
|
+
expect(match_query.to_hash).to eq base_query
|
127
|
+
end
|
128
|
+
|
129
|
+
it 'will have the correct json representation' do
|
130
|
+
expect(match_query.to_json).to eq base_query.to_json
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
context 'with a non-integer minimum should match specified' do
|
135
|
+
it 'will raise an error' do
|
136
|
+
expect {subject.new(field: :foo, query: :bar, minimum_should_match: 'foo')}.to raise_error
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
context 'with a cutoff frequency of 0.5 specified' do
|
141
|
+
let(:match_query) do
|
142
|
+
subject.new(field: :foo, query: :bar, cutoff_frequency: 0.5)
|
143
|
+
end
|
144
|
+
|
145
|
+
before do
|
146
|
+
base_query[:match][:foo][:cutoff_frequency] = 0.5
|
147
|
+
end
|
148
|
+
|
149
|
+
it 'will set the phrase type to :phrase' do
|
150
|
+
expect(match_query.cutoff_frequency).to eq 0.5
|
151
|
+
end
|
152
|
+
|
153
|
+
it 'will have the correct hash representation' do
|
154
|
+
expect(match_query.to_hash).to eq base_query
|
155
|
+
end
|
156
|
+
|
157
|
+
it 'will have the correct json representation' do
|
158
|
+
expect(match_query.to_json).to eq base_query.to_json
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
context 'with a non-float cutoff frequency specified' do
|
163
|
+
it 'will raise an error' do
|
164
|
+
expect {subject.new(field: :foo, query: :bar, cutoff_frequency: 'foo')}.to raise_error
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
context 'with an analyzer of :foo specified' do
|
169
|
+
let(:match_query) do
|
170
|
+
subject.new(field: :foo, query: :bar, analyzer: :foo)
|
171
|
+
end
|
172
|
+
|
173
|
+
before do
|
174
|
+
base_query[:match][:foo][:analyzer] = :foo
|
175
|
+
end
|
176
|
+
|
177
|
+
it 'will set the phrase type to :phrase' do
|
178
|
+
expect(match_query.analyzer).to eq :foo
|
179
|
+
end
|
180
|
+
|
181
|
+
it 'will have the correct hash representation' do
|
182
|
+
expect(match_query.to_hash).to eq base_query
|
183
|
+
end
|
184
|
+
|
185
|
+
it 'will have the correct json representation' do
|
186
|
+
expect(match_query.to_json).to eq base_query.to_json
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
context 'with a boost of 2 specified' do
|
191
|
+
let(:match_query) do
|
192
|
+
subject.new(field: :foo, query: :bar, boost: 2)
|
193
|
+
end
|
194
|
+
|
195
|
+
before do
|
196
|
+
base_query[:match][:foo][:boost] = 2
|
197
|
+
end
|
198
|
+
|
199
|
+
it 'will set the phrase type to :phrase' do
|
200
|
+
expect(match_query.boost).to eq 2
|
201
|
+
end
|
202
|
+
|
203
|
+
it 'will have the correct hash representation' do
|
204
|
+
expect(match_query.to_hash).to eq base_query
|
205
|
+
end
|
206
|
+
|
207
|
+
it 'will have the correct json representation' do
|
208
|
+
expect(match_query.to_json).to eq base_query.to_json
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
context 'with a non integer boost specified' do
|
213
|
+
it 'will raise an error' do
|
214
|
+
expect {subject.new(field: :foo, query: :bar, boost: 'foo')}.to raise_error
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
context 'with a fuzziness of 0.5 specified' do
|
219
|
+
let(:match_query) do
|
220
|
+
subject.new(field: :foo, query: :bar, fuzziness: 0.5)
|
221
|
+
end
|
222
|
+
|
223
|
+
before do
|
224
|
+
base_query[:match][:foo][:fuzziness] = 0.5
|
225
|
+
end
|
226
|
+
|
227
|
+
it 'will set the phrase type to :phrase' do
|
228
|
+
expect(match_query.fuzziness).to eq 0.5
|
229
|
+
end
|
230
|
+
|
231
|
+
it 'will have the correct hash representation' do
|
232
|
+
expect(match_query.to_hash).to eq base_query
|
233
|
+
end
|
234
|
+
|
235
|
+
it 'will have the correct json representation' do
|
236
|
+
expect(match_query.to_json).to eq base_query.to_json
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
context 'with a non float or integer fuzziness specified' do
|
241
|
+
it 'will raise an error' do
|
242
|
+
expect {subject.new(field: :foo, query: :bar, fuzziness: 'foo')}.to raise_error
|
243
|
+
end
|
244
|
+
end
|
245
|
+
end
|