MS SQL-FULLTEXT INDEX-哪一栏

时间:2019-02-12 11:14:46

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

我在MS SQL Server上的FULLTEXT索引有一些问题。我在下面创建了一个简单的解决方案(请忽略明显的数据设计问题)。我已经开始在数据库中使用全文索引,这与将LIKE与通配符一起使用有很多好处,特别是在具有多列可搜索的大型表上的速度方面。

create fulltext catalog FullTextCatalog as default
select * from sys.fulltext_catalogs
GO

IF OBJECT_ID('dbo.Books') IS NOT NULL
    DROP TABLE  dbo.Books
GO

CREATE TABLE dbo.Books
(
BookID          INT IDENTITY(1,1),
BookTitle       NVARCHAR(255),
BookDescription NVARCHAR(1000),
BookCategory    NVARCHAR(255),
CONSTRAINT [PK_Books] PRIMARY KEY CLUSTERED
(
BookID ASC
)
)
GO

INSERT INTO dbo.Books (BookTitle, BookDescription, BookCategory)
VALUES
('Book1', 'This is the first book in the series', 'Sport'),
('Book2', 'This is the second book in the series', 'Sport'),
('Book3', 'This is the third sport book in the series', 'Sport'),
('Book4', 'This is the NEW book in the series about nature', 'Nature')
GO

CREATE FULLTEXT INDEX ON dbo.Books
(
BookTitle       LANGUAGE 'Neutral',
BookDescription LANGUAGE 'Neutral',
BookCategory    LANGUAGE 'Neutral'
)
KEY INDEX [PK_Books] ON ([FullTextCatalog], FILEGROUP [PRIMARY])
WITH (CHANGE_TRACKING = AUTO, STOPLIST = SYSTEM);
GO

WAITFOR DELAY '00:00:03'

SELECT *
FROM dbo.Books
WHERE FREETEXT((BookTitle, BookDescription, BookCategory), 'sport')

我的要求之一是知道我的搜索字符串在哪一列中。因此,使用上表我需要知道在记录3的BookDescription和BookCategory列中找到了搜索词“ sport”,而在记录中只有BookCategory了1和2。下面的代码是我为搜索索引而设计的,尽管这几乎达到了我的目标,但仍然存在许多问题。特别是我需要使用LIKE和通配符搜索索引的事实,这首先使我无法使用全文索引。如果我要查找的部分单词(端口)与FREETEXT相比,LIKE将返回不同的结果。

SELECT  kw.*, c.name
FROM    sys.dm_fts_index_keywords_by_document(DB_ID(), OBJECT_ID('dbo.Books')) kw
JOIN    sys.all_columns c
ON      kw.column_id = c.column_id
WHERE   display_term like '%sport%'
AND     c.object_id = OBJECT_ID('dbo.Books')
ORDER BY kw.document_id, c.column_id

还有其他人不知道如何找到全文索引位于搜索词的哪些列吗?

0 个答案:

没有答案