Skip to content
Browse files

Merge pull request #165 from krzyzak/master

Fixed greater_than and lower_than behaviour
  • Loading branch information...
2 parents d2a3bcb + 55eb58d commit 7c4223786535782a6500f5fb6d0310a69e2c7cf5 @nz nz committed
View
34 sunspot/lib/sunspot/query/restriction.rb
@@ -185,6 +185,23 @@ def solr_value(value = @value)
end
def to_solr_conditional
+ "{* TO #{solr_value}}"
+ end
+ end
+
+ #
+ # Results must have field with value less or equal to than given value
+ #
+ class LessThanOrEqualTo < Base
+ private
+
+ def solr_value(value = @value)
+ solr_value = super
+ solr_value = "\"#{solr_value}\"" if solr_value.index(' ')
+ solr_value
+ end
+
+ def to_solr_conditional
"[* TO #{solr_value}]"
end
end
@@ -202,6 +219,23 @@ def solr_value(value = @value)
end
def to_solr_conditional
+ "{#{solr_value} TO *}"
+ end
+ end
+
+ #
+ # Results must have field with value greater than or equal to given value
+ #
+ class GreaterThanOrEqualTo < Base
+ private
+
+ def solr_value(value = @value)
+ solr_value = super
+ solr_value = "\"#{solr_value}\"" if solr_value.index(' ')
+ solr_value
+ end
+
+ def to_solr_conditional
"[#{solr_value} TO *]"
end
end
View
10 sunspot/spec/api/query/connectives_examples.rb
@@ -23,7 +23,7 @@
end
connection.should have_last_search_including(
:fq,
- '(blog_id_i:2 OR (category_ids_im:1 AND average_rating_ft:[3\.0 TO *]))'
+ '(blog_id_i:2 OR (category_ids_im:1 AND average_rating_ft:{3\.0 TO *}))'
)
end
@@ -38,7 +38,7 @@
end
end
connection.should have_last_search_including(
- :fq, '(category_ids_im:1 OR (-average_rating_ft:[3\.0 TO *] AND blog_id_i:1))'
+ :fq, '(category_ids_im:1 OR (-average_rating_ft:{3\.0 TO *} AND blog_id_i:1))'
)
end
@@ -62,7 +62,7 @@
end
end
connection.should have_last_search_including(
- :fq, '-(-category_ids_im:1 AND average_rating_ft:[3\.0 TO *])'
+ :fq, '-(-category_ids_im:1 AND average_rating_ft:{3\.0 TO *})'
)
end
@@ -149,7 +149,7 @@
end
end
connection.should have_last_search_including(
- :fq, '-(average_rating_ft:[* TO *] AND -average_rating_ft:[3\.0 TO *])'
+ :fq, '-(average_rating_ft:[* TO *] AND -average_rating_ft:{3\.0 TO *})'
)
end
@@ -162,7 +162,7 @@
end
end
connection.should have_last_search_including(
- :fq, "(id:(Post\\ #{post.id}) OR average_rating_ft:[3\\.0 TO *])"
+ :fq, "(id:(Post\\ #{post.id}) OR average_rating_ft:{3\\.0 TO *})"
)
end
View
2 sunspot/spec/api/query/dynamic_fields_examples.rb
@@ -14,7 +14,7 @@
with(:test).less_than(1)
end
end
- connection.should have_last_search_including(:fq, 'custom_integer\:test_i:[* TO 1]')
+ connection.should have_last_search_including(:fq, 'custom_integer\:test_i:{* TO 1}')
end
it 'restricts by dynamic float field with between restriction' do
View
4 sunspot/spec/api/query/fulltext_examples.rb
@@ -224,7 +224,7 @@
end
end
end
- connection.should have_last_search_with(:bq => ['average_rating_ft:[2\.0 TO *]^2.0'])
+ connection.should have_last_search_with(:bq => ['average_rating_ft:{2\.0 TO *}^2.0'])
end
it 'creates multiple boost queries' do
@@ -240,7 +240,7 @@
end
connection.should have_last_search_with(
:bq => [
- 'average_rating_ft:[2\.0 TO *]^2.0',
+ 'average_rating_ft:{2\.0 TO *}^2.0',
'featured_bs:true^1.5'
]
)
View
10 sunspot/spec/api/query/scope_examples.rb
@@ -46,21 +46,21 @@
search do
with(:average_rating).less_than 3.0
end
- connection.should have_last_search_including(:fq, 'average_rating_ft:[* TO 3\.0]')
+ connection.should have_last_search_including(:fq, 'average_rating_ft:{* TO 3\.0}')
end
it 'should quote string with space in a less than match' do
search do
with(:title).less_than('test value')
end
- connection.should have_last_search_including(:fq, 'title_ss:[* TO "test\ value"]')
+ connection.should have_last_search_including(:fq, 'title_ss:{* TO "test\ value"}')
end
it 'scopes by greater than match with float' do
search do
with(:average_rating).greater_than 3.0
end
- connection.should have_last_search_including(:fq, 'average_rating_ft:[3\.0 TO *]')
+ connection.should have_last_search_including(:fq, 'average_rating_ft:{3\.0 TO *}')
end
it 'scopes by short-form between match with integers' do
@@ -116,14 +116,14 @@
search do
without(:average_rating).less_than 3.0
end
- connection.should have_last_search_including(:fq, '-average_rating_ft:[* TO 3\.0]')
+ connection.should have_last_search_including(:fq, '-average_rating_ft:{* TO 3\.0}')
end
it 'scopes by not greater than match with float' do
search do
without(:average_rating).greater_than 3.0
end
- connection.should have_last_search_including(:fq, '-average_rating_ft:[3\.0 TO *]')
+ connection.should have_last_search_including(:fq, '-average_rating_ft:{3\.0 TO *}')
end
it 'scopes by not between match with shorthand' do
View
30 sunspot/spec/integration/scoped_search_spec.rb
@@ -33,24 +33,48 @@ def self.test_field_type(name, attribute, field, *values)
it 'should filter by less than' do
results = Sunspot.search(clazz) { with(field).less_than values[2] }.results
- (0..2).each { |i| results.should include(@objects[i]) }
- (3..4).each { |i| results.should_not include(@objects[i]) }
+ (0..1).each { |i| results.should include(@objects[i]) }
+ (2..4).each { |i| results.should_not include(@objects[i]) }
end
it 'should reject by less than' do
results = Sunspot.search(clazz) { without(field).less_than values[2] }.results
+ (0..1).each { |i| results.should_not include(@objects[i]) }
+ (2..4).each { |i| results.should include(@objects[i]) }
+ end
+
+ it 'should filter by less than or equal to' do
+ results = Sunspot.search(clazz) { with(field).less_than_or_equal_to values[2] }.results
+ (0..2).each { |i| results.should include(@objects[i]) }
+ (3..4).each { |i| results.should_not include(@objects[i]) }
+ end
+
+ it 'should reject by less than or equal to' do
+ results = Sunspot.search(clazz) { without(field).less_than_or_equal_to values[2] }.results
(0..2).each { |i| results.should_not include(@objects[i]) }
(3..4).each { |i| results.should include(@objects[i]) }
end
it 'should filter by greater than' do
results = Sunspot.search(clazz) { with(field).greater_than values[2] }.results
+ (3..4).each { |i| results.should include(@objects[i]) }
+ (0..2).each { |i| results.should_not include(@objects[i]) }
+ end
+
+ it 'should reject by greater than' do
+ results = Sunspot.search(clazz) { without(field).greater_than values[2] }.results
+ (3..4).each { |i| results.should_not include(@objects[i]) }
+ (0..2).each { |i| results.should include(@objects[i]) }
+ end
+
+ it 'should filter by greater than or equal to' do
+ results = Sunspot.search(clazz) { with(field).greater_than_or_equal_to values[2] }.results
(2..4).each { |i| results.should include(@objects[i]) }
(0..1).each { |i| results.should_not include(@objects[i]) }
end
it 'should reject by greater than' do
- results = Sunspot.search(clazz) { without(field).greater_than values[2] }.results
+ results = Sunspot.search(clazz) { without(field).greater_than_or_equal_to values[2] }.results
(2..4).each { |i| results.should_not include(@objects[i]) }
(0..1).each { |i| results.should include(@objects[i]) }
end

0 comments on commit 7c42237

Please sign in to comment.
Something went wrong with that request. Please try again.