解析AND、OR查询来制定sql
我正在开发一个小型搜索引擎,我想实现基于逻辑运算符 AND OR 的搜索功能...
我在解析包含 AND、OR、NOT 的查询时遇到困难...尤其是当它出现时括号...(猫或狗)不是(自行车不是迈克)
对于简单的 AND 和 OR 查询,它显然太简单了,我弄清楚了如何制定 sql 查询,但是当它变得那么复杂时我迷失了! !!
我不确定搜索引擎是否有此功能,但我想出于学习目的深入研究它。
我为我的上一个问题表示歉意,这个问题不太清楚,我希望这次我做得更好。
I'm developping a mini search engine, and I want to implement the feature of searches based on logic operators AND OR...
I'm having a difficulty on parsing a query containing AND, OR, NOT... especially when it comes to parentheses... (cat or dog) not (bike not mike)
For simple AND, and OR queries, it's obviously too simple and I figured out how to formulate the sql query, but when it becomes that complicated I'm lost !!!
I'm not sure if search engines have this feature, but I want to dive into it for learning purpose.
I apologize for my last question which wasn't really clear, I hope this time I'm doing better.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议查看词法分析器/解析器生成器,例如 ANTLR。一个简单的语法应该可以帮助你解决问题。甚至可能有这样的事情的现有语法。
I'd recommend looking at a lexer/parser generator like ANTLR. A simple grammar should be able to sort you out. There might even be an existing grammar for such a thing.
查看 searchparser.py 示例http://pyparsing.wikispaces.com/" rel="nofollow noreferrer">pyparsing 项目。
它展示了一种实现
全部用 293 行代码完成(包括注释和测试)...
Take a look at the searchparser.py example from the pyparsing project.
It shows a way to implement:
All done in 293 lines of code (including comments and tests) ...
如果您使用 MySQL,则可以使用内置布尔搜索:
http ://dev.mysql.com/doc/refman/5.1/en/fulltext-boolean.html
If you are using MySQL you can use the builtin boolean search:
http://dev.mysql.com/doc/refman/5.1/en/fulltext-boolean.html