tire-erez 0.5.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +14 -0
- data/.travis.yml +32 -0
- data/.yardopts +1 -0
- data/Gemfile +10 -0
- data/MIT-LICENSE +20 -0
- data/README.markdown +775 -0
- data/Rakefile +51 -0
- data/examples/rails-application-template.rb +263 -0
- data/examples/tire-dsl.rb +932 -0
- data/lib/tire.rb +59 -0
- data/lib/tire/alias.rb +296 -0
- data/lib/tire/configuration.rb +38 -0
- data/lib/tire/count.rb +85 -0
- data/lib/tire/dsl.rb +114 -0
- data/lib/tire/http/client.rb +62 -0
- data/lib/tire/http/clients/curb.rb +61 -0
- data/lib/tire/http/clients/faraday.rb +71 -0
- data/lib/tire/http/response.rb +27 -0
- data/lib/tire/index.rb +443 -0
- data/lib/tire/logger.rb +60 -0
- data/lib/tire/model/callbacks.rb +40 -0
- data/lib/tire/model/import.rb +27 -0
- data/lib/tire/model/indexing.rb +134 -0
- data/lib/tire/model/naming.rb +100 -0
- data/lib/tire/model/percolate.rb +99 -0
- data/lib/tire/model/persistence.rb +72 -0
- data/lib/tire/model/persistence/attributes.rb +148 -0
- data/lib/tire/model/persistence/finders.rb +54 -0
- data/lib/tire/model/persistence/storage.rb +77 -0
- data/lib/tire/model/search.rb +322 -0
- data/lib/tire/multi_search.rb +263 -0
- data/lib/tire/results/collection.rb +156 -0
- data/lib/tire/results/item.rb +94 -0
- data/lib/tire/results/pagination.rb +68 -0
- data/lib/tire/rubyext/hash.rb +8 -0
- data/lib/tire/rubyext/ruby_1_8.rb +1 -0
- data/lib/tire/rubyext/symbol.rb +11 -0
- data/lib/tire/rubyext/uri_escape.rb +74 -0
- data/lib/tire/search.rb +211 -0
- data/lib/tire/search/facet.rb +81 -0
- data/lib/tire/search/filter.rb +28 -0
- data/lib/tire/search/highlight.rb +37 -0
- data/lib/tire/search/queries/match.rb +40 -0
- data/lib/tire/search/query.rb +250 -0
- data/lib/tire/search/scan.rb +114 -0
- data/lib/tire/search/script_field.rb +23 -0
- data/lib/tire/search/sort.rb +25 -0
- data/lib/tire/tasks.rb +138 -0
- data/lib/tire/utils.rb +17 -0
- data/lib/tire/version.rb +18 -0
- data/test/fixtures/articles/1.json +1 -0
- data/test/fixtures/articles/2.json +1 -0
- data/test/fixtures/articles/3.json +1 -0
- data/test/fixtures/articles/4.json +1 -0
- data/test/fixtures/articles/5.json +1 -0
- data/test/integration/active_model_indexing_test.rb +51 -0
- data/test/integration/active_model_searchable_test.rb +114 -0
- data/test/integration/active_record_searchable_test.rb +620 -0
- data/test/integration/boolean_queries_test.rb +43 -0
- data/test/integration/boosting_queries_test.rb +32 -0
- data/test/integration/bulk_test.rb +86 -0
- data/test/integration/count_test.rb +64 -0
- data/test/integration/custom_score_queries_test.rb +89 -0
- data/test/integration/dis_max_queries_test.rb +68 -0
- data/test/integration/dsl_search_test.rb +30 -0
- data/test/integration/explanation_test.rb +44 -0
- data/test/integration/facets_test.rb +311 -0
- data/test/integration/filtered_queries_test.rb +66 -0
- data/test/integration/filters_test.rb +75 -0
- data/test/integration/fuzzy_queries_test.rb +20 -0
- data/test/integration/highlight_test.rb +64 -0
- data/test/integration/index_aliases_test.rb +122 -0
- data/test/integration/index_mapping_test.rb +43 -0
- data/test/integration/index_store_test.rb +112 -0
- data/test/integration/index_update_document_test.rb +121 -0
- data/test/integration/match_query_test.rb +79 -0
- data/test/integration/mongoid_searchable_test.rb +309 -0
- data/test/integration/multi_search_test.rb +114 -0
- data/test/integration/nested_query_test.rb +135 -0
- data/test/integration/percolator_test.rb +111 -0
- data/test/integration/persistent_model_test.rb +205 -0
- data/test/integration/prefix_query_test.rb +43 -0
- data/test/integration/query_return_version_test.rb +70 -0
- data/test/integration/query_string_test.rb +52 -0
- data/test/integration/range_queries_test.rb +36 -0
- data/test/integration/reindex_test.rb +56 -0
- data/test/integration/results_test.rb +58 -0
- data/test/integration/scan_test.rb +56 -0
- data/test/integration/script_fields_test.rb +38 -0
- data/test/integration/sort_test.rb +52 -0
- data/test/integration/text_query_test.rb +39 -0
- data/test/models/active_model_article.rb +31 -0
- data/test/models/active_model_article_with_callbacks.rb +49 -0
- data/test/models/active_model_article_with_custom_document_type.rb +7 -0
- data/test/models/active_model_article_with_custom_index_name.rb +7 -0
- data/test/models/active_record_models.rb +131 -0
- data/test/models/article.rb +15 -0
- data/test/models/mongoid_models.rb +85 -0
- data/test/models/persistent_article.rb +11 -0
- data/test/models/persistent_article_in_index.rb +16 -0
- data/test/models/persistent_article_in_namespace.rb +12 -0
- data/test/models/persistent_article_with_casting.rb +28 -0
- data/test/models/persistent_article_with_defaults.rb +12 -0
- data/test/models/persistent_article_with_percolation.rb +5 -0
- data/test/models/persistent_articles_with_custom_index_name.rb +10 -0
- data/test/models/supermodel_article.rb +17 -0
- data/test/models/validated_model.rb +11 -0
- data/test/test_helper.rb +118 -0
- data/test/unit/active_model_lint_test.rb +17 -0
- data/test/unit/configuration_test.rb +84 -0
- data/test/unit/count_test.rb +67 -0
- data/test/unit/http_client_test.rb +79 -0
- data/test/unit/http_response_test.rb +49 -0
- data/test/unit/index_alias_test.rb +335 -0
- data/test/unit/index_test.rb +1098 -0
- data/test/unit/logger_test.rb +125 -0
- data/test/unit/model_callbacks_test.rb +116 -0
- data/test/unit/model_import_test.rb +75 -0
- data/test/unit/model_initialization_test.rb +31 -0
- data/test/unit/model_persistence_test.rb +548 -0
- data/test/unit/model_search_test.rb +964 -0
- data/test/unit/multi_search_test.rb +304 -0
- data/test/unit/results_collection_test.rb +372 -0
- data/test/unit/results_item_test.rb +173 -0
- data/test/unit/rubyext_test.rb +66 -0
- data/test/unit/search_facet_test.rb +186 -0
- data/test/unit/search_filter_test.rb +42 -0
- data/test/unit/search_highlight_test.rb +46 -0
- data/test/unit/search_query_test.rb +419 -0
- data/test/unit/search_scan_test.rb +113 -0
- data/test/unit/search_script_field_test.rb +26 -0
- data/test/unit/search_sort_test.rb +50 -0
- data/test/unit/search_test.rb +556 -0
- data/test/unit/tire_test.rb +144 -0
- data/tire.gemspec +83 -0
- metadata +586 -0
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Tire::Search
|
4
|
+
|
5
|
+
class HighlightTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
context "Highlight" do
|
8
|
+
|
9
|
+
should "be serialized to JSON" do
|
10
|
+
assert_respond_to Highlight.new(:body), :to_json
|
11
|
+
end
|
12
|
+
|
13
|
+
should "specify highlight for single field" do
|
14
|
+
assert_equal( {:fields => { :body => {} }}.to_json,
|
15
|
+
Highlight.new(:body).to_json )
|
16
|
+
end
|
17
|
+
|
18
|
+
should "specify highlight for more fields" do
|
19
|
+
assert_equal( {:fields => { :title => {}, :body => {} }}.to_json,
|
20
|
+
Highlight.new(:title, :body).to_json )
|
21
|
+
end
|
22
|
+
|
23
|
+
should "specify highlight for more fields with options" do
|
24
|
+
assert_equal( {:fields => { :title => {}, :body => { :a => 1, :b => 2 } }}.to_json,
|
25
|
+
Highlight.new(:title, :body => { :a => 1, :b => 2 }).to_json )
|
26
|
+
end
|
27
|
+
|
28
|
+
should "specify highlight for more fields with highlight options" do
|
29
|
+
# p Highlight.new(:title, :body => {}, :options => { :tag => '<strong>' }).to_hash
|
30
|
+
assert_equal( {:fields => { :title => {}, :body => {} }, :pre_tags => ['<strong>'], :post_tags => ['</strong>'] }.to_json,
|
31
|
+
Highlight.new(:title, :body => {}, :options => { :tag => '<strong>' }).to_json )
|
32
|
+
end
|
33
|
+
|
34
|
+
context "with custom tags" do
|
35
|
+
|
36
|
+
should "properly parse tags with class" do
|
37
|
+
assert_equal( {:fields => { :title => {} }, :pre_tags => ['<strong class="highlight">'], :post_tags => ['</strong>'] }.to_json,
|
38
|
+
Highlight.new(:title, :options => { :tag => '<strong class="highlight">' }).to_json )
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,419 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Tire::Search
|
4
|
+
|
5
|
+
class QueryTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
context "Query" do
|
8
|
+
should "be serialized to JSON" do
|
9
|
+
assert_respond_to Query.new, :to_json
|
10
|
+
end
|
11
|
+
|
12
|
+
should "return itself as a Hash" do
|
13
|
+
assert_respond_to Query.new, :to_hash
|
14
|
+
assert_equal( { :term => { :foo => { :term => 'bar' } } }, Query.new.term(:foo, 'bar').to_hash )
|
15
|
+
end
|
16
|
+
|
17
|
+
should "allow a block to be given" do
|
18
|
+
assert_equal( { :term => { :foo => { :term => 'bar' } } }.to_json, Query.new do
|
19
|
+
term(:foo, 'bar')
|
20
|
+
end.to_json)
|
21
|
+
end
|
22
|
+
|
23
|
+
should "have accessor for value" do
|
24
|
+
assert_equal( {}, Query.new.value )
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "Term query" do
|
29
|
+
should "allow search for single term" do
|
30
|
+
assert_equal( { :term => { :foo => { :term => 'bar' } } }, Query.new.term(:foo, 'bar') )
|
31
|
+
end
|
32
|
+
|
33
|
+
should "allow search for single term passing an options hash" do
|
34
|
+
assert_equal( { :term => { :foo => { :term => 'bar', :boost => 2.0 } } }, Query.new.term(:foo, 'bar', :boost => 2.0) )
|
35
|
+
end
|
36
|
+
|
37
|
+
should "allow complex term queries" do
|
38
|
+
assert_equal( { :term => { :foo => { :field => 'bar', :boost => 2.0 } } }, Query.new.term(:foo, {:field => 'bar', :boost => 2.0}) )
|
39
|
+
end
|
40
|
+
|
41
|
+
should "allow complex term queries with Hash-like objects" do
|
42
|
+
assert_equal(
|
43
|
+
{ :term => { :foo => { :field => 'bar', :boost => 2.0 } } },
|
44
|
+
Query.new.term(:foo, Hashr.new( :field => 'bar', :boost => 2.0 ))
|
45
|
+
)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context "Terms query" do
|
50
|
+
should "allow search for multiple terms" do
|
51
|
+
assert_equal( { :terms => { :foo => ['bar', 'baz'] } }, Query.new.terms(:foo, ['bar', 'baz']) )
|
52
|
+
end
|
53
|
+
|
54
|
+
should "allow set minimum match when searching for multiple terms" do
|
55
|
+
assert_equal( { :terms => { :foo => ['bar', 'baz'], :minimum_match => 2 } },
|
56
|
+
Query.new.terms(:foo, ['bar', 'baz'], :minimum_match => 2) )
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context "Range query" do
|
61
|
+
should "allow search for a range" do
|
62
|
+
assert_equal( { :range => { :age => { :gte => 21 } } }, Query.new.range(:age, { :gte => 21 }) )
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context "Text query" do
|
67
|
+
should "allow search with a text search" do
|
68
|
+
assert_equal( { :text => {'field' => {:query => 'foo'}}}, Query.new.text('field', 'foo'))
|
69
|
+
end
|
70
|
+
|
71
|
+
should "allow search with a different operator for text search" do
|
72
|
+
assert_equal( { :text => {'field' => {:query => 'foo', :operator => 'and'}}},
|
73
|
+
Query.new.text('field', 'foo', :operator => 'and'))
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context "Query String query" do
|
78
|
+
should "allow search with a query string" do
|
79
|
+
assert_equal( { :query_string => { :query => 'title:foo' } },
|
80
|
+
Query.new.string('title:foo') )
|
81
|
+
end
|
82
|
+
|
83
|
+
should "allow set default field when searching with a query string" do
|
84
|
+
assert_equal( { :query_string => { :query => 'foo', :default_field => 'title' } },
|
85
|
+
Query.new.string('foo', :default_field => 'title') )
|
86
|
+
end
|
87
|
+
|
88
|
+
should "allow set default operator when searching with a query string" do
|
89
|
+
assert_equal( { :query_string => { :query => 'foo', :default_operator => 'AND' } },
|
90
|
+
Query.new.string('foo', :default_operator => 'AND') )
|
91
|
+
end
|
92
|
+
|
93
|
+
should "allow to set options when searching with a query string" do
|
94
|
+
assert_equal( { :query_string => { :query => 'foo', :fields => ['title.*'], :use_dis_max => true } },
|
95
|
+
Query.new.string('foo', :fields => ['title.*'], :use_dis_max => true) )
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
context "Custom Score query" do
|
100
|
+
should "allow to set script for custom score queries" do
|
101
|
+
query = Query.new.custom_score(:script => "_score * doc['price'].value") do
|
102
|
+
string 'foo'
|
103
|
+
end
|
104
|
+
|
105
|
+
assert_equal "_score * doc['price'].value", query[:custom_score][:script]
|
106
|
+
end
|
107
|
+
|
108
|
+
should "allow to pass parameters for custom score queries" do
|
109
|
+
query = Query.new.custom_score(:script => "_score * doc['price'].value / max(a, b)",
|
110
|
+
:params => { :a => 1, :b => 2 }) do
|
111
|
+
string 'foo'
|
112
|
+
end
|
113
|
+
|
114
|
+
assert_equal 1, query[:custom_score][:params][:a]
|
115
|
+
assert_equal 2, query[:custom_score][:params][:b]
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
context "All query" do
|
120
|
+
should "search for all documents" do
|
121
|
+
assert_equal( { :match_all => { } }, Query.new.all )
|
122
|
+
end
|
123
|
+
|
124
|
+
should "allow passing arguments" do
|
125
|
+
assert_equal( { :match_all => {:boost => 1.2} }, Query.new.all(:boost => 1.2) )
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
context "IDs query" do
|
130
|
+
should "search for documents by IDs" do
|
131
|
+
assert_equal( { :ids => { :values => [1, 2], :type => 'foo' } },
|
132
|
+
Query.new.ids([1, 2], 'foo') )
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
context "FuzzyQuery" do
|
137
|
+
|
138
|
+
should "allow a fuzzy search" do
|
139
|
+
assert_equal( { :fuzzy => { :foo => { :term => 'bar' } } }, Query.new.fuzzy(:foo, 'bar') )
|
140
|
+
end
|
141
|
+
|
142
|
+
should "allow a fuzzy search with an options hash" do
|
143
|
+
assert_equal( { :term => { :foo => { :term => 'bar', :boost => 1.0, :min_similarity => 0.5 } } }, Query.new.term(:foo, 'bar', :boost => 1.0, :min_similarity => 0.5 ) )
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
|
+
|
148
|
+
context "BooleanQuery" do
|
149
|
+
|
150
|
+
should "not raise an error when no block is given" do
|
151
|
+
assert_nothing_raised { Query.new.boolean }
|
152
|
+
end
|
153
|
+
|
154
|
+
should "encode options" do
|
155
|
+
query = Query.new.boolean(:minimum_number_should_match => 1) do
|
156
|
+
must { string 'foo' }
|
157
|
+
end
|
158
|
+
|
159
|
+
assert_equal 1, query[:bool][:minimum_number_should_match]
|
160
|
+
end
|
161
|
+
|
162
|
+
should "wrap single query" do
|
163
|
+
assert_equal( { :bool => {:must => [{ :query_string => { :query => 'foo' } }] }},
|
164
|
+
Query.new.boolean { must { string 'foo' } } )
|
165
|
+
end
|
166
|
+
|
167
|
+
should "wrap multiple queries for the same condition" do
|
168
|
+
query = Query.new.boolean do
|
169
|
+
must { string 'foo' }
|
170
|
+
must { string 'bar' }
|
171
|
+
end
|
172
|
+
|
173
|
+
assert_equal( 2, query[:bool][:must].size, query[:bool][:must].inspect )
|
174
|
+
assert_equal( { :query_string => {:query => 'foo'} }, query[:bool][:must].first )
|
175
|
+
assert_equal( { :query_string => {:query => 'bar'} }, query[:bool][:must].last )
|
176
|
+
end
|
177
|
+
|
178
|
+
should "wrap queries for multiple conditions" do
|
179
|
+
query = Query.new.boolean do
|
180
|
+
should { string 'foo' }
|
181
|
+
must { string 'bar' }
|
182
|
+
must { string 'baz' }
|
183
|
+
must_not { string 'fuu' }
|
184
|
+
end
|
185
|
+
|
186
|
+
assert_equal 2, query[:bool][:must].size
|
187
|
+
assert_equal 1, query[:bool][:should].size
|
188
|
+
assert_equal 1, query[:bool][:must_not].size
|
189
|
+
|
190
|
+
assert_equal( { :query_string => {:query => 'foo'} }, query[:bool][:should].first )
|
191
|
+
assert_equal( { :query_string => {:query => 'bar'} }, query[:bool][:must].first )
|
192
|
+
assert_equal( { :query_string => {:query => 'baz'} }, query[:bool][:must].last )
|
193
|
+
assert_equal( { :query_string => {:query => 'fuu'} }, query[:bool][:must_not].first )
|
194
|
+
end
|
195
|
+
|
196
|
+
should "allow passing variables from outer scope" do
|
197
|
+
@q1 = 'foo'
|
198
|
+
@q2 = 'bar'
|
199
|
+
query = Query.new.boolean do |boolean|
|
200
|
+
boolean.must { |query| query.string @q1 }
|
201
|
+
boolean.must { |query| query.string @q2 }
|
202
|
+
end
|
203
|
+
|
204
|
+
assert_equal( 2, query[:bool][:must].size, query[:bool][:must].inspect )
|
205
|
+
assert_equal( { :query_string => {:query => 'foo'} }, query[:bool][:must].first )
|
206
|
+
assert_equal( { :query_string => {:query => 'bar'} }, query[:bool][:must].last )
|
207
|
+
end
|
208
|
+
|
209
|
+
end
|
210
|
+
|
211
|
+
context "FilteredQuery" do
|
212
|
+
|
213
|
+
should "not raise an error when no block is given" do
|
214
|
+
assert_nothing_raised { Query.new.filtered }
|
215
|
+
end
|
216
|
+
|
217
|
+
should "properly encode filter" do
|
218
|
+
query = Query.new.filtered do
|
219
|
+
query { term :foo, 'bar' }
|
220
|
+
filter :terms, :tags => ['ruby']
|
221
|
+
end
|
222
|
+
|
223
|
+
query[:filtered].tap do |f|
|
224
|
+
assert_equal( { :term => { :foo => { :term => 'bar' } } }, f[:query].to_hash )
|
225
|
+
assert_equal( { :tags => ['ruby'] }, f[:filter][:and].first[:terms] )
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
should "properly encode multiple filters" do
|
230
|
+
query = Query.new.filtered do
|
231
|
+
query { term :foo, 'bar' }
|
232
|
+
filter :terms, :tags => ['ruby']
|
233
|
+
filter :terms, :tags => ['python']
|
234
|
+
end
|
235
|
+
|
236
|
+
query[:filtered][:filter].tap do |filter|
|
237
|
+
assert_equal 1, filter.size
|
238
|
+
assert_equal( { :tags => ['ruby'] }, filter[:and].first[:terms] )
|
239
|
+
assert_equal( { :tags => ['python'] }, filter[:and].last[:terms] )
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
should "allow passing variables from outer scope" do
|
244
|
+
@my_query = 'bar'
|
245
|
+
@my_filter = { :tags => ['ruby'] }
|
246
|
+
|
247
|
+
query = Query.new.filtered do |f|
|
248
|
+
f.query { |q| q.term :foo, @my_query }
|
249
|
+
f.filter :terms, @my_filter
|
250
|
+
end
|
251
|
+
|
252
|
+
query[:filtered].tap do |f|
|
253
|
+
assert_equal( { :term => { :foo => { :term => 'bar' } } }, f[:query].to_hash )
|
254
|
+
assert_equal( { :tags => ['ruby'] }, f[:filter][:and].first[:terms] )
|
255
|
+
end
|
256
|
+
end
|
257
|
+
|
258
|
+
context "Prefix query" do
|
259
|
+
should "allow search for a prefix" do
|
260
|
+
assert_equal( { :prefix => { :user => "foo" } }, Query.new.prefix(:user, "foo") )
|
261
|
+
end
|
262
|
+
|
263
|
+
should "allow setting boost for prefix" do
|
264
|
+
assert_equal( { :prefix => {:user => {:prefix => "foo", :boost => 2.0 } } },
|
265
|
+
Query.new.prefix(:user, "foo", :boost => 2.0) )
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
end
|
270
|
+
|
271
|
+
context "DisMaxQuery" do
|
272
|
+
|
273
|
+
should "not raise an error when no block is given" do
|
274
|
+
assert_nothing_raised { Query.new.dis_max }
|
275
|
+
end
|
276
|
+
|
277
|
+
should "encode options" do
|
278
|
+
query = Query.new.dis_max(:tie_breaker => 0.7) do
|
279
|
+
query { string 'foo' }
|
280
|
+
end
|
281
|
+
|
282
|
+
assert_equal 0.7, query[:dis_max][:tie_breaker]
|
283
|
+
end
|
284
|
+
|
285
|
+
should "wrap single query" do
|
286
|
+
assert_equal( { :dis_max => {:queries => [{ :query_string => { :query => 'foo' } }] }},
|
287
|
+
Query.new.dis_max { query { string 'foo' } } )
|
288
|
+
end
|
289
|
+
|
290
|
+
should "wrap multiple queries" do
|
291
|
+
query = Query.new.dis_max do
|
292
|
+
query { string 'foo' }
|
293
|
+
query { string 'bar' }
|
294
|
+
query { string 'baz' }
|
295
|
+
end
|
296
|
+
|
297
|
+
assert_equal 3, query[:dis_max][:queries].size
|
298
|
+
|
299
|
+
assert_equal( { :query_string => {:query => 'foo'} }, query[:dis_max][:queries][0] )
|
300
|
+
assert_equal( { :query_string => {:query => 'bar'} }, query[:dis_max][:queries][1] )
|
301
|
+
assert_equal( { :query_string => {:query => 'baz'} }, query[:dis_max][:queries][2] )
|
302
|
+
end
|
303
|
+
|
304
|
+
should "allow passing variables from outer scope" do
|
305
|
+
@q1 = 'foo'
|
306
|
+
@q2 = 'bar'
|
307
|
+
query = Query.new.dis_max do |dis_max|
|
308
|
+
dis_max.query { |query| query.string @q1 }
|
309
|
+
dis_max.query { |query| query.string @q2 }
|
310
|
+
end
|
311
|
+
|
312
|
+
assert_equal( 2, query[:dis_max][:queries].size, query[:dis_max][:queries].inspect )
|
313
|
+
assert_equal( { :query_string => {:query => 'foo'} }, query[:dis_max][:queries].first )
|
314
|
+
assert_equal( { :query_string => {:query => 'bar'} }, query[:dis_max][:queries].last )
|
315
|
+
end
|
316
|
+
|
317
|
+
end
|
318
|
+
|
319
|
+
context "BoostingQuery" do
|
320
|
+
|
321
|
+
should "not raise an error when no block is given" do
|
322
|
+
assert_nothing_raised { Query.new.boosting }
|
323
|
+
end
|
324
|
+
|
325
|
+
should "encode options" do
|
326
|
+
query = Query.new.boosting(:negative_boost => 0.2) do
|
327
|
+
positive { string 'foo' }
|
328
|
+
end
|
329
|
+
|
330
|
+
assert_equal 0.2, query[:boosting][:negative_boost]
|
331
|
+
end
|
332
|
+
|
333
|
+
should "wrap positive query" do
|
334
|
+
assert_equal( { :boosting => {:positive => [{ :query_string => { :query => 'foo' } }] }},
|
335
|
+
Query.new.boosting { positive { string 'foo' } } )
|
336
|
+
end
|
337
|
+
|
338
|
+
should "wrap negative query" do
|
339
|
+
assert_equal( { :boosting => {:negative => [{ :query_string => { :query => 'foo' } }] }},
|
340
|
+
Query.new.boosting { negative { string 'foo' } } )
|
341
|
+
end
|
342
|
+
|
343
|
+
should "wrap multiple queries for the same condition" do
|
344
|
+
query = Query.new.boosting do
|
345
|
+
positive { string 'foo' }
|
346
|
+
positive { term('bar', 'baz') }
|
347
|
+
end
|
348
|
+
|
349
|
+
assert_equal( 2, query[:boosting][:positive].size, query[:boosting][:positive].inspect )
|
350
|
+
assert_equal( { :query_string => {:query => 'foo'} }, query[:boosting][:positive].first )
|
351
|
+
assert_equal( { :term => { "bar" => { :term => "baz" } } }, query[:boosting][:positive].last )
|
352
|
+
end
|
353
|
+
|
354
|
+
should "allow passing variables from outer scope" do
|
355
|
+
@q1 = 'foo'
|
356
|
+
@q2 = 'bar'
|
357
|
+
query = Query.new.boosting do |boosting|
|
358
|
+
boosting.positive { |query| query.string @q1 }
|
359
|
+
boosting.negative { |query| query.string @q2 }
|
360
|
+
end
|
361
|
+
|
362
|
+
assert_equal( { :query_string => {:query => 'foo'} }, query[:boosting][:positive].first )
|
363
|
+
assert_equal( { :query_string => {:query => 'bar'} }, query[:boosting][:negative].last )
|
364
|
+
end
|
365
|
+
|
366
|
+
end
|
367
|
+
|
368
|
+
context "MatchQuery" do
|
369
|
+
|
370
|
+
should "allow searching in single field" do
|
371
|
+
assert_equal( { :match => { :foo => { :query => 'bar' } } },
|
372
|
+
Query.new.match(:foo, 'bar') )
|
373
|
+
end
|
374
|
+
|
375
|
+
should "allow searching in multiple fields with multi_match" do
|
376
|
+
assert_equal( { :multi_match => { :query => 'bar', :fields => [:foo, :moo] } },
|
377
|
+
Query.new.match([:foo, :moo], 'bar') )
|
378
|
+
end
|
379
|
+
|
380
|
+
should "encode options" do
|
381
|
+
query = Query.new.match(:foo, 'bar', :type => 'phrase_prefix')
|
382
|
+
assert_equal 'phrase_prefix', query[:match][:foo][:type]
|
383
|
+
end
|
384
|
+
|
385
|
+
should "automatically construct a boolean query" do
|
386
|
+
query = Query.new
|
387
|
+
query.match(:foo, 'bar')
|
388
|
+
query.match(:moo, 'bar')
|
389
|
+
|
390
|
+
assert_not_nil query.to_hash[:bool]
|
391
|
+
assert_equal 2, query.to_hash[:bool][:must].size
|
392
|
+
end
|
393
|
+
|
394
|
+
end
|
395
|
+
|
396
|
+
context "NestedQuery" do
|
397
|
+
|
398
|
+
should "not raise an error when no block is given" do
|
399
|
+
assert_nothing_raised { Query.new.nested }
|
400
|
+
end
|
401
|
+
|
402
|
+
should "encode options" do
|
403
|
+
query = Query.new.nested(:path => 'articles', :score_mode => 'score_mode') do
|
404
|
+
query { string 'foo' }
|
405
|
+
end
|
406
|
+
|
407
|
+
assert_equal 'articles', query[:nested][:path]
|
408
|
+
assert_equal 'score_mode', query[:nested][:score_mode]
|
409
|
+
end
|
410
|
+
|
411
|
+
should "wrap single query" do
|
412
|
+
assert_equal( { :nested => {:query => { :query_string => { :query => 'foo' } } }},
|
413
|
+
Query.new.nested { query { string 'foo' } } )
|
414
|
+
end
|
415
|
+
|
416
|
+
end
|
417
|
+
|
418
|
+
end
|
419
|
+
end
|