SQL 服务器全文搜索未返回预期的行
SQL Server FullText Search not returning expected rows
我正在 SQL Server 2016 上练习使用全文搜索。下面是重现我的问题的完整步骤代码。我不明白为什么 none 我的查询 return 有任何结果(如果你 运行 这些步骤,完成后不要忘记删除 FTSEARCH 数据库)。
请注意,我故意设置了 STOPLIST=OFF,因为我想搜索数值,而数字包含在系统停止列表中
--1. Confirm FullText is installed
SELECT
CASE FULLTEXTSERVICEPROPERTY('IsFullTextInstalled')
WHEN 1 THEN 'Full-Text installed.'
ELSE 'Full-Text is NOT installed.'
END;
--2. Create DB for testing
CREATE DATABASE FTSEARCH
--3. Enable FullText Search
USE FTSEARCH
GO
EXEC sp_fulltext_database 'enable';
-- Confirm that FullTextSearch is enabled
SELECT is_fulltext_enabled
FROM sys.databases
WHERE database_id = DB_ID()
--4. Create fulltext catalog
CREATE FULLTEXT CATALOG FullTextCatalog AS DEFAULT;
-- Verify catalog exists
SELECT *
FROM sys.fulltext_catalogs
--5. Create sample table and populate some data
IF OBJECT_ID('[dbo].[Test_FT_Search]') IS NOT NULL DROP TABLE [dbo].[Test_FT_Search]
CREATE TABLE [dbo].[Test_FT_Search]
(
[PKID] [int] NOT NULL IDENTITY(1, 1),
[Account_Number] [nvarchar] (16) NULL,
[Code] [nvarchar] (6)
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Test_FT_Search] ADD CONSTRAINT [PK_PKID] PRIMARY KEY CLUSTERED ([PKID]) ON [PRIMARY]
GO
INSERT [dbo].[Test_FT_Search]
(
[Account_Number],
[Code]
)
VALUES
( N'4445002020875', N'0A6133' ),
( N'4445002020883', N'0B6133' ),
( N'4445002020891', N'0C6134' ),
( N'4445002020909', N'0F6135' ),
( N'4445002020917', N'0F6136' )
--6. Create FullText Index
-- NOTE: the STOPLIST is OFF on purpose because I want to search on numbers
CREATE FULLTEXT INDEX ON [dbo].[Test_FT_Search] (
[Account_Number],
[Code]
)
KEY INDEX [PK_PKID]
WITH
CHANGE_TRACKING = AUTO,
STOPLIST=OFF
;
--7. Force FullText index population
ALTER FULLTEXT INDEX ON [dbo].[Test_FT_Search] START FULL POPULATION
--8. Perform some searches
-- None of these queries return any resuls
SELECT *
FROM [dbo].[Test_FT_Search]
WHERE CONTAINS([Account_Number],'20208') ;
SELECT *
FROM [dbo].[Test_FT_Search]
WHERE CONTAINS([Account_Number],'444500') ;
SELECT *
FROM [dbo].[Test_FT_Search]
WHERE CONTAINS([Code],'F6') ;
SELECT *
FROM [dbo].[Test_FT_Search]
WHERE CONTAINS([Code],'3') ;
• 单词或短语。
• 单词或短语的前缀。
SELECT *
FROM [dbo].[Test_FT_Search]
WHERE CONTAINS([Account_Number],'4445002020891'); --a word or phrase
SELECT *
FROM [dbo].[Test_FT_Search]
WHERE CONTAINS([Account_Number],'"44450020208*"') ; --prefix of word: prefix+asterisk in double quotes
我正在 SQL Server 2016 上练习使用全文搜索。下面是重现我的问题的完整步骤代码。我不明白为什么 none 我的查询 return 有任何结果(如果你 运行 这些步骤,完成后不要忘记删除 FTSEARCH 数据库)。
请注意,我故意设置了 STOPLIST=OFF,因为我想搜索数值,而数字包含在系统停止列表中
--1. Confirm FullText is installed
SELECT
CASE FULLTEXTSERVICEPROPERTY('IsFullTextInstalled')
WHEN 1 THEN 'Full-Text installed.'
ELSE 'Full-Text is NOT installed.'
END;
--2. Create DB for testing
CREATE DATABASE FTSEARCH
--3. Enable FullText Search
USE FTSEARCH
GO
EXEC sp_fulltext_database 'enable';
-- Confirm that FullTextSearch is enabled
SELECT is_fulltext_enabled
FROM sys.databases
WHERE database_id = DB_ID()
--4. Create fulltext catalog
CREATE FULLTEXT CATALOG FullTextCatalog AS DEFAULT;
-- Verify catalog exists
SELECT *
FROM sys.fulltext_catalogs
--5. Create sample table and populate some data
IF OBJECT_ID('[dbo].[Test_FT_Search]') IS NOT NULL DROP TABLE [dbo].[Test_FT_Search]
CREATE TABLE [dbo].[Test_FT_Search]
(
[PKID] [int] NOT NULL IDENTITY(1, 1),
[Account_Number] [nvarchar] (16) NULL,
[Code] [nvarchar] (6)
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Test_FT_Search] ADD CONSTRAINT [PK_PKID] PRIMARY KEY CLUSTERED ([PKID]) ON [PRIMARY]
GO
INSERT [dbo].[Test_FT_Search]
(
[Account_Number],
[Code]
)
VALUES
( N'4445002020875', N'0A6133' ),
( N'4445002020883', N'0B6133' ),
( N'4445002020891', N'0C6134' ),
( N'4445002020909', N'0F6135' ),
( N'4445002020917', N'0F6136' )
--6. Create FullText Index
-- NOTE: the STOPLIST is OFF on purpose because I want to search on numbers
CREATE FULLTEXT INDEX ON [dbo].[Test_FT_Search] (
[Account_Number],
[Code]
)
KEY INDEX [PK_PKID]
WITH
CHANGE_TRACKING = AUTO,
STOPLIST=OFF
;
--7. Force FullText index population
ALTER FULLTEXT INDEX ON [dbo].[Test_FT_Search] START FULL POPULATION
--8. Perform some searches
-- None of these queries return any resuls
SELECT *
FROM [dbo].[Test_FT_Search]
WHERE CONTAINS([Account_Number],'20208') ;
SELECT *
FROM [dbo].[Test_FT_Search]
WHERE CONTAINS([Account_Number],'444500') ;
SELECT *
FROM [dbo].[Test_FT_Search]
WHERE CONTAINS([Code],'F6') ;
SELECT *
FROM [dbo].[Test_FT_Search]
WHERE CONTAINS([Code],'3') ;
• 单词或短语。
• 单词或短语的前缀。
SELECT *
FROM [dbo].[Test_FT_Search]
WHERE CONTAINS([Account_Number],'4445002020891'); --a word or phrase
SELECT *
FROM [dbo].[Test_FT_Search]
WHERE CONTAINS([Account_Number],'"44450020208*"') ; --prefix of word: prefix+asterisk in double quotes