轉 一個全文檢索索引完整的例子

來源:互聯網
上載者:User

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 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.