oracle全文检索

转载自:http://blog.sina.com.cn/s/blog_613fee860100yhyz.html

 

1.需要对ctxsys用户解锁,以获得ctx_ddl包的操作权。
进入system用户,输入如下命令,解锁ctxsys用户
alter user ctxsys account unlock;
然后将ctx_ddl包的操作权限赋给ctfs用户。
也是在system用户下,输入如下命令,赋予目标用户ctx_ddl包操作权限
grant execute on ctx_ddl to ctfs;
至此,准备工作已经完成了。

2.创建分析器
这里使用chinese_vgram_lexer这个分词器,用ctfs用户登录,执行下面的命令,创建分析器。
exec ctx_ddl.create_preference(‘ctfs_lexer‘,‘chinese_vgram_lexer‘);
这句话的意思是,创建一个“chinese_vgram_lexer”分析器,名称为ctfs_lexer。

3.创建过滤词组
用ctfs用户登录,执行下面的命令,创建过滤词组
exec ctx_ddl.create_stoplist(‘ctfs_stoplist‘);
创建过滤词组成功以后,需要自定义需要过滤的词组
exec ctx_ddl.add_stopword(‘ctfs_stoplist‘,‘有限公司‘);

4.创建表索引
create index ctfs_buy_service_index on ctfs_buy_service(keyword_) indextype is ctxsys.context parameters(‘lexer ctfs_lexer stoplist ctfs_stoplist‘);
create index ctfs_supply_service_index on ctfs_supply_service(keyword_) indextype is ctxsys.context parameters(‘lexer ctfs_lexer stoplist ctfs_stoplist‘);

5.使用索引
select score(1),b.* from ctfs_buy_service where contains(keyword_,‘java开发‘,1)>0 order by score(1) desc;
这里的score是oracle全文检索对关键字的匹配程度所计算的分数,contains里的最后一个参数“1”就是对这个分数的一个标识

6.索引优化(用于数据变动时:添加、删除、修改)
索引同步:
exec ctx_ddl.sync_index(‘ctfs_buy_service_index‘);
exec ctx_ddl.sync_index(‘ctfs_supply_service_index‘);
索引优化:
exec ctx_ddl.optimize_index(‘ctfs_buy_service_index‘,full);
exec ctx_ddl.optimize_index(‘ctfs_supply_service_index‘,full);

7.用户输入关键词切词
要实现这种效果,需要用到oracle 10g的新特性,可以将传入的关键词先进行切词,然后在进行检索。
首先需要先创建一个POLICY过程
exec ctx_ddl.create_policy(‘ctfs_policy‘,lexer => ‘ctfs_lexer‘);

这里创建了一个名称为ctfs_policy的policy过程,分析器用到了前面创建的ctfs_lexer分析器。
写一个oracle函数,来处理关键词切词:
create or replace function p_split_chinese(p_input in varchar2)
    return varchar2
    as
        v_tab ctx_doc.token_tab;
        v_return varchar2(32767);
    begin
        ctx_doc.policy_tokens(‘ctfs_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;
/

在plsql 中执行这个函数
select * from ctfs_buy_service where contains(keyword_,p_split_chinese(‘java开发‘),1)>0;

oracle全文检索,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。