IBM®
跳转到主要内容
    中国 [选择]    使用条款
 
 
Select a scope: Search for:    
    首页    产品    服务与解决方案     支持与下载    个性化服务    
跳转到主要内容

developerWorks 中国  >  Information Management  >

使用 DB2 Text Search 进行全文本搜索

DB2 Text Search 基本特性入门

developerWorks
前一页第 2 页,共 11 页后一页

文档选项

讨论

样例代码


对本教程的评价

帮助我们改进这些内容


为文本搜索设置数据库

设置测试数据库的步骤

  1. 创建一个数据库。在这个步骤中,创建一个适合存储 XML 文档的数据库。从 DB2 命令窗口发出以下命令:


    清单 1. 创建数据库的命令
    							
    CREATE DATABASE mytstest USING CODESET UTF-8 TERRITORY US
    

  2. 指定默认的数据库。DB2 Text Search 提供了一个选项,它设置一个环境变量来指定用于文本索引管理命令的数据库。要设置该变量,指定:


    清单 2. 用于指定默认数据库的命令
    							
    EXPORT DB2DBDFT=mytstest
    			

    注意:对于 Windows,使用 SET DB2DBDFT=mytstest 命令。

    如果已经拥有必要的用户授权,那么可以直接发出 db2ts 命令行命令,而不必指定连接子句。

  3. 启动文本搜索实例服务。要启动文本搜索实例服务,使用命令:


    清单 3. 启动文本搜索的命令
    							
    db2ts "START FOR TEXT"
    			

    在 Linux 和 UNIX 操作系统上,这个命令将启动一个守护进程。这个守护进程控制调度 DB2 数据库服务器上的文本搜索索引更新。IBM OmniFind Text Search 服务器也将被启动,并作为一个独立的进程运行。守护进程和 IBM OmniFind Text Search 服务器是在 DB2 实例所有者的授权名称下启动的。

    在 Windows 操作系统上,这个命令启动 DB2TS 服务(DB2TS - <instance name>)。DB2TS 服务又启动 IBM OmniFind Text Search 服务器,然后控制调度文本搜索索引的更新。运行该命令的用户必须满足启动该服务的 Windows 需求。

  4. 启用数据库的文本搜索功能。DB2 Text Search 管理关于文本索引的信息,以支持索引更新或收集状态信息等管理任务。启用数据库文本搜索将创建包含该信息的系统表和视图。


    清单 4. 启用数据库文本搜索的命令
    							
    db2ts "ENABLE DATABASE FOR TEXT"
    			

    注意,如果没有设置 DB2DBDFT,那么该启用命令需要包括连接信息,如下面的例子所示:



    清单 5. 启用数据库文本搜索的命令,它包含特定的连接信息
    							
    db2ts "ENABLE DATABASE FOR TEXT CONNECT TO mytstest USER db2admin USING mypswd"
    			

  5. 连接到数据库 mytstest:


    清单 6. 连接到数据库 mytstest
    							
    connect to mytstest
    

  6. 创建并填充一个表:


    清单 7. 表 books
    							
    CREATE TABLE books ( isbn VARCHAR(18) NOT NULL PRIMARY KEY,
                         author VARCHAR(30), 
                         title VARCHAR(128), 
                         year INTEGER, 
                         bookinfo XML)
    

    注意,为了能够创建文本索引和运行文本搜索查询,必须为数据表指定一个主键。

  7. 将以下数据插入到 books 表中:


    清单 8. 将数据插入到 books 表中
    							
    INSERT INTO books VALUES ('123-014014014', 'Joe Climber',
                              'Climber''s Mountain Tops',1995,
    XMLPARSE(
    DOCUMENT '<bookinfo>  
     <author>Joe Climber</author> 
    <title>Climber''s Mountain Tops</title>
    <story>This vivid description of Joe Climber''s experiences 
    when tackling the mountains of his native Mountainland lets you 
    hold your breath when you follow Joe on his adventures in the regions where 
     the air is thin and the weather is treacherous. Includes beautiful color photos
      of Mountainland''s mountain ranges.</story> 
        <year>1995</year> 
        <price>16.00</price>
        <pages>176</pages>
    </bookinfo>'));
    
    INSERT INTO books VALUES ('678-014014078', 'Joe Smith', 'The Range', 1991, 
    XMLPARSE(
    DOCUMENT '<bookinfo> 
     <author>Joe Smith</author>
    <title>The Range</title>
    <story>All you need to know about kitchen ranges. A pictured description
     based on the most recent ergonomics studies for everybody who is involved in 
     food preparation in the home.</story> 
       <year>1991</year> 
       <price>6.00</price>
       <pages>76</pages>
    </bookinfo>'));
    
    INSERT INTO books VALUES ('111-223334444', 'Sam Climber',
    'Top of the Mountain: Mountain Lore', 1966,
     XMLPARSE(
     DOCUMENT '<bookinfo> 
     <author>Sam Climber</author>
     <title>Top of the Mountain: Mountain Lore</title>
     <story>Sam Climber has traveled through the world to gather
    stories about mountains.  This compendium includes the best stories
    and is beautifully illustrated.</story>
       <year>1966</year>
       <price>20.00</price>
       <pages>449</pages>
     </bookinfo>'));
     
     INSERT INTO books VALUES ('777-010101010', 'Samantha Smitt',
              'The Database Compendium', 2001, 
     XMLPARSE(DOCUMENT
      '<bookinfo> 
      <author>Samantha Smitt</author> 
      <title>The Database Compendium</title>
    <story>Follow Samantha into the world of database management.
    Covers a wide range of the most popular database architectures.</story> 
        <year>2001</year> 
        <price>19.00</price>
        <pages>222</pages>
      </bookinfo>'));
    
      INSERT INTO books VALUES ('123-918273645', 'Joanne Miller', 
            'The Travel Companion', 2005, 
      XMLPARSE(
      DOCUMENT '<bookinfo> 
      <author>Joanne Miller</author> 
      <title>The Travel Companion </title>
     <story>If you like to travel, this is the book for you. Joanne Miller has compiled 
     her funny and often surprising diary entries about places and people which were 
     previously  published as travel log in ''Here and There''</story> 
        <year>2005</year>
        <price>31.00</price>
        <pages>321</pages>
      </bookinfo>'));
      


前一页第 2 页,共 11 页后一页
    关于 IBM 隐私条约 联系 IBM 使用条款