T-SQL FTS CONTAINS单个字符'n'匹配数字

时间:2018-06-01 08:40:31

标签: tsql full-text-search contains sql-server-2016

我正在使用SQL Server 2016并遇到一种非常奇怪的行为。

我有一个为FTS索引的表。

按照... CONTAINS(N'"a*"')这样的单个字符进行搜索时,它会返回包含从a开始的单词的所有记录 - 好吧。

但是,如果我按照... CONTAINS(N'"n*"')这样的单个字符 n 进行搜索,则会返回不仅包含从n开始但还包含<的单词的记录强>位

我不能谷歌没有提到这种行为。我相信这是一种通配符,但在文档中没有任何相关内容。

为什么会这样?是否存在逃脱序列?

UPD :测试环境

CREATE FULLTEXT CATALOG [ContentCatalog] WITH ACCENT_SENSITIVITY = ON
GO
CREATE TABLE [dbo].[Content]([id] [uniqueidentifier] NOT NULL, [content] [nvarchar](max) NOT NULL,
  CONSTRAINT [PK_Content] PRIMARY KEY CLUSTERED ([id] ASC))
GO
ALTER TABLE [dbo].[Content] ADD CONSTRAINT [DF_Content_id]  DEFAULT (newid()) FOR [id]
GO
INSERT [Content] ([content]) VALUES
    (N'abc def nop'),
    (N'zxc asd qwe'),
    (N'zaq nhy bgt'),
    (N'123 456 789'),
    (N'z12 x34 c56')
GO
CREATE FULLTEXT INDEX ON [Content]([content] LANGUAGE 'English')
KEY INDEX [PK_Content]ON ([ContentCatalog], FILEGROUP [PRIMARY])
WITH (CHANGE_TRACKING = AUTO, STOPLIST = SYSTEM)

SELECT * FROM [dbo].[Content]
SELECT FULLTEXTSERVICEPROPERTY('IsFullTextInstalled') -- should be 1
SELECT * FROM [dbo].[Content] WHERE CONTAINS([Content], N'"a*"') -- returns 2 records
SELECT * FROM [dbo].[Content] WHERE CONTAINS([Content], N'"n*"') -- returns extra record with digits only

1 个答案:

答案 0 :(得分:0)

这不是我所知道的违反规则的答案,但我认为它增加了OP可能希望纳入其问题的有用的额外信息。

有关详细信息,这似乎只能在n中达到一定数量的contains值。下面使用问题中的脚本来构建环境但使用不同的测试值。然后select将包含返回的所有值整理成一行以便于比较:

insert [content] ([content]) values
    (N'abc nop abc'),
    (N'abc nno abc'),
    (N'abc nnn abc'),
    (N'abc nnnnbc'),
    (N'abc nnnnnc'),
    (N'1 2 3'),
    (N'10 11 12'),
    (N'100 101 102'),
    (N'1000 1001 1002'),
    (N'10000 10001 10002')
go
          select 'a*' as ContainsSearch,    stuff((select ' || ' + content from [dbo].[content] where contains([content], N'"a*"') for xml path('')),1,4,'') as ReturnedValues
union all select 'n*' as ContainsSearch,    stuff((select ' || ' + content from [dbo].[content] where contains([content], N'"n*"') for xml path('')),1,4,'') as ReturnedValues
union all select 'nn*' as ContainsSearch,   stuff((select ' || ' + content from [dbo].[content] where contains([content], N'"nn*"') for xml path('')),1,4,'') as ReturnedValues
union all select 'nnn*' as ContainsSearch,  stuff((select ' || ' + content from [dbo].[content] where contains([content], N'"nnn*"') for xml path('')),1,4,'') as ReturnedValues
union all select 'nnnn*' as ContainsSearch, stuff((select ' || ' + content from [dbo].[content] where contains([content], N'"nnnn*"') for xml path('')),1,4,'') as ReturnedValues
union all select 'nnnnn*' as ContainsSearch,stuff((select ' || ' + content from [dbo].[content] where contains([content], N'"nnnnn*"') for xml path('')),1,4,'') as ReturnedValues

结果是:

+----------------+-----------------------------------------------------------------------------------------------------------------------------------------+
| ContainsSearch |                                                             ReturnedValues                                                              |
+----------------+-----------------------------------------------------------------------------------------------------------------------------------------+
| a*             | abc nop abc || abc nno abc || abc nnn abc || abc nnnnbc || abc nnnnnc                                                                   |
| n*             | abc nop abc || abc nno abc || abc nnn abc || abc nnnnbc || abc nnnnnc || 10 11 12 || 100 101 102 || 1000 1001 1002 || 10000 10001 10002 |
| nn*            | abc nno abc || abc nnn abc || abc nnnnbc || abc nnnnnc || 10 11 12 || 100 101 102 || 1000 1001 1002 || 10000 10001 10002                |
| nnn*           | abc nnn abc || abc nnnnbc || abc nnnnnc                                                                                                 |
| nnnn*          | abc nnnnbc || abc nnnnnc                                                                                                                |
| nnnnn*         | abc nnnnnc                                                                                                                              |
+----------------+-----------------------------------------------------------------------------------------------------------------------------------------+