本文主要是学习oracle text 对服务器本地文件进行检索的学习笔记,主要参考oracle全文检索这个电子书和网上的一些例子。
首先将DBA权限赋予你的登录账号
begin
ctx_ddl.create_preference ('my_datastore_prefs', 'FILE_DATASTORE');
ctx_ddl.set_attribute ('my_datastore_prefs', 'path', 'd:\file');
end;
--将服务器(windows)D盘文件夹file设置成oracle读取文件的根目录
begin
ctx_ddl.create_preference('URL_PREF','URL_DATASTORE');
ctx_ddl.set_attribute('URL_PREF','Timeout','300');
end;
--设置URL存储类型(顺便记录下)
begin
ctx_ddl.create_preference('cs_filter', 'CHARSET_FILTER');
ctx_ddl.set_attribute('cs_filter', 'charset', 'UTF8');
end;
--设置字符集为UTF-8,oracle默认为utf-8,txt文件要改存储类型为utf-8
begin
ctx_ddl.create_preference ('my_lexer', 'chinese_lexer');
end;
--中文分析器,据说比CHINESE_VGRAM_LEXER 性能好,但是它是按词组查询 单字不一定能匹--配出来
begin
ctx_ddl.create_preference ('my_chinese_lexer', 'CHINESE_VGRAM_LEXER');
end;
--同样是中文分析器,单字可以查询
CREATE TABLE mydocs( id NUMBER PRIMARY KEY, title VARCHAR2(255), thefile
VARCHAR2(255) );
--建立测试表
INSERT INTO mydocs( id, title, thefile ) VALUES( 1, 'Document1', 'test1.txt');
INSERT INTO mydocs( id, title, thefile ) VALUES( 2, 'Document2', 'test2.txt');
--插入测试数据,文件不用带路径名,放在D:/file目录下
INSERT INTO mydocs( id, title, thefile ) VALUES( 5, '163', 'http://www.163.com');
INSERT INTO mydocs( id, title, thefile ) VALUES( 6, 'qq', 'http://www.qq.com');
--URL的测试数据
COMMIT;
CREATE INDEX mydocs_text_index ON mydocs(thefile) INDEXTYPE IS ctxsys.context
PARAMETERS('datastore my_datastore_prefs filter cs_filter Lexer my_lexer');
--对存储文件路径的字段建立索引
--这时数据库自动建立索引表 其中$I为读取文件内容分析词组后建立的索引
--强制删除索引(有时删不掉)
drop index mydocs_text_index force
SELECT id,title,m.thefile
FROM mydocs m
WHERE contains( thefile, '查询条件' ) > 0;
--查询条件为 txt文件中出现的字符
--模拟真实环境测试时,对大数据量的考虑将测试表建立成分区表,每个表空间对应一个物--理硬盘
--建立Oracle Text索引需要消耗CTXSYS用户默认的临时表空间空间。如果空间不够的话将导--致 ORA-01652 错误。你可以扩展CTXSYS的临时表空间,而不是发出命令的用户默认的临时--表空间。
--更改全文索引建立的表空间
Begin
Ctx_ddl.create_preference('mystore', 'basic_storage'); --建立storage
Ctx_ddl.set_attribute('mystore', --设置参数
'i_table_clause',
'tablespace test_space03 storage (initial 1k)');
Ctx_ddl.set_attribute('mystore',
'k_table_clause',
'tablespace test_space03 storage (initial 1k)');
Ctx_ddl.set_attribute('mystore',
'r_table_clause',
'tablespace users storage (initial 1k) lob
(data) store as (disable storage in row cache)');
Ctx_ddl.set_attribute('mystore',
'n_table_clause',
'tablespace test_space03 storage (initial 1k)');
Ctx_ddl.set_attribute('mystore',
'i_index_clause',
'tablespace test_space03 storage (initial 1k) compress 2');
Ctx_ddl.set_attribute('mystore',
'p_table_clause',
'tablespace test_space03 storage (initial 1k)');
end;
--查询 更改系统默认语言
SELECT * FROM V$NLS_PARAMETERS WHERE PARAMETER = 'NLS_LANGUAGE';
ALTER SESSION SET NLS_LANGUAGE = 'AMERICAN';
--对多语言的支持
begin
CTX_DDL.CREATE_PREFERENCE('TEST_FILE_ENGLISH', 'BASIC_LEXER');
CTX_DDL.SET_ATTRIBUTE('TEST_FILE_ENGLISH', 'MIXED_CASE', 'YES');
ctx_ddl.create_preference ('TEST_FILE_CHINESE', 'CHINESE_VGRAM_LEXER');
CTX_DDL.CREATE_PREFERENCE('TEST_FILE_MULTI_LEXER', 'MULTI_LEXER');
CTX_DDL.ADD_SUB_LEXER('TEST_FILE_MULTI_LEXER', 'DEFAULT', 'TEST_ENGLISH');
CTX_DDL.ADD_SUB_LEXER('TEST_FILE_MULTI_LEXER', 'SIMPLIFIED CHINESE', 'TEST_FILE_CHINESE', 'CHINESE');
end;
--最终建立在分区表支持多语言的文件存储类型的全文索引
CREATE INDEX test_text_index ON test(thefile) INDEXTYPE IS CTXSYS.CONTEXT
PARAMETERS ('datastore my_datastore_prefs filter cs_filter LEXER TEST2_FILE_MULTI_LEXER LANGUAGE COLUMN thefile');
--更新索引,当文件改变时,需要执行该命令更新索引
begin
ctx_ddl.sync_index('mydocs_text_index ', '2M');
end;
--建立job 自动更新索引(job这里没有测试)
VARIABLE job1 number;
BEGIN
DBMS_JOB.SUBMIT(:job1,'CTX_DDL.SYNC_INDEX(''mydocs_text_index'', ''2M'');', SYSDATE, 'SYSDATE + (1/24/4)');
commit;
END;
--查看系统索引状态
Select * from ctxsys.ctx_indexes --(idx_status 索引状态)
-----以下是其他常用索引方式-------
CREATE INDEX mydocs_text_index ON mydocs(title) INDEXTYPE IS ctxsys.context
PARAMETERS('filter cs_filter Lexer my_lexer');
--对title字段内容建立索引
CREATE INDEX mydocs_text_index ON mydocs(thefile) INDEXTYPE IS ctxsys.context parameters
( 'Datastore URL_PREF' );
--URL索引的建立方式
oracle text 学起来确实挺麻烦,网上例子不多,所以记录下来。
最终由于我们的系统数据量太大,避免加重数据库负担,改用Lucene了。
头一次写笔记 写的比较乱,只是把以前测试时的SQL代码粘上去简单的说明
oracle全文检索.pdf 有兴趣的可以看下
首先将DBA权限赋予你的登录账号
begin
ctx_ddl.create_preference ('my_datastore_prefs', 'FILE_DATASTORE');
ctx_ddl.set_attribute ('my_datastore_prefs', 'path', 'd:\file');
end;
--将服务器(windows)D盘文件夹file设置成oracle读取文件的根目录
begin
ctx_ddl.create_preference('URL_PREF','URL_DATASTORE');
ctx_ddl.set_attribute('URL_PREF','Timeout','300');
end;
--设置URL存储类型(顺便记录下)
begin
ctx_ddl.create_preference('cs_filter', 'CHARSET_FILTER');
ctx_ddl.set_attribute('cs_filter', 'charset', 'UTF8');
end;
--设置字符集为UTF-8,oracle默认为utf-8,txt文件要改存储类型为utf-8
begin
ctx_ddl.create_preference ('my_lexer', 'chinese_lexer');
end;
--中文分析器,据说比CHINESE_VGRAM_LEXER 性能好,但是它是按词组查询 单字不一定能匹--配出来
begin
ctx_ddl.create_preference ('my_chinese_lexer', 'CHINESE_VGRAM_LEXER');
end;
--同样是中文分析器,单字可以查询
CREATE TABLE mydocs( id NUMBER PRIMARY KEY, title VARCHAR2(255), thefile
VARCHAR2(255) );
--建立测试表
INSERT INTO mydocs( id, title, thefile ) VALUES( 1, 'Document1', 'test1.txt');
INSERT INTO mydocs( id, title, thefile ) VALUES( 2, 'Document2', 'test2.txt');
--插入测试数据,文件不用带路径名,放在D:/file目录下
INSERT INTO mydocs( id, title, thefile ) VALUES( 5, '163', 'http://www.163.com');
INSERT INTO mydocs( id, title, thefile ) VALUES( 6, 'qq', 'http://www.qq.com');
--URL的测试数据
COMMIT;
CREATE INDEX mydocs_text_index ON mydocs(thefile) INDEXTYPE IS ctxsys.context
PARAMETERS('datastore my_datastore_prefs filter cs_filter Lexer my_lexer');
--对存储文件路径的字段建立索引
--这时数据库自动建立索引表 其中$I为读取文件内容分析词组后建立的索引
--强制删除索引(有时删不掉)
drop index mydocs_text_index force
SELECT id,title,m.thefile
FROM mydocs m
WHERE contains( thefile, '查询条件' ) > 0;
--查询条件为 txt文件中出现的字符
--模拟真实环境测试时,对大数据量的考虑将测试表建立成分区表,每个表空间对应一个物--理硬盘
--建立Oracle Text索引需要消耗CTXSYS用户默认的临时表空间空间。如果空间不够的话将导--致 ORA-01652 错误。你可以扩展CTXSYS的临时表空间,而不是发出命令的用户默认的临时--表空间。
--更改全文索引建立的表空间
Begin
Ctx_ddl.create_preference('mystore', 'basic_storage'); --建立storage
Ctx_ddl.set_attribute('mystore', --设置参数
'i_table_clause',
'tablespace test_space03 storage (initial 1k)');
Ctx_ddl.set_attribute('mystore',
'k_table_clause',
'tablespace test_space03 storage (initial 1k)');
Ctx_ddl.set_attribute('mystore',
'r_table_clause',
'tablespace users storage (initial 1k) lob
(data) store as (disable storage in row cache)');
Ctx_ddl.set_attribute('mystore',
'n_table_clause',
'tablespace test_space03 storage (initial 1k)');
Ctx_ddl.set_attribute('mystore',
'i_index_clause',
'tablespace test_space03 storage (initial 1k) compress 2');
Ctx_ddl.set_attribute('mystore',
'p_table_clause',
'tablespace test_space03 storage (initial 1k)');
end;
--查询 更改系统默认语言
SELECT * FROM V$NLS_PARAMETERS WHERE PARAMETER = 'NLS_LANGUAGE';
ALTER SESSION SET NLS_LANGUAGE = 'AMERICAN';
--对多语言的支持
begin
CTX_DDL.CREATE_PREFERENCE('TEST_FILE_ENGLISH', 'BASIC_LEXER');
CTX_DDL.SET_ATTRIBUTE('TEST_FILE_ENGLISH', 'MIXED_CASE', 'YES');
ctx_ddl.create_preference ('TEST_FILE_CHINESE', 'CHINESE_VGRAM_LEXER');
CTX_DDL.CREATE_PREFERENCE('TEST_FILE_MULTI_LEXER', 'MULTI_LEXER');
CTX_DDL.ADD_SUB_LEXER('TEST_FILE_MULTI_LEXER', 'DEFAULT', 'TEST_ENGLISH');
CTX_DDL.ADD_SUB_LEXER('TEST_FILE_MULTI_LEXER', 'SIMPLIFIED CHINESE', 'TEST_FILE_CHINESE', 'CHINESE');
end;
--最终建立在分区表支持多语言的文件存储类型的全文索引
CREATE INDEX test_text_index ON test(thefile) INDEXTYPE IS CTXSYS.CONTEXT
PARAMETERS ('datastore my_datastore_prefs filter cs_filter LEXER TEST2_FILE_MULTI_LEXER LANGUAGE COLUMN thefile');
--更新索引,当文件改变时,需要执行该命令更新索引
begin
ctx_ddl.sync_index('mydocs_text_index ', '2M');
end;
--建立job 自动更新索引(job这里没有测试)
VARIABLE job1 number;
BEGIN
DBMS_JOB.SUBMIT(:job1,'CTX_DDL.SYNC_INDEX(''mydocs_text_index'', ''2M'');', SYSDATE, 'SYSDATE + (1/24/4)');
commit;
END;
--查看系统索引状态
Select * from ctxsys.ctx_indexes --(idx_status 索引状态)
-----以下是其他常用索引方式-------
CREATE INDEX mydocs_text_index ON mydocs(title) INDEXTYPE IS ctxsys.context
PARAMETERS('filter cs_filter Lexer my_lexer');
--对title字段内容建立索引
CREATE INDEX mydocs_text_index ON mydocs(thefile) INDEXTYPE IS ctxsys.context parameters
( 'Datastore URL_PREF' );
--URL索引的建立方式
oracle text 学起来确实挺麻烦,网上例子不多,所以记录下来。
最终由于我们的系统数据量太大,避免加重数据库负担,改用Lucene了。
头一次写笔记 写的比较乱,只是把以前测试时的SQL代码粘上去简单的说明
oracle全文检索.pdf 有兴趣的可以看下