oracle全文检索Word文档下载推荐.docx
《oracle全文检索Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《oracle全文检索Word文档下载推荐.docx(53页珍藏版)》请在冰豆网上搜索。
![oracle全文检索Word文档下载推荐.docx](https://file1.bdocx.com/fileroot1/2022-12/11/f4126c7c-4bc9-4ef6-8ec9-a5cb745b3705/f4126c7c-4bc9-4ef6-8ec9-a5cb745b37051.gif)
(1).
配置oracletext
9i之前,OracleText不是默认安装的,必须手工安装。
检查数据库中是否有ctxsys用户
和ctxapp脚色,如果没有这个用户和角色,这意味着你在创建数据库时没有安装oracletext
功能,需要先配置上该功能。
9i安装text
--创建表空间
SQL>
createtablespacedrsysdatafile'
/opt/oracle10g/oradata/10gtest/drsys01.dbf'
size
100m;
connect/assysdba
--创建ctxsys用户
@?
/ctx/admin/dr0csyspassworddefault_tablespace_nametemporary_tablespace_name
connectctxsys/password
--创建数据字典
/ctx/admin/dr0inst@?
/ctx/lib/libctxx9.so
/ctx/admin/defaults/drdefus.sql;
Oracl0g安装text:
/ctx/admin/catctx.sqlctxsysTBS_DRSYStempunlock
Connctxsys/ctxsys
/ctx/admin/defaults/drdefel.sql
(2).
创建测试用户oratext
Alteruserctxsysidentifiedbyctxsysaccountunlock(10g默认安装,帐号被锁定);
Createtablespaceoratextdatafile'
/opt/oracle10g/oradata/10gtest/oratext01.dbf'
2000m;
Createuseroratextidentifiedbyoratectdefaulttablespaceoratexttemporarytablespace
temp;
Alterdatabasetempfile'
/opt/oracle10g/oradata/10gtest/temp01.dbf'
resize2000m;
Createuseroratextidentifiedbyoratextdefaulttablespaceoratexttemporary
tablespacetemp;
Grantresource,connect,ctxapptooratext;
Grantexecuteonctxsys.ctx_clstooratext;
Grantexecuteonctxsys.ctx_ddltooratext;
Grantexecuteonctxsys.ctx_doctooratext;
Grantexecuteonctxsys.ctx_outputtooratext;
Grantexecuteonctxsys.ctx_querytooratext;
Grantexecuteonctxsys.ctx_reporttooratext;
Grantexecuteonctxsys.ctx_thestooratext;
Grantexecuteonctxsys.ctx_ulexertooratext;
查看系统默认的oracletext参数
Selectpre_name,pre_objectfromctx_preferences
(3).
OracleText索引原理
Oracletext索引将文本中所有的字符转化成记号(token),如会转化
成www,taobao,com这样的记号。
Oracle10g里面支持四种类型的索引,context,ctxcat,ctxrule,ctxxpath
索引类型描述查询操作符
CONTEXT用于对含有大量连续文本数据进行检索。
支持word、html、xml、text
等很多数据格式。
支持范围(range)分区,支持并行创建索引(Parallel
indexing)的索引类型。
支持类型:
VARCHAR2,CLOB,BLOB,CHAR,BFILE,
XMLType,andURIType.
DML操作后,需要CTX_DDL.SYNC_INDEX手工同步索引
如果有查询包含多个词语,直接用空格隔开(如oracleitpub)
CONTAINS
CTXCAT适用于混合查询语句(如查询条件包括产品id,价格,描述等)。
适
合于查询较小的具有一定结构的文本段。
具有事务性。
DML操作后,索引会自动进行同步。
操作符:
and,or,>
<
=,between,in
CATSEARCH
CTXRULEUseCTXRULEindextobuildadocumentclassificationorrouting
application.TheCTXRULEindexisanindexcreatedonatable
ofqueries,wherethequeriesdefinetheclassificationor
routingcriteria.
MATCHES
CTXXPATHCreatethisindexwhenyouneedtospeedupexistsNode()queries
onanXMLTypecolumn.
1.
Context索引
Oracletext索引把全部的word转化成记号,context索引的架构是反向索引(inverted
index),每个记号都映射着包含它自己的文本位置,如单词dog可能会有如下的条目
DogDoc1Doc3Doc5
这表示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手工同步索引。
例子:
Createtabledocs(idnumberprimarykey,textvarchar2(200));
Insertintodocsvalues(1,'
<
html>
californiaisastateintheus.<
/html>
'
);
Insertintodocsvalues(2,'
parisisacityinfrance.<
Insertintodocsvalues(3,'
franceisineurope.<
Commit;
/
--建立context索引
Createindexidx_docsondocs(text)
indextypeisctxsys.contextparameters
('
filterctxsys.null_filtersectiongroupctxsys.html_section_group'
--查询
Columntextformata40;
Selectid,textfromdocswherecontains(text,'
france'
)>
0;
idtext
-----------------------------------------
3<
2<
--继续插入数据
Insertintodocsvalues(4,'
losangelesisacityincalifornia.<
Insertintodocsvalues(5,'
mexicocityisbig.<
commit;
city'
--新插入的数据没有查询到
--------------------------------------------
--索引同步
begin
ctx_ddl.sync_index('
idx_docs'
'
2m'
end;
Columntextformata50;
--查到数据
-----------------------------------------------
5<
4<
--or操作符
cityorstate'
--and操作符
cityandstate'
或是
citystate'
--score表示得分,分值越高,表示查到的数据越精确
SELECTSCORE
(1),id,textFROMdocsWHERECONTAINS(text,'
oracle'
1)>
Context类型的索引不会自动同步,这需要在进行Dml后,需要手工同步索引。
与context索引相对
于的查询操作符为contains
语法:
Contains(
[schema.]column,
text_queryvarchar2
[,labelnumber])
Returnnumber;
indextypeisctxsys.context:
表示所定义的索引类型为context
2.
Ctxcat索引
用在多列混合查询中
Ctxcat可以利用indexset建立一个索引集,把一些经常与ctxcat查询组合使用的查询列添加
到索引集中。
比如你在查询一个商品名时,还需要查询生产日期,价格,描述等,你可可以
将这些列添加到索引集中。
oracle将这些查询封装到catsearch操作中,从而提高全文索引
的效率。
在一些实时性要求较高的交易上,context的索引不能自动同步显然是个问题,ctxcat
则会自动同步索引(原文:
Thisexamplecreatesacatalogindexforanauctionsitethatsells
electronicequipmentsuchascamerasandCDplayers.Newinventoryisaddedeverydayanditem
descriptions,biddates,andpricesmustbestoredtogether.Theapplicationrequiresgoodresponse
timeformixedqueries.Thekeyistodeterminewhatcolumnsusersfrequentlysearchsothatwe
cancreateasuitableCTXCATindex.QueriesonthistypeofindexareissuedwiththeCATSEARCH
operator.)
Createtableauction(Item_idnumber,Titlevarchar2(100),Category_idnumber,Price
number,Bid_closedate);
Insertintoauctionvalues(1,'
nikoncamera'
1,400,'
24-oct-2002'
Insertintoauctionvalues(2,'
olympuscamera'
1,300,'
25-oct-2002'
Insertintoauctionvalues(3,'
pentaxcamera'
1,200,'
26-oct-2002'
Insertintoauctionvalues(4,'
canoncamera'
1,250,'
27-oct-2002'
--确定你的查询条件(很重要)
Determinethatallqueriessearchthetitlecolumnforitemdescriptions
--建立索引集
ctx_ddl.create_index_set('
auction_iset'
ctx_ddl.add_index('
'
price'
/*sub-indexa*/
--建立索引
Createindexauction_titlexonauction(title)indextypeisctxsys.ctxcat
parameters('
indexsetauction_iset'
Columntitleformata40;
Selecttitle,pricefromauctionwherecatsearch(title,'
camera'
orderbyprice'
)>
Titleprice
-------------------------
Pentaxcamera200
Canoncamera250
Olympuscamera300
Nikoncamera400
Insertintoauctionvalues(5,'
aigocamera'
1,10,'
Insertintoauctionvalues(6,'
lencamera'
1,23,'
--测试索引是否自动同步
price<
=100'
0;
aigocamera10
lencamera23
添加多个子查询到索引集:
ctx_ddl.drop_index_set('
/*sub-indexA*/
price,bid_close'
/*sub-indexB*/
dropindexauction_titlex;
SELECT*FROMauctionWHERECATSEARCH(title,'
price=200orderbybid_close'
orderbyprice,bid_close'
任何的Dml操作后,Ctxcat的索引会自动进行同步,不需要手工去执行,与ctxcat索引相对
应的查询操作符是catsearch.
Catsearch(
Text_queryvarchar2,
Structured_queryvarchar2,
catsearch(text,'
dog'
foo>
15'
)
bar='
SMITH'
foobetween1and15'
foo=1andabc=123'
3.
Ctxrule索引
Thefunctionofaclassificationapplicationistoperformsomeactionbasedondocumentcontent.
Theseactionscanincludeassigningacategoryidtoadocumentorsendingthedocumenttoauser.
Theresultisclassificationofadocument.
Createtablequeries(query_idnumber,query_stringvarchar2(80));
insertintoqueriesvalues(1,'
insertintoqueriesvalues(2,'
larryorellison'
insertintoqueriesvalues(3,'
oracleandtext'
insertintoqueriesvalues(4,'
marketshare'
Createindexqueryxonqueries(query_string)indextypeisctxsys.ctxrule;
Columnquery_stringformata35;
Selectquery_id,query_stringfromqueries
wherematches(query_string,
oracleannouncedthatitsmarketshareindatabases
increasedoverthelastyear.'
query_idquery_string
---------------------------------------------
1oracle
4marketshare
4.
Ctxxpath索引
CreatethisindexwhenyouneedtospeedupexistsNode()queriesonanXMLTypecolumn
以上是oracletext索引的几种类型,通常用的比较多的是context和ctxcat索引。
语法参考:
接下来,我们来重新看一下索引的内部处理流程,先从Datasotre开始
类别描述
Datastore从哪儿得到数据
Filter数据如何转化成文本
Lexer正在索引什么语言
Wordlist如何展开词根和模糊查询
Storage索引怎么存储
StopList哪些词和主题不被索引
SectionGroup允许分区查询吗,如何定义文