使用SQL Server 2008中的全文搜索按特定文本查找行

时间:2018-01-30 10:05:09

标签: sql sql-server full-text-search

我想在使用全文搜索搜索特定文本时显示正确的结果。我为每个需求创建了一个示例表和特定查询。以下大多数查询未返回预期值。

CREATE TABLE t1(id INT identity, ftcol NVarChar(100))
GO

INSERT t1(ftcol) 
VALUES ('<field>hello</field>'), ('hello'), ('world'),
       ('hello again'), ('/hello/'), 
       ('Portals/1/'), ('Portals:1'), ('{"SpInitiatedSso')
GO

CREATE fulltext catalog cat1
GO

-- now create the view, must be schemabound view so it must explicitly
--specify columns and 2 part name for table
CREATE VIEW myview 
WITH SCHEMABINDING 
AS 
     SELECT id, ftcol 
     FROM dbo.t1 
GO

-- create the index on the view to be used as full text key index
CREATE UNIQUE CLUSTERED INDEX idx ON myview(id)
GO

CREATE FULLTEXT INDEX ON myview(ftcol) KEY INDEX idx ON cat1
GO

DECLARE @x nvarchar(100)='<field>hello</field>'

SELECT * 
FROM myview 
WHERE CONTAINS(ftcol, @x)     -- should return '<field>hello</field>'

DECLARE @x1 NVARCHAR(100)='Portals/1/'

SELECT * 
FROM myview 
WHERE CONTAINS(ftcol, @x1)    -- should return 'Portals/1/'

DECLARE @x2 nvarchar(100)='"hello again"'

SELECT * 
FROM myview 
WHERE CONTAINS(ftcol, @x2)    -- should return 'hello again'

DECLARE @x3 nvarchar(100)='/hello/'

SELECT * 
FROM myview 
WHERE CONTAINS(ftcol, @x3)    -- should return '/hello/'

DECLARE @x4 NVARCHAR(100)='"Portals:1"'

SELECT * 
FROM myview 
WHERE CONTAINS(ftcol, @x4)    --should return 'Portals:1'

DECLARE @x5 NVARCHAR(100)='{"SpInitiatedSso'

SELECT *
FROM myview 
WHERE CONTAINS(ftcol, @x5)    -- should return '{"SpInitiatedSso', throws error

更新:我已经为每个条件设置了预期的返回值。

0 个答案:

没有答案