SQL Server 2008 中的 FREETEXT 查询不进行短语匹配
我在 SQL Server 2008 中有一个全文索引表,我正在尝试使用 FULLTEXT 查询精确的短语匹配。我不认为使用 CONTAINS 或 LIKE 适合于此,因为在其他情况下查询可能不准确(用户没有用双引号括起短语)并且一般来说我想要 FREETEXT 的灵活性。
根据FREETEXT的文档[MSDN]:
如果 freetext_string 用双引号括起来,则执行短语匹配;不执行词干提取和同义词库。
这会让我相信这样的查询:
SELECT Description
FROM Projects
WHERE FREETEXT(Description, '"City Hall"')
只会返回“描述”字段中出现术语“市政厅”的结果,但我得到的结果如下:
1 曼宁厅残疾人坡道的设计。
2 天线调查。客户:克兰斯顿市工程部
3 关于国际网球名人堂火灾损坏的结构调查。
4 调查 先驱厅拟议卫星设计的屋顶调查。
...等等
显然,这些结果至少包括我的短语中的一个单词,但不包括短语本身。更糟糕的是,我原以为结果会被排名,但我真正想要的两个结果(因为它们包含实际的短语)被掩埋了。
SELECT Description
FROM Projects
WHERE Description LIKE '%City Hall%'
1 马萨诸塞州昆西现有市政厅的重大外部和内部翻修
2 对受渗漏困扰的波塔基特市政厅塔楼进行粗略的结构调查。
我确信这是我不理解文档的情况,但是有没有办法实现我正在寻找的东西?也就是说,能够传入不带引号的搜索字符串并准确获取我现在得到的内容,或者带引号并仅获取确切的短语?
I have a full text indexed table in SQL Server 2008 that I am trying to query for an exact phrase match using FULLTEXT. I don't believe using CONTAINS or LIKE is appropriate for this, because in other cases the query might not be exact (user doesn't surround phrase in double quotes) and in general I want to flexibility of FREETEXT.
According to the documentation[MSDN] for FREETEXT:
If freetext_string is enclosed in double quotation marks, a phrase match is instead performed; stemming and thesaurus are not performed.
which would lead me to believe a query like this:
SELECT Description
FROM Projects
WHERE FREETEXT(Description, '"City Hall"')
would only return results where the term "City Hall" appears in the Description field, but instead I get results like this:
1 Design of handicap ramp at Manning Hall.
2 Antenna investigation. Client: City of Cranston Engineering Dept.
3 Structural investigation regarding fire damage to International Tennis Hall of Fame.
4 Investigation Roof investigation for proposed satellite design on Herald Hall.
... etc
Obviously those results include at least one of the words in my phrase, but not the phrase itself. What's worse, I had thought the results would be ranked but the two results I actually wanted (because they include the actual phrase) are buried.
SELECT Description
FROM Projects
WHERE Description LIKE '%City Hall%'
1 Major exterior and interior renovation of the existing city hall for Quincy Massachusetts
2 Cursory structural investigation of Pawtucket City Hall tower plagued by leaks.
I'm sure this is a case of me not understanding the documentation, but is there a way to achieve what I'm looking for? Namely, to be able to pass in a search string without quotes and get exactly what I'm getting now or with quotes and get only that exact phrase?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您所说,
FREETEXT
会查找短语中的每个单词,而不是整个短语。为此,您需要使用CONTAINS
语句。像这样:如果你想获得结果的排名,你必须使用
CONTAINSTABLE
。它的工作原理大致相同,但它返回一个包含两列的表:[Key],其中包含搜索表的主键;[Rank],它给出结果的排名。As you said,
FREETEXT
looks up every word in your phrase, not the phrase as an all. For that you need to use theCONTAINS
statement. Like this:If you want to get the rank of the results, you have to use
CONTAINSTABLE
. It works roughly the same, but it returns a table with two columns: [Key] wich contains the primary key of the search table and [Rank], which gives you the rank of the result.