SQL Server包含不返回预期结果的全文函数

时间:2012-05-21 12:39:37

标签: sql-server sql-server-2008 full-text-search

我无法弄清楚为什么我会从以下查询/语句中获得意外结果。我已经包含了复制问题的代码(这可能不是问题,但更多的是我对contains如何工作的误解。)

create table dbo.temp (id int identity, description nvarchar(max))
insert dbo.temp values ('this is a website.') --this record will be returned in the select query
insert dbo.temp values ('a website exists.') --this record will be returned in the select 
insert dbo.temp values ('go to mywebsite.net') --this record will NOT be returned in the select 
insert dbo.temp values ('go to mywebsite.net.') --this record will NOT be returned in the select 


create fulltext catalog temp
create unique index idx_dbo_temp_1 on dbo.temp (id)
create fulltext index on dbo.temp(description)
    key index idx_dbo_temp_1 on temp
    with change_tracking auto


declare @search_client nvarchar(100) = 'website'

select
    *
from
    dbo.temp
where
    contains ((description),@search_client)


drop fulltext index on dbo.temp
drop index idx_dbo_temp_1 on dbo.temp
drop fulltext catalog temp
drop table dbo.temp

查询将返回描述字段中包含website的记录,但不会返回描述字段中包含mywebsite.net的记录。

有什么想法吗?

更新:@search_client变量实际上是通过SSRS传入的参数,因此声明变量来模拟传入的参数。

3 个答案:

答案 0 :(得分:2)

在Fulltext中,在索引中删除所有非字母数字字符,并替换为空格。 所以在你的搜索中,因为你有“。”在字符串中,您正在搜索“网站”和“网络”。

您可以通过两种方式解决此问题。

您需要有一个单独的表或具有全文数据的单独字段,如果您保留原始数据,则与原始表分开。

在全文表格中,您可以删除“”并存储“ websitenet ”。

在这种情况下,您需要删除所有“。”在执行查询之前从搜索字符串开始。如果要使用“。”查询,则需要替换“。”。用字符串 - 例如“dot”。

因此,在这种情况下,您可以存储“ websitedotnet ”。

这次搜索时,您将替换所有“。”在查询中使用“点”。

现在好了你的情况,有一个新字段存储要由FTS搜索的列,所以:

    ID      DESCRIPTION               DESCFTS
    -----------------------------------------------------
    1   this is a website.        this is a websitedot
    2   a website exists.         a website existsdot
    3   go to mywebsite.net       go to mywebsitedotnet
    4   go to mywebsite.net.      go to mywebsitedotnetdot

然后你的查询:

declare @search_client nvarchar(100) = 'website'

set @search_client = replace(@search_client, '.', 'dot')

select * from dbo.temp where contains ((DESCFTS), @search_client)

答案 1 :(得分:0)

尝试以下方法:

更新:

select
    *
from
    dbo.temp
where
    contains ((description),'"website*"')

答案 2 :(得分:0)

我认为您遇到的问题是,无法在FTS中使用前导通配符进行搜索。如果您使用CONTAINS短语进行搜索,则不能使用前导,只能使用尾随功能。寻找领先的通配符搜索的解决方法。这是我在mywebsite.net中创建问题而未显示的问题。这与已经提到的DOT问题相结合。有一些解决方法,但它们对我来说似乎很苛刻 - 并且还研究了FREETEXT的性能提升可能性。