Skip to content

Commit

Permalink
Added downcasing of misplaced keywords occurring at the end of a quer…
Browse files Browse the repository at this point in the history
…y. The current version errors on queries like "BOB AND".
  • Loading branch information
Jonathan Vaught committed Aug 22, 2008
1 parent b4b90d7 commit 645ab5a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
10 changes: 10 additions & 0 deletions examples/lucene_query.rb
Expand Up @@ -32,6 +32,12 @@
lambda { '\d{10}' }.should generate_query("'\\\\d\\{10\\}'")
end

it "should downcase incorrectly-located keywords" do
lambda { "Me AND" }.should generate_query("'Me and'")
lambda { "You OR" }.should generate_query("'You or'")
lambda { "Maybe NOT" }.should generate_query("'Maybe not'")
end

it "should group Arrays" do
lambda { [:red, :green, :blue] }.should generate_query("(red green blue)")
end
Expand All @@ -44,6 +50,10 @@
lambda { Or(:symbol, 42, "string") }.should generate_query("(symbol OR 42 OR 'string')")
end

it "should negate a term with NOT" do
lambda { Not("me") }.should generate_query("NOT 'me'")
end

it "should support fields" do
lambda { Field(:city, "Portland") }.should generate_query("city:'Portland'")
lambda { Field("city", "Portland") }.should generate_query("'city':'Portland'")
Expand Down
8 changes: 7 additions & 1 deletion lib/lucene_query.rb
@@ -1,7 +1,7 @@
class LuceneQuery
## Syntax Nodes
::String.class_eval do
def to_lucene; "'#{escape_lucene}'" end
def to_lucene; "'#{escape_lucene.downcase_ending_keywords}'" end

def parens
if self =~ /^\s*$/
Expand All @@ -23,6 +23,12 @@ def parens
def escape_lucene
gsub(RE_ESCAPE_LUCENE) { |m| "\\#{m}" }
end

ENDING_KEYWORDS = /(AND$ | OR$ | NOT$)/x unless defined?(ENDING_KEYWORDS)

def downcase_ending_keywords
gsub(ENDING_KEYWORDS) { |w| w.downcase }
end
end

::Symbol.class_eval do
Expand Down

0 comments on commit 645ab5a

Please sign in to comment.