Bashar Abdullah
Bashar Abdullah

Reputation: 1545

Sunspot/Solr queries ending with logical operators AND/OR/NOT result in error

I noticed that queries ending with logical operators like AND/OR/NOT example ('this AND') will result in an error. Now what would be the best way to handle this? Just trim out or escape all the queries ending with one of those? Note that it also happens for queries starting with one of these words. And sometimes, valid names end with such words, like Oregon OR.

Upvotes: 3

Views: 1324

Answers (1)

Dylan Markow
Dylan Markow

Reputation: 124419

I believe escaping any AND/OR/NOT instances in your query that aren't meant to be boolean logic would be your best bet:

Article.search do
  fulltext 'Oregon OR'
end
# => Throws error along the lines of this:
# RSolr::RequestError: Solr Response: orgapachelucenequeryParserParseException_Cannot_parse_Oregon_OR_Encountered_EOF_at_line_1_column_9_Was_expecting_one_of_____NOT______________________________QUOTED______TERM______PREFIXTERM______WILDTERM__________________NUMBER______TERM____________


Article.search do
  fulltext 'Oregon \OR'
end
# => Returns results with "Oregon OR"

Keep in mind that when escaping AND/OR/NOT in double-quoted strings, you need two backslashes:

fulltext "Oregon \\OR"

Upvotes: 6

Related Questions