如何在 sunspot/solr 全文搜索中转义非法字符?
我有一个用于全文搜索模型属性的 sunspot/solr 设置。我的 QA 刚刚搜索了 " 和 +,这两个错误都导致了 500 错误:
Solr 响应:orgapachelucenequeryParserParseException_Cannot_parse__Encountered_EOF_at_line_1_column_0_Was_expecting_one_of_____NOT_________________________QUOTED__TERM__PREFIXTERM__WILDTERM__________________NUMBER__TERM________
如何我使这些查询字符串安全吗?Sunspot 中有方法可以处理这个问题吗?
I have a sunspot/solr setup for fulltext searching model attributes. My QA just searched on " and +, both of which caused a 500 error:
Solr Response: orgapachelucenequeryParserParseException_Cannot_parse__Encountered_EOF_at_line_1_column_0_Was_expecting_one_of_____NOT______________________________QUOTED______TERM______PREFIXTERM______WILDTERM__________________NUMBER______TERM____________
How can I make these query strings safe? Is there a method in Sunspot to handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Sunspot 中没有方法来过滤这些,因为它们在某些类型的 Lucene 查询中是有效的。 Sunspot 默认使用 DisMax 查询解析器,因此您可以阅读其文档以了解更多信息关于那些角色。
如果您打算永远不使用这些字符,您可以自己从查询中过滤它们(反斜杠是为了转义减号)。
您可能希望将其包裹在如果您在控制器中调用 Sunspot 的
search
方法,则为控制器方法;如果您从类自己的自定义中调用 Sunspot 的solr_search
方法,则为模型方法搜索
方法。There is no method in Sunspot to filter those, because they are valid in certain kinds of Lucene queries. Sunspot is using the DisMax Query Parser by default, so you can read its documentation to learn more about those characters.
If you intend to never use those characters, you can filter them yourself from queries(the back slash is to escape minus sign).
You may want to wrap that in a controller method, if you're invoking Sunspot's
search
method within a controller, or a model method if you're calling Sunspot'ssolr_search
method from within your class's own customsearch
method.