2007年01月22日

再次實作10g全文檢索

這次因為工作的需求再次測驗全文檢索的功能,是否都符合專案所需,記錄一下測試流程:

我的資料來源
SQL> select * from testfind;

ID TEXT
---------- --------------------------------------------------------------------------------
1 中文
2 台灣人
3 台北
4 我住在台北
5 我是台北人
6 我是火星人不是地球人
7 別問我是誰
8 什麼跟什麼
9 這就是愛台灣啦

建立一般的全文檢索的index
SQL> create index testfind_index on testfind(text) indextype is ctxsys.context;

查看結果,發現並不是我所需要的
SQL> select * from dr$testfind_index$i;

TOKEN_TEXT TOKEN_TYPE TOKEN_FIRST TOKEN_LAST TOKEN_COUNT TOKEN_INFO
---------------------------------------------------------------- ---------- ----------- ----------- ----------- ----------
中文 0 1 1 1 <BLOB>
什麼跟什麼 0 8 8 1 <BLOB>
別問我是誰 0 7 7 1 <BLOB>
台北 0 3 3 1 <BLOB>
台灣人 0 2 2 1 <BLOB>
我住在台北 0 4 4 1 <BLOB>
我是台北人 0 5 5 1 <BLOB>
我是火星人不是地球人 0 6 6 1 <BLOB>
這就是愛台灣啦 0 9 9 1 <BLOB>

刪除舊的index
SQL> drop index testfind_index;

Index dropped

建立較聰明的index
SQL> begin
2 ctx_ddl.create_preference('testfind_lexer','chinese_lexer');
3 end;
4 /

PL/SQL procedure successfully completed

SQL> create index testfind_index on testfind(text) indextype is ctxsys.context parameters('lexer testfind_lexer');

Index created

再次查看index的內容,內容已是我所需要的
SQL> select * from dr$testfind_index$i;

TOKEN_TEXT TOKEN_TYPE TOKEN_FIRST TOKEN_LAST TOKEN_COUNT TOKEN_INFO
---------------------------------------------------------------- ---------- ----------- ----------- ----------- ----------
不是 0 6 6 1 <BLOB>
中文 0 1 1 1 <BLOB>
人 0 5 6 2 <BLOB>
什麼 0 8 8 1 <BLOB>
住在 0 4 4 1 <BLOB>
別問 0 7 7 1 <BLOB>
台北 0 3 5 3 <BLOB>
台灣 0 9 9 1 <BLOB>
台灣人 & nbsp; 0 2 2 1 <BLOB>
問我 0 7 7 1 <BLOB>
啦 0 9 9 1 <BLOB>
在 0 4 4 1 <BLOB>
地球 0 6 6 1 <BLOB>
愛 0 9 9 1 <BLOB>
我住 0 4 4 1 <BLOB>
我是 0 5 7 3 <BLOB>
是 0 5 6 2 <BLOB>
是愛 0 9 9 1 <BLOB>
是誰 0 7 7 1 <BLOB>
火星 0 6 6 1 <BLOB>

TOKEN_TEXT TOKEN_TYPE TOKEN_FIRST TOKEN_LAST TOKEN_COUNT TOKEN_INFO
---------------------------------------------------------------- ---------- ----------- ----------- ----------- ----------
誰 0 7 7 1 <BLOB>
跟 0 8 8 1 <BLOB>
這就 0 9 9 1 <BLOB>

23 rows selected

最常用的語法
SQL> select score(1),text from testfind where contains(text,'台北',1)>0 order by score(1) desc;

SCORE(1) TEXT
---------- --------------------------------------------------------------------------------
5 台北
5 我住在台北
5 我是台北人

測試一些boolean的語法
SQL> select text from testfind where contains(text,'台北 not 我是',1)>0 order by score(1) desc;

TEXT
--------------------------------------------------------------------------------
台北
我住在台北

SQL> select text from testfind where contains(text,'台北 - 我是',1)>0 order by score(1) desc;

TEXT
--------------------------------------------------------------------------------
台北
我住在台北

附記一些優化跟更新index的語法
SQL> begin
2 ctx_ddl.sync_index('testfind_index','2M');
3 end;
4 /

PL/SQL procedure successfully completed



SQL> begin
2 ctx_ddl.optimize_index('testfind_index','full');
3 end;
4 /


心得:建立跟使用上是較沒有什困難的地方,不過選擇lexer時需要考慮一下你的資料量大小,欄位格式、是否立即更新indez的內容等等來選擇所需的類型。並且小心你的index會成長很快,注意整体hd的使用量
參考link:http://epub.itpub.net/4/1.htm

Posted by my_work at 樂多Roodo! │17:12 │回應(0)引用(0)DB
樂多分類:網路/3C 共同主題:Oracle 工具:編輯本文
Ads by Roodo! 

引用URL

http://cgi.blog.roodo.com/trackback/2662957