2012-07-25 44 views
7

我们有一个使用SQL Server 2008数据库和全文搜索的应用程序。我试图理解为什么下面的搜索行为不同:SQL Server全文搜索包含连字符的短语不会返回预期结果

首先,含有连字符的单词短语,如:

contains(column_name, '"one two-three-four five"') 

第二,相同的短语,其中连字符用空格代替:

contains(column_name, '"one two three four five"') 

全文索引使用英语(1033)语言环境和默认系统停止列表。

从我对包含带连字符的其他全文搜索的观察,第一个应允许在one two three four fiveone twothreefour five之间匹配。相反,它只匹配one twothreefour five(而不是one two-three-four five)。


测试用例

设置:

create table ftTest 
(
    Id int identity(1,1) not null, 
    Value nvarchar(100) not null, 
    constraint PK_ftTest primary key (Id) 
); 

insert ftTest (Value) values ('one two-three-four five'); 
insert ftTest (Value) values ('one twothreefour five'); 

create fulltext catalog ftTest_catalog; 
create fulltext index on ftTest (Value language 1033) 
    key index PK_ftTest on ftTest_catalog; 
GO 

查询:

--returns one match 
select * from ftTest where contains(Value, '"one two-three-four five"') 

--returns two matches 
select * from ftTest where contains(Value, '"one two three four five"') 
select * from ftTest where contains(Value, 'one and "two-three-four five"') 
select * from ftTest where contains(Value, '"one two-three-four" and five') 
GO 

清理:

drop fulltext index on ftTest 
drop fulltext catalog ftTest_catalog; 
drop table ftTest; 

回答

7

http://support.microsoft.com/default.aspx?scid=kb;en-us;200043

“哪里非字母数字字符必须在搜索性判据(‘ - ’主要是连字符)使用的,而不是使用的FULLTEXT Transact-SQL的LIKE子句或CONTAINS谓词”。

+1

的问题更多的是关于*为什么* SQL服务器表现出对匹配不同的行为。解决这个问题肯定是可行的,但对我来说,“两三四五”将返回两行是毫无意义的,但“一二三四”不会。 “一二三四”同上。这真的是预期的行为?如果是这样,为什么? – Laviak 2012-07-26 07:35:15

5

在这样的情况下,您无法预知断路器的行为,在您的字符串上运行sys.dm_fts_parser以了解单词如何拆分并存储内部索引。

例如,在下面运行的“‘一,二,三四个五’的业绩sys.dm_fts_parser -

select * from sys.dm_fts_parser('"one two-three-four five"', 1033, NULL, 0) 
--edited-- 
1 0 1 Exact Match one 
1 0 2 Exact Match two-three-four 
1 0 2 Exact Match two 
1 0 3 Exact Match three 
1 0 4 Exact Match four 
1 0 5 Exact Match five 

你可以从返回的结果看,字断路器解析字符串并输出六种形式,可以解释您在运行CONTAINS查询时看到的结果。

1

全文搜索将单词视为没有空格或标点符号的字符串。非字母数字字符的出现可能会在搜索过程中“破坏”一个字词。由于SQL Server全文搜索是基于单词的引擎,标点符号通常不被考虑,并且在搜索索引时被忽略。因此,CONTAINS子句(如CONTAINS(测试,“计算机故障”))将与一行匹配值“找不到我的计算机将会很昂贵”。

请按照链接为什么:https://support.microsoft.com/en-us/kb/200043

相关问题