oracle全文检索.docx
- 文档编号:3745976
- 上传时间:2022-11-25
- 格式:DOCX
- 页数:17
- 大小:38.86KB
oracle全文检索.docx
《oracle全文检索.docx》由会员分享,可在线阅读,更多相关《oracle全文检索.docx(17页珍藏版)》请在冰豆网上搜索。
oracle全文检索
全文检索(oracletext)OracleText使Oracle9i具备了强大的文本检索能力和智能化的文本管理能力,OracleText是Oracle9i采用的新名称,在oracle8/8i中被称为oracleintermediatext,oracle8以前是oraclecontextcartridge。
OracleText的索引和查找功能并不局限于存储在数据库中的数据。
它可以对存储于文件系统中的文档进行检索和查找,并可检索超过150种文档类型,包括MicrosoftWord、PDF和XML。
OracleText查找功能包括模糊查找、词干查找(搜索mice和查找mouse)、通配符、相近性等查找方式,以及结果分级和关键词突出显示等。
你甚至可以增加一个词典,以查找搭配词,并找出包含该搭配词的文档。
Oracletext需要为可检索的数据项建立索引,用户才能够通过搜索查找内容,索引进程是根据管道建模的,在这个管道中,数据经过一系列的转换后,将其关键字会添加到索引中。
该索引进程分为多个阶段,如下图1.数据检索(Datastore):
只是将数据从数据存储(例如web页面、数据库大型对象或本地文件系统)中取出,然后作为数据流传送到下一个阶段。
2.过滤(Filter):
过滤器负责将各种文件格式的数据转换为纯文本格式,索引管道中的其他组件只能处理纯文本数据,不能识别Msword或excel等文件格式。
3.分段(Sectioner):
分段器添加关于原始数据项结构的元数据。
4.词法分析(Lexer):
根据数据项的语言将字符流分为几个字词。
5.索引(Index):
最后一个阶段将关键字添加到实际索引中。
测试环境:
LinuxASrelease4(NahantUpdate3),oracle10g(10.2.0.2.0)内容简介:
本文档主要以实验为主,文档中包含了大量的实验例子,部分测试用例来自document,部分来自网友的测试,所有的例子都在oracle10g中测试通过。
(1).oracletext9i之前,OracleText不是默认安装的,必须手工安装。
检查数据库中是否有ctxsys用户和ctxapp脚色,如果没有这个用户和角色,这意味着你在创建数据库时没有安装oracletext功能,需要先配置上该功能。
9i安装text--创建表空间SQL>createtablespacedrsysdatafile'/opt/oracle10g/oradata/10gtest/drsys01.dbf'size100m;SQL>connect/assysdba--创建ctxsys用户SQL>@?
/ctx/admin/dr0csyspassworddefault_tablespace_nametemporary_tablespace_nameSQL>connectctxsys/password--创建数据字典SQL>@?
/ctx/admin/dr0inst@?
/ctx/lib/libctxx9.soSQL>@?
/ctx/admin/defaults/drdefus.sql;Oracl0g安装text:
SQL>connect/assysdbaSQL>@?
/ctx/admin/catctx.sqlctxsysTBS_DRSYStempunlockConnctxsys/ctxsysSQL>@?
/ctx/admin/defaults/drdefel.sql
(2).oratextAlteruserctxsysidentifiedbyctxsysaccountunlock(10g默认安装,帐号被锁定);Createtablespaceoratextdatafile'/opt/oracle10g/oradata/10gtest/oratext01.dbf'size2000m;Createuseroratextidentifiedbyoratectdefaulttablespaceoratexttemporarytablespacetemp;Alterdatabasetempfile'/opt/oracle10g/oradata/10gtest/temp01.dbf'resize2000m;SQL>Createuseroratextidentifiedbyoratextdefaulttablespaceoratexttemporarytablespacetemp;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).OracleTextOracletext索引将文本中所有的字符转化成记号(token),如会转化成www,taobao,com这样的记号。
Oracle10g里面支持四种类型的索引,context,ctxcat,ctxrule,ctxxpath索引类型描述查询操作符CONTEXT用于对含有大量连续文本数据进行检索。
支持word、html、xml、text等很多数据格式。
支持范围(range)分区,支持并行创建索引(Parallelindexing)的索引类型。
支持类型:
VARCHAR2,CLOB,BLOB,CHAR,BFILE,XMLType,andURIType.DML操作后,需要CTX_DDL.SYNC_INDEX手工同步索引如果有查询包含多个词语,直接用空格隔开(如oracleitpub)CONTAINSCTXCAT适用于混合查询语句(如查询条件包括产品id,价格,描述等)。
适合于查询较小的具有一定结构的文本段。
具有事务性。
DML操作后,索引会自动进行同步。
操作符:
and,or,>,<,=,between,inCATSEARCHCTXRULEUseCTXRULEindextobuildadocumentclassificationorroutingapplication.TheCTXRULEindexisanindexcreatedonatableofqueries,wherethequeriesdefinetheclassificationorroutingcriteria.MATCHESCTXXPATHCreatethisindexwhenyouneedtospeedupexistsNode()queriesonanXMLTypecolumn.1.ContextOracletext索引把全部的word转化成记号,context索引的架构是反向索引(invertedindex),每个记号都映射着包含它自己的文本位置,如单词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,'californiaisastateintheus.');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-----------------------------------------3franceisineurope.2parisisacityinfrance.--继续插入数据Insertintodocsvalues(4,'losangelesisacityincalifornia.');Insertintodocsvalues(5,'mexicocityisbig.');commit;Selectid,textfromdocswherecontains(text,'city')>0;--新插入的数据没有查询到idtext--------------------------------------------2parisisacityinfrance.--索引同步beginctx_ddl.sync_index('idx_docs','2m');end;--查询Columntextformata50;Selectid,textfromdocswherecontains(text,'city')>0;--查到数据idtext-----------------------------------------------5mexicocityisbig.4losangelesisacityincalifornia.2parisisacityinfrance.--or操作符
Selectid,textfromdocswherecontains(text,'cityorstate')>0;--and操作符Selectid,textfromdocswherecontains(text,'cityandstate')>0;或是Selectid,textfromdocswherecontains(text,'citystate')>0;--score表示得分,分值越高,表示查到的数据越精确SELECTSCORE
(1),id,textFROMdocsWHERECONTAINS(text,'oracle',1)>0;Context类型的索引不会自动同步,这需要在进行Dml后,需要手工同步索引。
与context索引相对于的查询操作符为contains语法:
Contains([schema.]column,text_queryvarchar2[,labelnumber])Returnnumber;indextypeisctxsys.context:
表示所定义的索引类型为context2.Ctxcat用在多列混合查询中Ctxcat可以利用indexset建立一个索引集,把一些经常与ctxcat查询组合使用的查询列添加到索引集中。
比如你在查询一个商品名时,还需要查询生产日期,价格,描述等,你可可以将这些列添加到索引集中。
oracle将这些查询封装到catsearch操作中,从而提高全文索引的效率。
在一些实时性要求较高的交易上,context的索引不能自动同步显然是个问题,ctxcat则会自动同步索引(原文:
ThisexamplecreatesacatalogindexforanauctionsitethatsellselectronicequipmentsuchascamerasandCDplayers.Newinventoryisaddedeverydayanditemdescriptions,biddates,andpricesmustbestoredtogether.Theapplicationrequiresgoodresponsetimeformixedqueries.ThekeyistodeterminewhatcolumnsusersfrequentlysearchsothatwecancreateasuitableCTXCATindex.QueriesonthistypeofindexareissuedwiththeCATSEARCHoperator.)例子:
Createtableauction(Item_idnumber,Titlevarchar2(100),Category_idnumber,Pricenumber,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');Commit;/--确定你的查询条件(很重要)Determinethatallqueriessearchthetitlecolumnforitemdescriptions--建立索引集beginctx_ddl.create_index_set('auction_iset');ctx_ddl.add_index('auction_iset','price');/*sub-indexa*/end;--建立索引Createindexauction_titlexonauction(title)indextypeisctxsys.ctxcatparameters('indexsetauction_iset');Columntitleformata40;Selecttitle,pricefromauctionwherecatsearch(title,'camera','orderbyprice')>0;Titleprice-------------------------Pentaxcamera200Canoncamera250Olympuscamera300Nikoncamera400Insertintoauctionvalues(5,'aigocamera',1,10,'27-oct-2002');Insertintoauctionvalues(6,'lencamera',1,23,'27-oct-2002');commit;/--测试索引是否自动同步Selecttitle,pricefromauctionwherecatsearch(title,'camera','price<=100')>0;Titleprice-------------------------aigocamera10lencamera23添加多个子查询到索引集:
begin
ctx_ddl.drop_index_set('auction_iset');ctx_ddl.create_index_set('auction_iset');ctx_ddl.add_index('auction_iset','price');/*sub-indexA*/ctx_ddl.add_index('auction_iset','price,bid_close');/*sub-indexB*/end;dropindexauction_titlex;Createindexauction_titlexonauction(title)indextypeisctxsys.ctxcatparameters('indexsetauction_iset');SELECT*FROMauctionWHERECATSEARCH(title,'camera','price=200orderbybid_close')>0;SELECT*FROMauctionWHERECATSEARCH(title,'camera','orderbyprice,bid_close')>0;任何的Dml操作后,Ctxcat的索引会自动进行同步,不需要手工去执行,与ctxcat索引相对应的查询操作符是catsearch.语法:
Catsearch([schema.]column,Text_queryvarchar2,Structured_queryvarchar2,Returnnumber;例子:
catsearch(text,'dog','foo>15')catsearch(text,'dog','bar=''SMITH''')catsearch(text,'dog','foobetween1and15')catsearch(text,'dog','foo=1andabc=123')3.CtxruleThefunctionofaclassificationapplicationistoperformsomeactionbasedondocumentcontent.Theseactionscanincludeassigningacategoryidtoadocumentorsendingthedocumenttoauser.Theresultisclassificationofadocument.
例子:
Createtablequeries(query_idnumber,query_stringvarchar2(80));insertintoqueriesvalues(1,'oracle');insertintoqueriesvalues(2,'larryorellison');insertintoqueriesvalues(3,'oracleandtext');insertintoqueriesvalues(4,'marketshare');commit;--建立索引Createindexqueryxonqueries(query_string)indextypeisctxsys.ctxrule;Columnquery_stringformata35;Selectquery_id,query_stringfromquerieswherematches(query_string,'oracleannouncedthatitsmarketshareindatabasesincreasedoverthelastyear.')>0;query_idquery_string---------------------------------------------1oracle4marketshare4.CtxxpathCreatethisindexwhenyouneedtospeedupexistsNode()queriesonanXMLTypecolumn以上是oracletext索引的几种类型,通常用的比较多的是context和ctxcat索引。
语法参考:
接下来,我们来重新看一下索引的内部处理流程,先从Datasotre开始Datastore从哪儿得到数据Filter数据如何转化成文本Lexer正在索引什么语言Wordlist如何展开词根和模糊查询Storage索引怎么存储StopList哪些词和主题不被索引SectionGroup允许分区查询吗,如何定义文档区段(4).Datastore属性数据检索负责将数据从数据存储(例如web页面、数据库大型对象或本地文件系统)中取出,然后作为数据流传送到下一个阶段。
Datastore包含的类型有Directdatastore,Multi_column_datastore,Detail_datastore,File_datastore,Url_datastore,User_datastore,Nested_datastore。
DIRECT_DATASTORE检索表(数据存储在单列中)MULTI_COLUMN_DATASTORE检索表(数据存储在多列中)DETAIL_DATASTOREDataisstoredinternallyinthetextcolumn.Document
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- oracle 全文 检索
![提示](https://static.bdocx.com/images/bang_tan.gif)