Skip to content

Commit

Permalink
Properly (if convolutedly) query for empty strings
Browse files Browse the repository at this point in the history
[sunspot#112 state:resolved]
  • Loading branch information
Mat Brown committed Jul 14, 2010
1 parent ab9c261 commit e53b4bf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
9 changes: 6 additions & 3 deletions sunspot/lib/sunspot/query/restriction.rb
Expand Up @@ -149,10 +149,13 @@ def solr_value(value = @value)
#
class EqualTo < Base
def to_positive_boolean_phrase
unless @value.nil?
super
else
case @value
when nil
"#{escape(@field.indexed_name)}:[* TO *]"
when ''
%Q(#{escape(@field.indexed_name)}:[* TO ""])
else
super
end
end

Expand Down
7 changes: 7 additions & 0 deletions sunspot/spec/api/query/scope_examples.rb
Expand Up @@ -13,6 +13,13 @@
connection.should have_last_search_including(:fq, 'title_ss:"OR"')
end

it 'scopes by exact match with empty string' do
search do
with :title, ''
end
connection.should have_last_search_including(:fq, 'title_ss:[* TO ""]')
end

it 'scopes by exact match with time' do
time = Time.parse('1983-07-08 05:00:00 -0400')
search do
Expand Down
25 changes: 25 additions & 0 deletions sunspot/spec/integration/scoped_search_spec.rb
Expand Up @@ -144,6 +144,31 @@ def self.test_field_type(name, attribute, field, *values)
end
end

describe 'empty strings' do
before :all do
Sunspot.remove_all
@posts = [Post.new(:title => ''), Post.new(:title => 'Bogus'), Post.new]
Sunspot.index!(@posts)
end

it 'should filter results with an empty string' do
search = Sunspot.search(Post) { with(:title, '') }
search.results.should == [@posts[0]]
end

it 'should not exclude results with non-empty string value' do
Sunspot.search(Post) { without(:title, '') }.results.should include(@posts[1])
end

it 'should not exclude results with no value' do
Sunspot.search(Post) { without(:title, '') }.results.should include(@posts[2])
end

it 'should exclude results with empty string value' do
Sunspot.search(Post) { without(:title, '') }.results.should_not include(@posts[0])
end
end

describe 'prefix searching' do
before :each do
Sunspot.remove_all
Expand Down

0 comments on commit e53b4bf

Please sign in to comment.