全文索引创建实例

create   table   testIndex

(

          id   int   identity(1,1)   primary   key,

          nm   varchar(100)   unique   not   null,

          sex   varchar(10)

)

create UNIQUE index UQ__testIndex__0DAF0CB0

on testindex(nm)

insert   into   testindex  

          select   'aaabbb','m'   union   all

          select   'bbb','w'   union   all

          select   'ccc','w'   union   all

          select   'ddd','m'

insert   into   testindex  

          select   '麦蒂未伤愈中途退出训练复出时间再度成疑','北京' 

go

--创建全文目录

sp_fulltext_catalog   'abc','create'    

go

--创建全文索引(‘表名‘,’创建/删除‘,’全文目录名‘,’约束名‘)

sp_fulltext_table   'testindex','create','abc','UQ__testIndex__0DAF0CB0'

go

--添加列到全文索引(‘表名‘,’列名‘,’添加/删除‘)

sp_fulltext_column   'testindex','nm','add'

go

--建立全文索引

--activate,是激活表的全文检索能力,也就是在全文目录中注册该表

execute sp_fulltext_table 'testindex','activate'

go

--填充全文索引目录

execute sp_fulltext_catalog 'abc','start_full'

go

--检查全文目录填充情况

While fulltextcatalogproperty('abc','populateStatus')<>0

begin

--如果全文目录正处于填充状态,则等待30秒后再检测一次

waitfor delay '0:0:30'

end

--全文目录填充完成后,即可使用全文目录检索


SELECT   *   FROM   testindex   WHERE   CONTAINS(nm,   '麦蒂')

/*

id          nm                                                                                                 sex       

----------- --------------------------------------------- ------------------------------------------------ ----------

5           麦蒂未伤愈中途退出训练复出时间再度成疑                                                             北京

(所影响的行数为 1 行)

*/

insert   into   testindex  

          select   '麦蒂未伤愈中途退出训练复出时间再度成疑12121','北京' 

go

SELECT   *   FROM   testindex   WHERE   CONTAINS(nm,   '麦蒂')

-----No result

/*

id          nm                                                                                                 sex       

----------- --------------------------------------------- ------------------------------------------------ ----------

5           麦蒂未伤愈中途退出训练复出时间再度成疑                                                             北京

(所影响的行数为 1 行)

*/

go

--填充全文索引目录

execute sp_fulltext_catalog 'abc','start_full'

go

--检查全文目录填充情况

While fulltextcatalogproperty('abc','populateStatus')<>0

begin

--如果全文目录正处于填充状态,则等待30秒后再检测一次

waitfor delay '0:0:30'

end

SELECT   *   FROM   testindex   WHERE   CONTAINS(nm,   '麦蒂')

go

/*

id          nm                                                                                                   sex       

----------- ---------------------------------------------------------------------------------------------------- ----------

6           麦蒂未伤愈中途退出训练复出时间再度成疑12121                                                                             北京

5           麦蒂未伤愈中途退出训练复出时间再度成疑                                                                                  北京

(所影响的行数为 2 行)

*/

sp_fulltext_table  'testindex','drop'

go

sp_fulltext_catalog   'abc','drop' 

go 

drop table testIndex