SQL 2005全文搜索:如何将所有用户搜索词默认为包含近类型FTS搜索
我有一个asp.net网页,其中有一个简单的搜索文本框,可以从MSSQL 2005数据库返回匹配的行。它目前使用LIKE语句返回匹配项,但强制用户键入准确的短语。用户希望获得更多谷歌搜索类型的体验,因此我决定使用FTS设置和索引所需的表格 我希望用户在包含搜索中使用一个或多个搜索词,并在文本框中几乎分隔他们键入的每个词。我是一个新的开发人员,不知道如何做到这一点,或者是否已经有一个内置的功能来涵盖这一点 例如,如果用户在搜索框中键入“Sawyer Tom”,则查询的功能如下:SQL 2005全文搜索:如何将所有用户搜索词默认为包含近类型FTS搜索,sql,contains,full-text-search,Sql,Contains,Full Text Search,我有一个asp.net网页,其中有一个简单的搜索文本框,可以从MSSQL 2005数据库返回匹配的行。它目前使用LIKE语句返回匹配项,但强制用户键入准确的短语。用户希望获得更多谷歌搜索类型的体验,因此我决定使用FTS设置和索引所需的表格 我希望用户在包含搜索中使用一个或多个搜索词,并在文本框中几乎分隔他们键入的每个词。我是一个新的开发人员,不知道如何做到这一点,或者是否已经有一个内置的功能来涵盖这一点 例如,如果用户在搜索框中键入“Sawyer Tom”,则查询的功能如下: declare @
declare @words as TABLE(wordNum int identity primary key, word nvarchar(max))
insert into @words select [value] from dbo.SplitWords('my dog has fleas')
declare @wordCount int
select @wordCount = COUNT(*) from @words
declare @searchString nvarchar(max)
set @searchString = ''
declare @thisWord nvarchar(max)
set @thisWord = ''
declare @i int
set @i = 1
WHILE @i <= @wordCount
BEGIN
select @thisWord = word from @words where wordNum = @i
if @i = @wordCount
select @searchString = @searchString + @thisWord
else
select @searchString = @searchString + @thisWord + ' NEAR '
SET @i = @i + 1
END
选择BookID、BookTitle
从tblBooks
其中包含(书名“汤姆附近的索耶”)
并返回:
12032汤姆·索耶历险记
目前使用like语句,用户搜索将找不到匹配项
如果用户仅键入Sawyer,则查询的功能应如下所示:
declare @words as TABLE(wordNum int identity primary key, word nvarchar(max))
insert into @words select [value] from dbo.SplitWords('my dog has fleas')
declare @wordCount int
select @wordCount = COUNT(*) from @words
declare @searchString nvarchar(max)
set @searchString = ''
declare @thisWord nvarchar(max)
set @thisWord = ''
declare @i int
set @i = 1
WHILE @i <= @wordCount
BEGIN
select @thisWord = word from @words where wordNum = @i
if @i = @wordCount
select @searchString = @searchString + @thisWord
else
select @searchString = @searchString + @thisWord + ' NEAR '
SET @i = @i + 1
END
选择BookID、BookTitle
从tblBooks
其中包含(书名为“Sawyer”)
返回:
12032汤姆·索耶历险记
72192罗伊·克雷恩的《Buz Sawyer:太平洋战争》(第一卷)
我当前的代码只是将搜索字符串插入查询,如下所示:
declare @words as TABLE(wordNum int identity primary key, word nvarchar(max))
insert into @words select [value] from dbo.SplitWords('my dog has fleas')
declare @wordCount int
select @wordCount = COUNT(*) from @words
declare @searchString nvarchar(max)
set @searchString = ''
declare @thisWord nvarchar(max)
set @thisWord = ''
declare @i int
set @i = 1
WHILE @i <= @wordCount
BEGIN
select @thisWord = word from @words where wordNum = @i
if @i = @wordCount
select @searchString = @searchString + @thisWord
else
select @searchString = @searchString + @thisWord + ' NEAR '
SET @i = @i + 1
END
选择BookID、BookTitle
从tblBooks
其中包含(书名,@Search)
这显然不起作用。如何使用“近”自动分隔每个单词
提前非常感谢您提供的任何帮助
-David首先,您需要将输入@Search变量拆分为单词 下面是一篇带有UDF的博客文章,它将实现这一点: 接下来,您将查看返回的每个单词,并将每个单词与近查询连接起来 大概是这样的:
declare @words as TABLE(wordNum int identity primary key, word nvarchar(max))
insert into @words select [value] from dbo.SplitWords('my dog has fleas')
declare @wordCount int
select @wordCount = COUNT(*) from @words
declare @searchString nvarchar(max)
set @searchString = ''
declare @thisWord nvarchar(max)
set @thisWord = ''
declare @i int
set @i = 1
WHILE @i <= @wordCount
BEGIN
select @thisWord = word from @words where wordNum = @i
if @i = @wordCount
select @searchString = @searchString + @thisWord
else
select @searchString = @searchString + @thisWord + ' NEAR '
SET @i = @i + 1
END
将@words声明为表(wordNum int identity主键,word nvarchar(max))
插入到@words中,从dbo.SplitWords中选择[value](“我的狗有跳蚤”)
声明@wordCount int
从@words中选择@wordCount=COUNT(*)
声明@searchString nvarchar(最大值)
设置@searchString=''
声明@thisWord nvarchar(最大值)
设置@thisWord=''
声明@i int
设置@i=1
而@i