Skip to content

Commit

Permalink
Quote reserved keywords in boolean queries
Browse files Browse the repository at this point in the history
[sunspot#88 state:resolved]
  • Loading branch information
Mat Brown committed Mar 10, 2010
1 parent 49bf335 commit 351cd58
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions sunspot/lib/sunspot/query/restriction.rb
Expand Up @@ -40,6 +40,8 @@ class Base #:nodoc:
include Filter
include RSolr::Char

RESERVED_WORDS = Set['AND', 'OR', 'NOT']

def initialize(field, value, negated = false)
@field, @value, @negated = field, value, negated
end
Expand Down Expand Up @@ -133,6 +135,11 @@ def negate
#
def solr_value(value = @value)
solr_value = escape(@field.to_indexed(value))
if RESERVED_WORDS.include?(solr_value)
%Q("#{solr_value}")
else
solr_value
end
end
end

Expand Down
7 changes: 7 additions & 0 deletions sunspot/spec/api/query/scope_spec.rb
Expand Up @@ -15,6 +15,13 @@
connection.should have_last_search_including(:fq, 'title_ss:My\ Pet\ Post')
end

it 'scopes by exact match with a special string' do
session.search Post do
with :title, 'OR'
end
connection.should have_last_search_including(:fq, 'title_ss:"OR"')
end

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

describe 'reserved words' do
%w(AND OR NOT TO).each do |word|
it "should successfully search for #{word.inspect}" do
Sunspot.index!(post = Post.new(:title => word))
Sunspot.search(Post) { with(:title, word) }.results.should == [post]
end
end
end

describe 'passing nil value to equal' do
before :all do
Sunspot.remove_all
Expand Down

0 comments on commit 351cd58

Please sign in to comment.