Oracle Text Examples
Oracle Text Examples
by Jeff Hunter, Sr. Database Administrator
Contents
- Overview
- Types of Index
- Supported Column Types
- Supported Document Formats
- Setting Up Your Environment
- Attention Linux Users!
- Detecting / Viewing Errors During Index Creation
- Your first CONTEXT Index Example
- Demonstrate using BFILE
- Demonstrate using BLOB
- Managing DML Operations for a CONTEXT Index
- Text Index Creation Strategies
- Example Code Repository
Oracle Text is a tool that provides mechanisms for developers to build Text Query applications as well as Document Classification applications. Oracle Text provides indexing, word and theme searching , and viewing capabilities for text.Types of IndexFor an explanation of the differences between, Oracle Text (9i), interMedia Text (8i) and Oracle ConText Cartridge (8) see: How Oracle Text (Oracle 9i) Relates To interMedia Text (Oracle 8i)
This article provides a short introduction and example of building a Text Query application.
Oracle Text supports the creation of three types of indexes depending on your application and text source. You use the CREATE INDEX statement to create all Oracle Text index types.Supported Column TypesThe following table describes these indexes and the type of applications you can build with them. The third column shows which query operator to use with the index.
Index Type ApplicationType Query Operator CONTEXT Use this index to build a text retrieval application when your text consists of large coherent documents. You can index documents of different formats such as MSWord, HTML, XML, or plain text. With a context index, you can customize your index in a variety of ways.
CONTAINS CTXCAT Use this index type to improve mixed query performance. Suitable for querying small text fragments with structured criteria like dates, item names, and prices that are stored across columns. CATSEARCH CTXRULE Use a CTXRULE index to build a document classification application. The CTXRULE index is an index created on a table of queries, where each query has a classification. Single documents (plain text, HTML, or XML) can be classified using the MATCHES operator.
MATCHES
With Oracle Text, you can create a CONTEXT index with columns of type VARCHAR2, CLOB, BLOB, CHAR, BFILE and XMLType.Supported Document FormatsNOTE The column types NCLOB, DATE and NUMBER cannot be indexed using a CONTEXT index.
Because Oracle Text can index most document formats including HTML, PDF, Microsoft Word and plain text, you can load any supported type into the text column.Setting Up Your EnvironmentWhen you have mixed formats in your text column, you can optionally include a format column to help filtering during indexing. With the format column you can specify whether a document is binary (formatted) or text (non-formatted such as HTML).
Click here for a detailed overview of the supported document formats in Oracle Text. (Taken from the Oracle Text Reference Version 9i)
- Create a user to used for the following examples:
CREATE USER ctx_demo IDENTIFIED BY ctx_demo DEFAULT TABLESPACE ctx_demod TEMPORARY TABLESPACE temp; GRANT connect, resource, ctxapp, dba TO ctx_demo;
- Set the instance pramater 'text_enable = FALSE'. This parameter had to be set to 'TRUE' in ConText (Pre interMedia Text 8.1.5). Ensure that the parameter now is set to FALSE.
- Include $ORACLE_HOME/ctx/bin in your PATH variable.
- On unix, the environment variable LD_LIBRARY_PATH or the SHLIB_PATH (depending on platform) must be set in the environment of the user using Oracle Text.
SHLIB_PATH must be set instead of LD_LIBRARY_PATH when working with the HP platform.
In versions below 8.1.7 it must also be set in the environment of the user who is starting and stopping the listener. Make sure that you set environment settings before starting extproc (lsnrctl start) because extproc looks up those values at startup time and modifications to these variables are thus not visible to the process resulting in indexing errors.
Set the environment variable to: <ORACLE_HOME>/lib:<ORACLE_HOME>/ctx/lib where <ORACLE_HOME> is the explicit full path for ORACLE HOME. DO NOT use the $ORACLE_HOME environment variable.
The variable can also be set in the ENVS section of the listener.ora file:
SID_LIST_listener= (SID_LIST= (SID_DESC = (SID_NAME = PLSExtProc) (ORACLE_HOME = /u01/app/oracle/product/8.1.7) (ENVS=LD_LIBRARY_PATH = /u01/app/oracle/product/8.1.7/ctx/lib:/u01/app/oracle/product/8.1.7/lib) (PROGRAM = extproc) ) (SID_DESC= (SID_NAME=OEM1DB) (ORACLE_HOME=/u01/app/oracle/product/8.1.7) ) )
For users of RedHat Linux (Version 7.1 and 7.2), you may have troubles when attempting to use any of the filters. When I first tried to index a Microsoft Word document, I recieved the following errors:Detecting / Viewing Errors During Index CreationSQL> select err_index_name, err_text from ctx_user_index_errors; ERR_INDEX_NAME ERR_TEXT -------------------- ------------------------------------------------------- DOCUMENT_BLOB_TAB_T1 DRG-11207: user filter command exited with status 127 DOCUMENT_BLOB_TAB_T1 DRG-11207: user filter command exited with status 127 DOCUMENT_BLOB_TAB_T1 DRG-11207: user filter command exited with status 127I then tried to use the ctxhx binary to check if there were any errors I could view.
% ctxhx utl_smtp.doc utl_smtp.html ctxhx: error while loading shared libraries: /u01/app/oracle/product/9.0.1/ctx/lib/libsc_ut.so: undefined symbol: statAfter working with Oracle on this, they advised me that this symbol stat is referenced from third party inso library libsc_ut.so. The problem is happening becuase of different behavior of linker.
This is recorded as BUG#: 2037255. You can download the following patch to fix the problem.
bug_2037255.tar
There are times when an index creation operations fail. Whenever the system encounters an error indexing a row, it logs the error into an Oracle Text view.Your first CONTEXT Index ExampleEnsure you are connected to the database as the user who created the index and query the view CTX_USER_INDEX_ERRORS. You may also view errors on ALL indexes in the database by connecting as CTXSYS and quering the view CTX_INDEX_ERRORS.
SELECT err_timestamp, err_text FROM ctx_user_index_errors ORDER BY err_timestamp DESC;
Navigate to the Example Code Repository portion of this article to download the files in the "VARCHAR2 Example" section.Demonstrate using BFILE
- Create the Text Table
SQL> @Create_Text_Table_VARCHAR2.sql- Run the example test query
SQL> @Test_Index_VARCHAR2.sql
Navigate to the Example Code Repository portion of this article to download the files in the "BFILE Example" section.Demonstrate using BLOB
- Create the Text Table
SQL> @Create_Text_Table_BFILE.sql- Run the example test query
SQL> @Test_Index_BFILE.sql
Navigate to the Example Code Repository portion of this article to download the files in the "BLOB Example" section.Managing DML Operations for a CONTEXT Index
- Create the Text Table
SQL> @Create_Text_Table_BLOB.sql- Run the example test query
SQL> @Test_Index_BLOB.sql
DML operations to the base table refer to when documents are inserted, updated or deleted from the base table. This section describes how you can monitor, synchronize, and optimize the Oracle Text CONTEXT index when DML operations occur.Text Index Creation StrategiesNote: CTXCAT indexes are transactional and thus updated immediately when there is an update to the base table. Manual synchronization as described in this section is not necessary for a CTXCAT index.
Viewing Pending DML
When documents in the base table are inserted, updated, or deleted, their ROWIDs are held in a DML queue until you synchronize the index. You can view this queue with the CTX_USER_PENDING view.
For example, to view pending DML on all your indexes, issue the following statement:
SELECT pnd_index_name, pnd_rowid , TO_CHAR(pnd_timestamp, 'dd-mon-yyyyhh24:mi:ss') timestamp FROM ctx_user_pending;This statement gives output in the form:PND_INDEX_NAME PND_ROWID TIMESTAMP -------------- ------------------ -------------------- MYINDEX AAADXnAABAAAS3SAAC 06-oct-1999 15:56:50Synchronizing the IndexSynchronizing the index involves processing all pending updates, inserts, and deletes to the base table. You can do this in PL/SQL with the CTX_DDL.SYNC_INDEX procedure.
The following example synchronizes the index with 2 megabytes of memory:
BEGIN ctx_ddl.sync_index('myindex', '2M'); END;Setting Background DML
You can set CTX_DDL.SYNC_INDEX to run automatically at regular intervals using the DBMS_JOB.SUBMIT procedure. Oracle Text includes a SQL script you can use to do this. The location of this script is:
$ORACLE_HOME/ctx/sample/script/drjobdml.sqlTo use this script, you must be the index owner and you must have execute privileges on the CTX_DDL package. You must also set the job_queue_ processes parameter in your Oracle initialization file.For example, to set the index synchronization to run every 360 minutes on myindex, you can issue the following in SQL*Plus:
SQL> @drjobdml myindex 360Index OptimizationFrequent index synchronization can fragment your CONTEXT index. Index fragmentation can adversely affect query response time. You can optimize your CONTEXT index to reduce fragmentation and index size and so improve query performance.
To understand index optimization, you must understand the structure of the index and what happens when it is synchronized.
CONTEXT Index Structure
The CONTEXT index is an inverted index where each word contains the list of documents that contain that word. For example, after a single initial indexing operation, the word DOG might have an entry as follows:
DOG DOC1 DOC3 DOC5Index Fragmentation
When new documents are added to the base table, the index is synchronized by adding new rows. Thus if you add a new document (DOC 7) with the word dog to the base table and synchronize the index, you now have:
DOG DOC1 DOC3 DOC5 DOG DOC7Subsequent DML will also create new rows:DOG DOC1 DOC3 DOC5 DOG DOC7 DOG DOC9 DOG DOC11Adding new documents and synchronizing the index causes index fragmentation. In particular, background DML which synchronizes the index frequently generally produces more fragmentation than synchronizing in batch.Less frequent batch processing results in longer document lists, reducing the number of rows in the index and hence reducing fragmentation.
You can reduce index fragmentation by optimizing the index in either FULL or FAST mode with CTX_DDL.OPTIMIZE_INDEX.
Document Invalidation and Garbage Collection
When documents are removed from the base table, Oracle Text marks the document as removed but does not immediately alter the index.
Because the old information takes up space and can cause extra overhead at query time, you must remove the old information from the index by optimizing it in FULL mode. This is called garbage collection.
Optimizing in FULL mode for garbage collection is necessary when you have frequent updates or deletes to the base table.
Single Token Optimization
In addition to optimizing the entire index, you can optimize single tokens. You can use token mode to optimize index tokens that are frequently searched, without spending time on optimizing tokens that are rarely referenced.
For example, you can specify that only the token DOG be optimized in the index, if you know that this token is updated and queried frequently.
An optimized token can improve query response time for the token.
Oracle published a small note on MetaLinks (Doc ID: 73605.1) that provides tips on how to manage and control the Oracle Text index creation process, storage preference and space usage.Example Code Repository
VARCHAR2 Example Create_Text_Table_VARCHAR2.sql
SQL Script to create Text table for VARCHAR2 column typeTest_Index_VARCHAR2.sql
SQL Script used to query VARCHAR2 Text tableTest_Index_VARCHAR2.out
Example output of Test_Index_VARCHAR2.sqlBFILE Example Create_Text_Table_BFILE.sql
SQL Script to create Text table for BFILEsTest_Index_BFILE.sql
SQL Script used to query BFILEs Text tableTest_Index_BFILE.out
Example output of Test_Index_BFILE.sqlBLOB Example Create_Text_Table_BLOB.sql
SQL Script to create Text table for BLOBsTest_Index_BLOB.sql
SQL Script used to query BLOBs Text tableTest_Index_BLOB.out
Example output of Test_Index_BLOB.sql























































































































































































































































































































































































































































































































































































































































































reference:
http://www.idevelopment.info/data/Oracle/DBA_tips/Oracle_Text/TEXT_3.shtml
posted on 2008-01-18 10:25 mjgforever 阅读(1112) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述