加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

SQL Server全文搜索包含连字符的短语不会返回预期结果

发布时间:2020-12-12 06:26:06 所属栏目:MsSql教程 来源:网络整理
导读:我们有一个使用SQL Server 2008数据库和全文搜索的应用程序.我试图理解为什么以下搜索的行为不同: 首先,包含带连字符的短语,如下所示: contains(column_name,'"one two-three-four five"') 第二,一个相同的短语,连字符被空格替换: contains(column_name,'"
我们有一个使用SQL Server 2008数据库和全文搜索的应用程序.我试图理解为什么以下搜索的行为不同:

首先,包含带连字符的短语,如下所示:

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

第二,一个相同的短语,连字符被空格替换:

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

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

根据我对包含带连字符的单词的其他全文搜索的观察,第一个应该允许匹配一两三四五或一二二五.相反,它只匹配一个twothreefour五(而不是一个二三四五).

测试用例

建立:

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;

解决方法

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

“如果必须在搜索标题中使用非字母数字字符(主要是短划线’ – ‘字符),请使用Transact-SQL LIKE子句而不是FULLTEXT或CONTAINS谓词.”

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读