Oracle資料庫建某欄位全文全文檢索
一、建表
create table myindextest (tid varchar2(50) primary key,
btext BLOB)
二、建索引及引數
1、中文分詞及分詞屬性設定
begin
ctx_ddl.create_preference('cnlex','CHINESE_LEXER');
ctx_ddl.create_preference('mywordlist','BASIC_WORDLIST');
ctx_ddl.set_attribute('mywordlist','PREFIX_INDEX','TRUE');
ctx_ddl.set_attribute('mywordlist','PREFIX_MIN_LENGTH',1);
ctx_ddl.set_attribute('mywordlist','PREFIX_MAX_LENGTH',8);
ctx_ddl.set_attribute('mywordlist','SUBSTRING_INDEX','YES');
END;
2、設定中文過濾(中文編碼)
begin
ctx_ddl.create_preference('cs_filter','CHARSET_FILTER');
ctx_ddl.set_attribute('cs_filter','charset','UTF8');
end;
3、建立索引
create index idx_myindextest on myindextest(btext)
indextype is ctxsys.context
parameters('DATASTORE CTXSYS.DIRECT_DATASTORE
FILTER cs_filter
LEXER CNLEX
WORDLIST MYWORDLIST');
三、查詢
1、檢視分詞情況
SELECT * FROM dr$idx_myindextest$I
2、通過關鍵詞查詢記錄
select * from myindextest where contains(btext,'正常')>0
四、同步 優化
begin
Ctx_ddl.sync_index('idx_myindextest','2M');
End;
begin
Ctx_dll.optimize_index('myidx','full');
End;
五、定時任務執行執行作業進行同步
create or replace procedure sync is begin execute immediate 'alter index idx_myindextest rebuild online' || ' parameters ( ''sync'' )' execute immediate 'alter index idx_myindextest rebuild online' || ' parameters ( ''optimize full maxtime unlimited'' )' end sync; / Set ServerOutput on declare v_job number; begin Dbms_Job.Submit ( job => v_job, what => 'sync;', next_date => sysdate, /* default */ interval => 'sysdate + 1/720' /* = 1 day / ( 24 hrs * 30 min) = 2 mins */ ); Dbms_Job.Run ( v_job ); Dbms_Output.Put_Line ( 'Submitted as job # ' || to_char ( v_job ) ); end;