一、建表
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;