oracle全文检索笔记

时间:2023-03-08 15:40:24

1、删除词法解析器

exec ctx_ddl.drop_preference('my_lexer');

2、创建中文词法解析器

exec ctx_ddl.create_preference ('my_lexer', 'chinese_vgram_lexer');

3、创建全文索引,多字段

EXEC ctx_ddl.create_preference('ctx_idx_jdw_person_pref','MULTI_COLUMN_DATASTORE');

EXEC ctx_ddl.set_attribute('ctx_idx_jdw_person_pref ','columns','name,address');

CREATE INDEX ctx_idx_jdw_person ON jdw_person(name) INDEXTYPE IS ctxsys.CONTEXT PARAMETERS('DATASTORE ctx_idx_jdw_person_pref lexer my_lexer');

4、切词

exec CTX_DDL.CREATE_POLICY('MY_POLICY', LEXER => 'my_lexer');

create or replace function p_split_chinese(p_input in varchar2)
return varchar2 as
v_tab CTX_DOC.TOKEN_TAB;
v_return VARCHAR2(323767);
begin
CTX_DOC.POLICY_TOKENS('my_policy',p_input,v_tab);
for i in 1..v_tab.count loop
v_return := v_return || ',' || v_tab(i).token;
end loop;
return LTRIM(v_return,',');
end;
/

5、同步及优化

exec ctx_ddl.sync_index('ctx_idx_jdw_person');

exec ctx_ddl.optimize_index('ctx_idx_jdw_person', 'full');

6、创建定时任务,定期优化和同步域索引

SQL> create or replace procedure hsp_sync_index as
2 begin
3 ctx_ddl.sync_index('id_cont_msg');
4 end;
5 /

Procedure created.

Elapsed: 00:00:00.08
SQL> VARIABLE jobno number;
SQL> BEGIN
2 DBMS_JOB.SUBMIT(:jobno,'hsp_sync_index();',
3 SYSDATE, 'SYSDATE + (1/24/4)');
4 commit;
5 END;
6 /

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.27
SQL> create or replace procedure hsp_optimize_index as
2 begin
3 ctx_ddl.optimize_index('id_cont_msg','FULL');
4 end;
5 /

SQL> VARIABLE jobno number;
SQL> BEGIN
2 DBMS_JOB.SUBMIT(:jobno,'hsp_optimize_index();',
3 SYSDATE, 'SYSDATE + 1');
4 commit;
5 END;
6 /
Procedure created.

Elapsed: 00:00:00.03

PL/SQL procedure successfully completed.

Elapsed: 00:00:00.02
SQL>