`
thomas0988
  • 浏览: 472701 次
  • 性别: Icon_minigender_1
  • 来自: 南阳
社区版块
存档分类
最新评论

【引用】Oracle全文检索方面的研究(全1)

阅读更多

 

参考百度文档:

http://wenku.baidu.com/view/c53e9e36a32d7375a417801a.html

 

1、准备流程        

 

1.1检查和设置数据库角色

首先检查数据库中是否有CTXSYS用户和CTXAPP脚色。如果没有这个用户和角色,意味着你的数据库创建时未安装intermedia功能。你必须修改数据库以安装这项功能。 默认安装情况下,ctxsys用户是被锁定的,因此要先启用ctxsys的用户。

 

默认ctxsys用户是被锁定的且密码即时失效,所以我们以sys用户进入em,然后修改ctxsys用户的状态和密码。如图:

 

 

1.2 赋权 

                测试用户以之前已经建好的foo用户为例,以该用户下的T_DOCNEWS为例

先以sys用户dba身份登录,对foo赋resource,connect权限

GRANT resource, connect  to foo;

 

再以ctxsys用户登录并对foo用户赋权

GRANT  ctxapp  TO foo;

GRANT execute ON ctxsys. ctx_cls  TO foo;

GRANT execute ON ctxsys. ctx_ddl  TO foo;

GRANT execute ON ctxsys. ctx_doc  TO foo;

GRANT execute ON ctxsys. ctx_output TO foo;

GRANT execute ON ctxsys. ctx_query TO foo;

GRANT execute ON ctxsys. ctx_report  TO foo;

GRANT execute ON ctxsys. ctx_thes  TO foo;

GRANT execute ON ctxsys. ctx_ulexer TO foo;

 

查看系统默认的oracle text 参数

Select pre_name, pre_object from ctx_preferences

 

 

2、Oracle Text 索引原理

Oracle text 索引将文本中所有的字符转化成记号(token),如www.taobao.com 会转化

成www,taobao,com 这样的记号。

Oracle10g 里面支持四种类型的索引,context,ctxcat,ctxrule,ctxxpath

 

 

 

2.1 Context 索引

Oracle text 索引把全部的word 转化成记号,context 索引的架构是反向索引(inverted

index),每个记号都映射着包含它自己的文本位置,如单词dog 可能会有如下的条目

这表示dog 在文档doc1,doc3,doc5 中都出现过。索引建好之后,系统中会自动产生

如下DR$MYINDEX$I,DR$MYINDEX$K,DR$MYINDEX$R,DR$MYINDEX$X,MYTABLE5 个表(假设表为

mytable, 索引为myindx) 。Dml 操作后, context 索引不会自动同步, 需要利用

ctx_ddl.sync_index 手工同步索引。

 

例子:

Create table docs (id number primary key, text varchar2(200));

Insert into docs values(1, '<html>california is a state in the us.</html>');

Insert into docs values(2, '<html>paris is a city in france.</html>');

Insert into docs values(3, '<html>france is in europe.</html>');

Commit;

/

--建立context 索引

Create index idx_docs on docs(text)

indextype is ctxsys.context parameters

('filter ctxsys.null_filter section group ctxsys.html_section_group');

--查询

Column text format a40;     --字符串截为40位显示。

Select id, text from docs where contains(text, 'france') > 0;

 

id text

---------- -------------------------------

3 <html>france is in europe.</html>

2 <html>paris is a city in france.</html>

--继续插入数据

Insert into docs values(4, '<html>los angeles is a city in california.</html>');

Insert into docs values(5, '<html>mexico city is big.</html>');

commit;

Select id, text from docs where contains(text, 'city') > 0;--新插入的数据没有查询到

 

id text

--------------------------------------------

2 <html>paris is a city in france.</html>

 

 

--索引同步

begin

ctx_ddl.sync_index('idx_docs', '2m');  --使用2M同步索引

end;

--查询

Column text format a50;

Select id, text from docs where contains(text, 'city') > 0; --查到数据

id text

-----------------------------------------------

5 <html>mexico city is big.</html>

4 <html>los angeles is a city in california.</html>

2 <html>paris is a city in france.</html>

 

 

-- or 操作符

Select id, text from docs where contains(text, 'city or state ') > 0;

--and 操作符

Select id, text from docs where contains(text, 'city and state ') > 0;

或是

Select id, text from docs where contains(text, 'city state ') > 0;

 

--score 表示得分,分值越高,表示查到的数据越精确

SELECT SCORE(1), id, text FROM docs WHERE CONTAINS(text, 'oracle', 1) > 0;

Context 类型的索引不会自动同步,这需要在进行Dml 后,需要手工同步索引。与context 索引相对于的查询操作符为contains

 

2.2 Ctxcat 索引

用在多列混合查询中

Ctxcat 可以利用index set 建立一个索引集,把一些经常与ctxcat 查询组合使用的查询列添加到索引集中。比如你在查询一个商品名时,还需要查询生产日期,价格,描述等,你可可以将这些列添加到索引集中。oracle 将这些查询封装到catsearch 操作中,从而提高全文索引的效率。在一些实时性要求较高的交易上,context 的索引不能自动同步显然是个问题,ctxcat则会自动同步索引

 

 

例子:

Create table auction(Item_id number,Title varchar2(100),Category_id number,Price number,Bid_close date);

Insert into auction values(1, 'nikon camera', 1, 400, '24-oct-2002');

Insert into auction values(2, 'olympus camera', 1, 300, '25-oct-2002');

Insert into auction values(3, 'pentax camera', 1, 200, '26-oct-2002');

Insert into auction values(4, 'canon camera', 1, 250, '27-oct-2002');

Commit;

 

 

/

--确定你的查询条件(很重要)

--Determine that all queries search the title column for item descriptions

--建立索引集

begin

ctx_ddl.create_index_set('auction_iset');

ctx_ddl.add_index('auction_iset','price'); /* sub-index a*/

end;

--建立索引

Create index auction_titlex on auction(title) indextype is ctxsys.ctxcat

parameters ('index set auction_iset');

Column title format a40;

Select title, price from auction where catsearch(title, 'camera', 'order by price')> 0;

 

 

Title price

--------------- ----------

Pentax camera 200

Canon camera 250

Olympus camera 300

Nikon camera 400

 

 

Insert into auction values(5, 'aigo camera', 1, 10, '27-oct-2002');

Insert into auction values(6, 'len camera', 1, 23, '27-oct-2002');

commit;

 

 

 

/

--测试索引是否自动同步

Select title, price from auction where catsearch(title, 'camera',

'price <= 100')>0;

 

Title price

--------------- ----------

aigo camera 10

len camera 23

 

 

添加多个子查询到索引集:

 

begin

ctx_ddl.drop_index_set('auction_iset');

ctx_ddl.create_index_set('auction_iset');

ctx_ddl.add_index('auction_iset','price'); /* sub-index A */

ctx_ddl.add_index('auction_iset','price, bid_close'); /* sub-index B */

end;

 

drop index auction_titlex;

 

Create index auction_titlex on auction(title) indextype is ctxsys.ctxcat

parameters ('index set auction_iset');

SELECT * FROM auction WHERE CATSEARCH(title, 'camera','price = 200 order by bid_close')>0;

SELECT * FROM auction WHERE CATSEARCH(title, 'camera','order by price, bid_close')>0;

 

任何的Dml 操作后,Ctxcat 的索引会自动进行同步,不需要手工去执行,与ctxcat 索引相对应的查询操作符是catsearch.

语法:

Catsearch(

[schema.]column,

Text_query varchar2,

Structured_query varchar2,

Return number;

例子:

catsearch(text, 'dog', 'foo > 15')

catsearch(text, 'dog', 'bar = ''SMITH''')

catsearch(text, 'dog', 'foo between 1 and 15')

catsearch(text, 'dog', 'foo = 1 and abc = 123')

 

2.3 Ctxrule 索引

The function of a classification application is to perform some action based on document content.

These actions can include assigning a category id to a document or sending the document to a user.

The result is classification of a document.

 

 

 

例子:

Create table queries (query_id number,query_string varchar2(80));

insert into queries values (1, 'oracle');

insert into queries values (2, 'larry or ellison');

insert into queries values (3, 'oracle and text');

insert into queries values (4, 'market share');

commit;

 

Create index queryx on queries(query_string) indextype is ctxsys.ctxrule;

Column query_string format a35;

Select query_id,query_string from queries

where matches(query_string,

'oracle announced that its market share in databases

increased over the last year.')>0;

query_id query_string

---------- -----------------------------------

1 oracle

4 market share

 

 

在一句话中建立索引匹配查询

 

2.4 Ctxxpath 索引

Create this index when you need to speed up existsNode() queries on an XMLType column

 

 

 

3. 索引的内部处理流程

 

 

3.1 Datastore 属性

数据检索负责将数据从数据存储(例如 web 页面、数据库大型对象或本地文件系统)

中取出,然后作为数据流传送到下一个阶段。Datastore 包含的类型有Direct datastore,

Multi_column_datastore, Detail_datastore, File_datastore, Url_datastore, User_datastore,

Nested_datastore。

 

 

 

 

 

 

 

3.1.1.Direct datastore

支持存储数据库中的数据,单列查询.没有attributes 属性

支持类型:char, varchar, varchar2, blob, clob, bfile,or xmltype.

例子:

Create table mytable(id number primary key, docs clob);

Insert into mytable values(111555,'this text will be indexed');

Insert into mytable values(111556,'this is a direct_datastore example');

Commit;

--建立 direct datastore

Create index myindex on mytable(docs)

indextype is ctxsys.context

parameters ('datastore ctxsys.default_datastore');

Select * from mytable where contains(docs, 'text') > 0;

 

3.1.2.Multi_column_datastore

适用于索引数据分布在多个列中

the column list is limited to 500 bytes

支持number 和date 类型,在索引之前会先转化成textt

raw and blob columns are directly concatenated as binary data.

不支持long, long raw, nchar, and nclob, nested table

 

Create table mytable1(id number primary key, doc1 varchar2(400),doc2 clob,doc3

clob);

 

Insert into mytable1 values(1,'this text will be indexed','following example creates amulti-column ','denotes that the bar column ');

Insert into mytable1 values(2,'this is a direct_datastore example','use this datastore when your text is stored in more than one column','the system concatenates the text columns');

Commit;

 

/

--建立 multi datastore 类型

Begin

Ctx_ddl.create_preference('my_multi', 'multi_column_datastore');

Ctx_ddl.set_attribute('my_multi', 'columns', 'doc1, doc2, doc3');

End;

--建立索引

Create index idx_mytable on mytable1(doc1)indextype is ctxsys.context

parameters('datastore my_multi')

Select * from mytable1 where contains(doc1,'direct datastore')>0;

Select * from mytable1 where contains(doc1,'example creates')>0;

注意:检索时,检索词对英文,必须是有意义的词,比如,

Select * from mytable1 where contains(doc1,' more than one column ')>0;

可以查出第二条纪录,但你检索more将没有显示,因为more在那句话中不是有意义的一个词。

 

--只更新从表,看是否能查到更新的信息

Update mytable1 set doc2='adladlhadad this datastore when your text is stored test' where

id=2;

Begin

Ctx_ddl.sync_index('idx_mytable');

End;

Select * from mytable1 where contains(doc1,'adladlhadad')>0; --没有记录

Update mytable1 set doc1='this is a direct_datastore example' where id=2; --更新主表

 

Begin

Ctx_ddl.sync_index('idx_mytable');--同步索引

End;

Select * from mytable1 where contains(doc1,'adladlhadad')>0; -查到从表的更新

对于多列的全文索引可以建立在任意一列上,但是,在查询时指定的列必须与索引时指定的

列保持一致,只有索引指定的列发生修改,oracle 才会认为被索引数据发生了变化,仅修改

其他列而没有修改索引列,即使同步索引也不会将修改同步到索引中.

也就是说,只有更新了索引列,同步索引才能生效,,要更改其他列的同时也要再写一次即可。

在多列中,对任意一列建立索引即可,更新其他列的同时,在update那个列,同步索引一次即可看到效果了。

 

 

 

3.1.3 Detail_datastore

适用于主从表查询(原文:use the detail_datastore type for text stored directly in the database in

detail tables, with the indexed text column located in the master table)

因为真正被索引的是从表上的列,选择主表的那个列作为索引并不重要,但是选定之后,查

询条件中就必须指明这个列

主表中的被索引列的内容并没有包含在索引中

DETAIL_DATASTORE 属性定义

 

 

 

 

 

 

例子:

create table my_master –建立主表

(article_id number primary key,author varchar2(30),title varchar2(50),body varchar2(1));

create table my_detail –建立从表

(article_id number, seq number, text varchar2(4000),

constraint fr_id foreign key (ARTICLE_ID) references my_master (ARTICLE_ID));

--模拟数据

insert into my_master values(1,'Tom','expert on and on',1);

insert into my_master values(2,'Tom','Expert Oracle Database Architecture',2);

commit;

insert into my_detail values(1,1,'Oracle will find the undo information for this transaction

either in the cached

undo segment blocks (most likely) or on disk ');

insert into my_detail values(1,2,'if they have been flushed (more likely for very large

transactions).');

insert into my_detail values(1,3,'LGWR is writing to a different device, then there is no

contention for

redo logs');

insert into my_detail values(2,1,'Many other databases treat the log files as');

insert into my_detail values(2,2,'For those systems, the act of rolling back can be

disastrous');

commit;

--建立 detail datastore

begin

ctx_ddl.create_preference('my_detail_pref', 'DETAIL_DATASTORE');

ctx_ddl.set_attribute('my_detail_pref', 'binary', 'true');

ctx_ddl.set_attribute('my_detail_pref', 'detail_table', 'my_detail');

ctx_ddl.set_attribute('my_detail_pref', 'detail_key', 'article_id');

ctx_ddl.set_attribute('my_detail_pref', 'detail_lineno', 'seq');

ctx_ddl.set_attribute('my_detail_pref', 'detail_text', 'text');

end;

--创建索引

CREATE INDEX myindex123 on my_master(body) indextype is ctxsys.context

parameters('datastore my_detail_pref');

select * from my_master where contains(body,'databases')>0

--只更新从表信息,看是否还能查到

update my_detail set text='undo is generated as a result of the DELETE, blocks are modified,

and redo is sent over to

the redo log buffer' where article_id=2 and seq=1

begin

ctx_ddl.sync_index('myindex123','2m'); --同步索引

end;

select * from my_master where contains(body,'result of the DELETE')>0 –没有查到刚才的更新

--跟新从表后,更新主表信息

update my_master set body=3 where body=2

begin

ctx_ddl.sync_index('myindex123','2m');

end;

select * from my_master where contains(body,'result of the DELETE')>0 –查到数据

如果更新了子表中的索引列,必须要去更新主表索引列来使oracle 认识到被索引数据发生变

化(这个可以通过触发器来实现)。

 

 

 

 

3.1.4 File_datastore

适用于检索本地服务器上的文件(原文:The FILE_DATASTORE type is used for text stored in

files accessed through the local file system.)

多个路径标识:Unix 下冒号分隔开如path1:path2:pathn Windows 下用分号;分隔开

create table mytable3(id number primary key, docs varchar2(2000));

 

insert into mytable3 values(111555,'1.txt');

 

insert into mytable3 values(111556,'1.doc');

 

commit;

 

--建立 file datastore

begin

ctx_ddl.create_preference('COMMON_DIR2','FILE_DATASTORE');

ctx_ddl.set_attribute('COMMON_DIR2','PATH','D:\search');

end;

--建立索引

create index myindex3 on mytable3(docs) indextype is ctxsys.context parameters ('datastore COMMON_DIR2');

select * from mytable3 where contains(docs,'word')>0; --查询

 

--暂时测试支持doc,txt

 

 

3.1.5 Url_datastore

适用于检索internet 上的信息,数据库中只需要存储相应的url 就可以

 

 

例子:

create table urls(id number primary key, docs varchar2(2000));

insert into urls values(111555,'http://context.us.oracle.com');

insert into urls values(111556,'http://www.sun.com');

insert into urls values(111557,'http://www.itpub.net');

insert into urls values(111558,'http://www.ixdba.com');

commit;

/

--建立url datastore

begin

ctx_ddl.create_preference('URL_PREF','URL_DATASTORE');

ctx_ddl.set_attribute('URL_PREF','Timeout','300');

end;

--建立索引

create index datastores_text on urls (docs) indextype is ctxsys.context parameters

( 'Datastore URL_PREF' );

select * from urls where contains(docs,'Aix')>0

若相关的url 不存在,oracle 并不会报错,只是查询的时候找不到数据而已。

oracle 中仅仅保存被索引文档的url 地址,如果文档本身发生了变化,必须要通过修改索引

列(url 地址列)的方式来告知oracle,被索引数据已经发生了变化。

 

 

 

3.1.6.User_datastore

Use the USER_DATASTORE type to define stored procedures that synthesize documents during

indexing. For example, a user procedure might synthesize author, date, and text columns into one

document to have the author and date information be part of the indexed text.

 

 

3.1.7 Nested_datastore

全文索引支持将数据存储在嵌套表中

 

 

 

3.1.8.参考脚本

--建立direct_store

Create index myindex on mytable(docs)

indextype is ctxsys.context

parameters ('datastore ctxsys.default_datastore');

--建立mutil_column_datastore

Begin

Ctx_ddl.create_preference('my_multi', 'multi_column_datastore');

Ctx_ddl.set_attribute('my_multi', 'columns', 'doc1, doc2, doc3');

End;

Create index idx_mytable on mytable1(doc1)indextype is ctxsys.context

parameters('datastore my_multi')

--建立file_datafilestore

begin

ctx_ddl.create_preference('COMMON_DIR','FILE_DATASTORE');

ctx_ddl.set_attribute('COMMON_DIR','PATH','/opt/tmp');

end;

create index myindex on mytable1(docs) indextype is ctxsys.context parameters ('datastore

COMMON_DIR');

--建立url_datastore

begin

ctx_ddl.create_preference('URL_PREF','URL_DATASTORE');

ctx_ddl.set_attribute('URL_PREF','Timeout','300');

end;

create index datastores_text on urls (docs) indextype is ctxsys.context parameters

( 'Datastore URL_PREF' );

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics