Wrap text
Report abuse
|
|
def parse_google query
return unless query
# alters google-style querystring into sphinx-style
query = query.scan(/[^"\(\) ]*["\(][^"\)]*["\)]|[^"\(\) ]+/) # thanks chris2
query.each_with_index do |token, index|
if token =~ /^(.*?)\((.*)\)(.*?$)/
token = query[index] = "#{$1}(#{parse_google $2})#{$3}" # recurse for parens
end
case token
when "OR"
query[index] = "|"
when "NOT"
query[index] = "-#{query[index+1]}"
query[index+1] = ""
when "AND"
query[index] = ""
when /:/
query[query.size] = "@" + query[index].sub(":", " ")
query[index] = ""
end
end
query.join(" ").squeeze(" ")
end
|