在 MS SQL 中创建 FREETEXTTABLE

Create FREETEXTTABLE in MS SQL

我正在尝试创建 FREETEXTTABLE。 我收到以下错误。

消息 7601,级别 16,状态 2,第 1 行

不能在 table 或索引视图 'Flags' 上使用 CONTAINS 或 FREETEXT 谓词,因为它不是全文索引。

我的样品,

CREATE TABLE Flags (Country nvarchar(30) NOT NULL, FlagColors varchar(200));

CREATE UNIQUE CLUSTERED INDEX FlagKey ON Flags(Country);

INSERT Flags VALUES ('France', 'Blue and White and Red');
INSERT Flags VALUES ('Italy', 'Green and White and Red');
INSERT Flags VALUES ('Tanzania', 'Green and Yellow and Black and Yellow and Blue');

SELECT * FROM Flags;
GO

CREATE FULLTEXT CATALOG TestFTCat;
CREATE FULLTEXT INDEX ON Flags(FlagColors) KEY INDEX FlagKey ON TestFTCat;
GO 

SELECT * FROM FREETEXTTABLE (Flags, FlagColors, 'Blue');

以下代码部分由于某种原因未在您的数据库上执行 请先执行以下命令,然后使用 FreeTextTable

CREATE FULLTEXT CATALOG TestFTCat;
CREATE FULLTEXT INDEX ON Flags(FlagColors) KEY INDEX FlagKey ON TestFTCat;

验证您是否安装了 MSSQL Server 2008 全文 "component" 的简单方法是执行以下 T-SQL

SELECT SERVERPROPERTY('IsFullTextInstalled')

如果此 return 的值为“1”,则组件已安装。

否则您必须 在现有 SQL 服务器实例上安装 SQL 服务器全文搜索

  1. 在服务器上下载 "SQL Server Express with Advanced Services" 的最新安装程序(例如保存在 "c:\installers" 中) MSSQL Server 2008 Express with Advanced Services.
  2. 浏览到您保存它的位置并解压缩自解压 EXE(右键单击并单击 "Extract")。
  3. 双击安装程序安装。
  4. 选择 "upgrade existing instance to a new version or edition" 并完成所有步骤。
  5. 我很确定从来没有人问我想要什么功能(即没有机会选择全文搜索)。当 运行 再次安装安装程序时,也无法 "add features to existing instance",所以最终我发现需要执行以下额外步骤...
  6. 转到“控制面板”>“添加或删除程序”
  7. 找到SQL服务器并打开它。 (它问了我一个类似 "uninstalling this while other people are using it will cause them some pain" 的问题——有点吓人,但我说好,然后它问了一个常见的问题:我想卸载还是更改)
  8. 问题:卸载或更改(答案:更改)。
  9. 点击"Add Features"
  10. 浏览安装媒体。在这种情况下,我们可以找到我们解压缩安装程序的地方 "SQL Server Express with Advanced Services" (注意它必须被提取,否则它不会喜欢它)
  11. 经历一堆乱七八糟的步骤。然后系统会询问您 "install new instance" 或 "add features to existing instance"(选择添加功能)。
  12. 在 "Features" 这一步,您可以勾选方框 "Fulltext Search"(万岁!!)
  13. 完成向导,现在一切正常,运行 sql 确认:

    SELECT 服务器属性('IsFullTextInstalled')

现在您可以使用 T-启用全文搜索SQL

-- We'll use Northwind sample database to enable
-- Full Text Search feature using T-SQL code only
USE Northwind
GO

-- We need to enable full text search for Northwind database
-- We will do that with sp_fulltext_database procedure
EXEC sp_fulltext_database 'enable'
-- Create catalog
EXEC sp_fulltext_catalog 'NorthwindCatalog','create'
-- Add some indexes to database
EXEC sp_fulltext_table 'Customers', 'create', 'NorthwindCatalog', 'pk_customers'
EXEC sp_fulltext_table 'Orders', 'create', 'NorthwindCatalog', 'pk_orders'
-- add columns for searching to full text search index
EXEC sp_fulltext_column 'Customers', 'CompanyName', 'add'
EXEC sp_fulltext_column 'Customers', 'ContactName', 'add'
EXEC sp_fulltext_column 'Customers', 'Address', 'add'
EXEC sp_fulltext_column 'Customers', 'City', 'add'
EXEC sp_fulltext_column 'Orders', 'ShipName', 'add'
EXEC sp_fulltext_column 'Orders', 'ShipAddress', 'add'
EXEC sp_fulltext_column 'Orders', 'ShipCity', 'add'
-- Activate full text search indexes
EXEC sp_fulltext_table 'Customers','activate'
EXEC sp_fulltext_table 'Orders','activate'
-- start full population of catalog
EXEC sp_fulltext_catalog 'NorthwindCatalog', 'start_full'

现在您可以使用 CONTAINSFREETEXTCONTAINSTABLEFREETEXTTABLE 个关键字。例如,假设我想检查名字是 Maria 或 Ana 的所有联系人:

USE Northwind
GO

SELECT CustomerId, ContactName, CompanyName, Address, City
FROM Customers c INNER JOIN
CONTAINSTABLE(Customers, (ContactName), '"Maria" OR "Ana"') AS KEY_TBL
ON c.CustomerId = KEY_TBL.[KEY]
ORDER BY KEY_TBL.RANK DESC

此 SQL 查询将 return 结果如下图所示: