Oracle模糊查詢之(5.3認識全文索引之全文索引的原理)Oracle全文檢索索引方面的研究(全) [主文]__Oracle

來源:互聯網
上載者:User
  Oracle全文檢索索引建索引,預存程序,以及java方法調用高亮顯示   Ajax實現全文檢索索引和伺服器端分頁(原創) Oracle全文檢索索引方面的研究(全)  

2010-10-15 10:13:51|  分類:資料庫 |字型大小 訂閱

 

參考百度文檔:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.