转 一个全文检索完整的例子

来源:互联网 发布:刚做淘宝快递怎么便宜 编辑:程序博客网 时间:2024/04/30 04:51

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,   '麦蒂')
-----这个时候进行select实际上得不到理想的结果,还是老数据,没有增加的这一条
/*

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 



原创粉丝点击