将用户输入的搜索查询转换为 where 子句以在 SQL Server 全文搜索中使用
将用户输入的搜索词转换为可在 where 子句中用于全文搜索以查询表并获取相关结果的查询的最佳方法是什么? 例如,用户输入的以下查询:
+"e-mail" +attachment -"word document" -"e-learning"
应翻译为:
SELECT * FROM MyTable WHERE (CONTAINS(*, '"e-mail"')) AND (CONTAINS(*, '"attachment"')) AND (NOT CONTAINS(*, '"word document"')) AND (NOT CONTAINS(*, '"e-learning"'))
我正在使用 查询解析器类目前,它使用正则表达式将用户输入的查询解析为标记,然后根据标记构造where子句。
然而,考虑到这可能是许多使用全文搜索的系统的常见要求,我很好奇其他开发人员如何解决这个问题,以及是否有更好的方法。
What's the best way to convert search terms entered by a user, into a query that can be used in a where clause for full-text searching to query a table and get back relevant results? For example, the following query entered by the user:
+"e-mail" +attachment -"word document" -"e-learning"
Should translate into something like:
SELECT * FROM MyTable WHERE (CONTAINS(*, '"e-mail"')) AND (CONTAINS(*, '"attachment"')) AND (NOT CONTAINS(*, '"word document"')) AND (NOT CONTAINS(*, '"e-learning"'))
I'm using a query parser class at the moment, which parses the query entered by users into tokens using a regular expression, and then constructs the where clause from the tokens.
However, given that this is probably a common requirement by a lot of systems using full-text search, I'm curious as to how other developers have approached this problem, and whether there's a better way of doing things.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如何使用 .Net / C# / Entity Framework 实现接受的答案...
使用 nuget 安装 Irony。
添加示例类:
http://irony.codeplex.com/SourceControl/latest#Irony .Samples/FullTextSearchQueryConverter/SearchGrammar.cs
编写这样的代码,将用户输入的字符串转换为查询。
也许编写一个像这样的存储过程:
运行查询。
How to implement the accepted answer using .Net / C# / Entity Framework...
Install Irony using nuget.
Add the sample class from:
http://irony.codeplex.com/SourceControl/latest#Irony.Samples/FullTextSearchQueryConverter/SearchGrammar.cs
Write code like this to convert the user-entered string to a query.
Perhaps write a stored procedure like this:
Run the query.
这可能不完全是您正在寻找的,但它可能会为您提供一些进一步的想法。
http://www.sqlservercentral.com/articles/Full-Text +搜索+(2008)/64248/
This may not be exactly what you are looking for but it may offer you some further ideas.
http://www.sqlservercentral.com/articles/Full-Text+Search+(2008)/64248/
除了上面 @franzo 的答案之外,您可能还想更改 SQL 中的默认停用词行为。 否则,包含单位数字(或其他停用词)的查询将不会返回任何结果。
禁用停用词、创建您自己的停用词列表和/或设置要转换的干扰词,如 SQL 2008:关闭全文搜索查询的停止词
要查看(英语)sql 停止词的系统列表,请运行:
In addition to @franzo's answer above you probably also want to change the default stop word behaviour in SQL. Otherwise queries containing single digit numbers (or other stop words) will not return any results.
Either disable stop words, create your own stop word list and/or set noise words to be transformed as explained in SQL 2008: Turn off Stop Words for Full Text Search Query
To view the system list of (English) sql stop words, run:
我意识到这有点偏离你原来的问题,但你是否考虑过放弃 SQL 全文索引并使用类似 Lucene/Solr 而是?
I realize it's a bit of a side-step from your original question, but have you considered moving away from SQL fulltext indexes and using something like Lucene/Solr instead?
最简单的方法是使用动态 SQL(我知道,在这里插入安全问题)并将短语分解为格式正确的字符串。
您可以使用函数将短语分解为可用于创建新字符串的表变量。
The easiest way to do this is to use dynamic SQL (I know, insert security issues here) and break the phrase into a correctly formatted string.
You can use a function to break the phrase into a table variable that you can use to create the new string.
GoldParser 和 Calitha 的组合应该可以解决您的问题。
本文:http://www.15seconds.com/issue/070719.htm还有一个 googleToSql 类,它可以为您完成一些翻译。
A combination of GoldParser and Calitha should sort you out here.
This article: http://www.15seconds.com/issue/070719.htm has a googleToSql class as well, which does some of the translation for you.