SQL Server全文搜索包含连字符的短语不';我们不能返回预期的结果
我们有一个使用SQLServer2008数据库和全文搜索的应用程序。我试图理解为什么以下搜索行为不同: 首先,一个包含连字符单词的短语,如下所示:SQL Server全文搜索包含连字符的短语不';我们不能返回预期的结果,sql,sql-server,sql-server-2008,tsql,full-text-search,Sql,Sql Server,Sql Server 2008,Tsql,Full Text Search,我们有一个使用SQLServer2008数据库和全文搜索的应用程序。我试图理解为什么以下搜索行为不同: 首先,一个包含连字符单词的短语,如下所示: contains(column_name, '"one two-three-four five"') 第二,一个相同的短语,其中连字符被空格替换: contains(column_name, '"one two three four five"') 全文索引使用英语(1033)区域设置和默认系统停止列表 根据我对其他包含连字号的全文搜索的观察,第
contains(column_name, '"one two-three-four five"')
第二,一个相同的短语,其中连字符被空格替换:
contains(column_name, '"one two three four five"')
全文索引使用英语(1033)区域设置和默认系统停止列表
根据我对其他包含连字号的全文搜索的观察,第一个搜索应该允许在12345
或12345
上进行匹配。相反,它只匹配一二三四五
(而不是一二三四五
)
测试用例 设置:
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;
如果搜索条件中必须使用非字母数字字符(主要是破折号“-”字符),请使用Transact-SQL-LIKE子句,而不是全文或包含谓词在这种情况下,如果无法预测分词器的行为,最好在字符串上运行sys.dm_fts_解析器,了解如何拆分单词并将其存储在内部索引中 例如,在“一二三四五”上运行sys.dm_fts_解析器会产生以下结果-
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查询时看到的结果。全文搜索将单词视为不带空格或标点的字符串。非字母数字字符的出现可以在搜索过程中“打断”单词。因为SQL Server全文搜索是一个基于单词的引擎,所以在搜索索引时通常不考虑标点符号,而忽略标点符号。因此,像“CONTAINS(testing,“computerfailure”)”这样的CONTAINS子句会将一行与值“未能找到我的计算机将是昂贵的”匹配 请按照链接查找原因:问题更多地是关于为什么SQL server表现出不同的匹配行为。解决这个问题当然是可行的,但“两三四五”将返回两行对我来说毫无意义,而“一二三四五”则不会。“一二三四”也一样。这真的是预期的行为吗?如果是,为什么?