oracle文档内容搜索示例
connect system/system
--创建新的用户
create user liyao identified by liyao;
--将建立文本应用的角色赋予用户
grant ctxapp to liyao;
grant create any directory to liyao;
grant connect,resource to liyao;
connect liyao/liyao;
--创建一个带blob字段的表
create table testdoc
(
id number primary key,
title varchar2(20),
doc blob
);
--建立记录,为blob字段初始化
INSERT INTO TESTDOC VALUES(1,'chunwang.doc',EMPTY_BLOB());
INSERT INTO TESTDOC VALUES(2,'将进酒.doc',EMPTY_BLOB());
INSERT INTO TESTDOC VALUES(2,'liangzhouci.xls',EMPTY_BLOB());
commit;
--建立目录对象
CREATE OR REPLACE DIRECTORY BFILE_DIR AS 'c:\temp';
--将文件导入到BLOB字段
DECLARE
lobd BLOB;
fils BFILE := BFILENAME('BFILE_DIR','chunwang.doc');
amt INTEGER := 19456;
BEGIN
SELECT DOC INTO lobd FROM testdoc WHERE id = 1 FOR UPDATE;
dbms_lob.fileopen(fils, dbms_lob.file_readonly);
dbms_lob.loadfromfile(lobd, fils, amt);
COMMIT;
dbms_lob.fileclose(fils);
END;
/
DECLARE
lobd BLOB;
fils BFILE := BFILENAME('BFILE_DIR','将进酒.xls');
amt INTEGER := 19456;
BEGIN
SELECT DOC INTO lobd FROM test_blob WHERE id = 2 FOR UPDATE;
dbms_lob.fileopen(fils, dbms_lob.file_readonly);
dbms_lob.loadfromfile(lobd, fils, amt);
COMMIT;
dbms_lob.fileclose(fils);
END;
/
DECLARE
lobd BLOB;
fils BFILE := BFILENAME('BFILE_DIR','liangzhouci.xls');
amt INTEGER := 13824;
BEGIN
SELECT DOC INTO lobd FROM testdoc WHERE id = 2 FOR UPDATE;
dbms_lob.fileopen(fils, dbms_lob.file_readonly);
dbms_lob.loadfromfile(lobd, fils, amt);
COMMIT;
dbms_lob.fileclose(fils);
END;
/
--建立索引环境
begin
--删除已有的环境
--ctx_ddl.drop_preference('TESTDOC_DIR');
--ctx_ddl.drop_preference('TESTDOC_FILTER');
--ctx_ddl.drop_preference('TESTDOC_LEXER');
--需要索引的文字直接存放在BLOB字段中
ctx_ddl.create_preference('TESTDOC_DIR','DIRECT_DATASTORE');
--使用格式文件
ctx_ddl.create_preference('TESTDOC_FILTER','INSO_FILTER');
--使用中文语法
ctx_ddl.create_preference('TESTDOC_LEXER','CHINESE_VGRAM_LEXER');
end;
/
--建立索引
CREATE INDEX IDX_TESTDOC ON TESTDOC(DOC) INDEXTYPE IS CTXSYS.CONTEXT PARAMETERS('lexer scott.TESTDOC_LEXER filter scott.TESTDOC_FILTER datastore scott.TESTDOC_DIR');
--查询结果:
SQL> select id,title from testdoc where contains(doc,'人生得意须尽欢')>0;
--使用或的操作
SQL> select id,title from testdoc where contains(doc,'杨柳|对月')>0;
ID TITLE
---------- --------------------
3 凉州词.xls
2 将进酒.doc