1 ----1 准备工作 解锁ctxsys用户并授权-----
2
3 --解锁ctxsys
4 alter user ctxsys account unlock;
5 --修改ctxsys密码(如果ctxsys用户过期了的话,需要重新设置一下密码)
6 alter user ctxsys identified by ctxsys;
7
8 --授予权限给oa这个用户--oa可以改为实际使用的用户
9 grant execute on ctx_ddl to oa;
10
11
12 ----2 准备工作 创建分析器、分词器、过滤词组-----
13 --创建分析器
14 exec ctx_ddl.create_preference ('my_lexer', 'chinese_vgram_lexer');
15 --创建切词器
16 exec CTX_DDL.CREATE_POLICY('MY_POLICY', LEXER => 'my_lexer');
17 --创建分词器(函数)
18 create or replace function p_split_chinese(p_input in varchar2)
19 return varchar2 as
20 v_tab CTX_DOC.TOKEN_TAB;
21 v_return VARCHAR2(32767);
22 begin
23 CTX_DOC.POLICY_TOKENS('my_policy',p_input,v_tab);
24 for i in 1..v_tab.count loop
25 v_return := v_return || ',' || v_tab(i).token;
26 end loop;
27 return LTRIM(v_return,',');
28 end;
29 /
30 --测试函数p_split_chinese
31 select p_split_chinese('中国重庆') from dual;
32
33 -- 创建过滤词组
34 exec ctx_ddl.create_stoplist('my_stoplist');
35 --往my_stoplist中加入要过滤的词,这些词将不会参加搜索
36 exec ctx_ddl.add_stopword('my_stoplist','有限公司');
37
38
39
40
41 -----3 创建索引(使用前面创建的分词器、过虑词组)-----
42 create index MY_TEST_INDEX on Test_Table(Text) indextype is CTXSYS.CONTEXT parameters('lexer my_lexer stoplist my_stoplist');
43
44
45 ----4、查询(使用索引)-----
46 --不使用分词器(rownum<=1000这个限制根据实际情况调整)
47 select score(1),t.* from Test_Table t where contains(Text,'系统管理员',1)>0 and rownum<=1000 order by score(1) desc;
48 --使用分词器(rownum<=1000这个限制根据实际情况调整)
49 select score(1),t.* from Test_Table t where contains(Text,p_split_chinese('系统管理员'),1)>0 and rownum<=1000 order by score(1) desc;
50
51
52
53 ----5、索引维护----
54 --同步索引(有新增数据或者修改数据后要重新执行此条语句,否则新的数据不会参与搜索)
55 exec ctx_ddl.sync_index('MY_TEST_INDEX');
56 --优化索引
57 exec ctx_ddl.optimize_index('MY_TEST_INDEX','full');
58
59