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

人走茶凉 提交于 2020-01-02 07:42:21

问题


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.


回答1:


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"


来源:https://stackoverflow.com/questions/5890401/sunspot-solr-queries-ending-with-logical-operators-and-or-not-result-in-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!