我不太明白为什么我从以下查询/语句中得到意外的结果。我已经包含了复制该问题的代码(这可能根本不是问题,但更多的是我对如何进行的误解contains
works).
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 传入的参数,因此声明该变量来模拟传入的参数。
在全文中,索引中的所有非字母数字字符均被删除并替换为空格。
所以在你的搜索中,因为你有“。”在字符串中,您正在搜索“website”和“net”。
您可以通过两种方式解决此问题。
您需要有一个包含全文数据的单独表或单独字段,与保留原始数据的原始表分开。
在全文表中,您可以删除“.“并存储”网站网".
在这种情况下,您需要删除所有“.”。在执行查询之前从搜索字符串中获取。如果想用“.”进行查询,需要将“.”替换掉带有字符串 -
例如“点”。
所以在这种情况下你会存储“网站dotnet".
这次搜索时,您将替换所有“。”。查询中带有“点”。
现在,您的案例有一个新字段,其中存储要由 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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)