zoukankan      html  css  js  c++  java
  • SQL Server之1:全文搜索(1)

    本章介绍在SQL Server 2008 R2 下的全文索引,它能够对数据中的字符类型列(如varchar、text等类型)进行索引,并通过索引实现全文搜索查询。首先对比简单介绍一下常规索引和全文索引的区别,如下图:
    OK,下面我们就利用SQL Server 提供的存储过程来建立一个全文索引,具体步骤为:

    (1)启动数据库的全文处理功能(sp_fulltext_datebase);
    (2)建立全文目录(sp_fulltext_catalog);
    (3)在全文目录中注册需要全文索引的表(sp_fulltext_table);
    (4)指出表中需要全文检索的列名(sp_fulltext_column)
    (5)为表创建全文索引(sp_fulltext_table);
    (6)填充全文索引(sp_fulltext_catalog)。

    接下来用实例一步步演示:
     
    SQL Server 全文搜索
    use DBFullText

    -- 建表
    createtable Student
    (
    id
    intprimarykeyidentity(1,1) notnull,
    name
    nvarchar(30) null,
    familyAddress
    nvarchar(100) null,
    schoolAddress
    nvarchar(100) null
    )

    --插入一些数据
    insertinto Student values('SAVEA','187 Suffolk Ln.','1900 Oak St.')
    insertinto Student values('VICTE','2, rue du Commerce','23 Tsawassen Blvd.')
    insertinto Student values('BLONP','24, place Kléber','25, rue Lauriston')
    insertinto Student values('PARIS','265, boulevard Charonne','2732 Baker Blvd.')
    insertinto Student values('OLDWO','2743 Bering St.','2817 Milton Dr.')
    insertinto Student values('WANDK','Adenauerallee 900','Åkergatan 24')
    insertinto Student values('BERGS','Berguvsvägen 8','Carrera 22 con Ave. Carlos Soublette #8-35')
    insertinto Student values('SANTG','Carrera 52 con Ave. Bolívar #65-98 Llano Largo','Erling Skakkes gate 78')
    insertinto Student values('OCEAN','Grenzacherweg 237','Jardim das rosas n. 32')
    insertinto Student values('LEHMS','Sierras de Granada 9993','Via Ludovico il Moro 22')
    insertinto Student values('SIMOB','South House 300 Queensbridge','P.O. Box 555')


    --检查 DBFullText 是否支持全文索引,如果不支持全文索引,则使用sp_fulltext_datebase打开该功能
    if (selectdatabaseproperty ('DBFullText','IsFulltextEnables'))=0
    exec sp_fulltext_database 'enable'


    --创建全文目录(‘全文目录名‘,’创建/删除‘)
    exec sp_fulltext_catalog 'FT_Student','create'


    --创建全文索引(‘表名‘,’创建/删除‘,’名称‘,’约束名‘),这里的约束名就是建表的时候自动生成的主键约束
    exec sp_fulltext_table 'Student','create','FT_Student','PK_Student'


    --设置全文索引列(‘表名‘,’列名‘,’添加/删除‘)
    exec sp_fulltext_column 'Student','familyAddress','add'
    exec sp_fulltext_column 'Student','schoolAddress','add'


    --激活表的全文检索能力,也就是在全文目录中注册该表
    exec sp_fulltext_table 'Student','activate'


    --填充全文索引目录
    exec sp_fulltext_catalog 'FT_Student','start_full'


    --测试一下
    select*from Student wherecontains (familyAddress,'South')
    select*from Student wherecontains (schoolAddress,'Dr.')
     
    OK,现在全文搜索的SQL Server代码部分已经做完。其实在SQL Server 2008 R2里面,完全不用上面那么多代码去操作存储过程创建全文索引,
    它自带的有 ‘Full Text Catalogs’,我们完全可以手动建立一个全文索引(实现过程当然是调用存储过程,只不过在这里省略了),首先找到目录
    Storage -> Full Text Catalogs,然后创建一个新的Full Text Catalog,如下图
     
     
     然后打开它,选择要进行全文索引的列,如下图
     
     
    保存之后,即可做如上述的全文搜索。
  • 相关阅读:
    declare handler 声明异常处理的语法
    mysql存储过程获取sqlstate message_text
    mongoTemplate操作内嵌文档
    mysql索引之七:组合索引中选择合适的索引列顺序
    mongoDB的操作总结
    explain之三:MYSQL EXPLAIN语句的extended 选项学习体会,分析诊断工具之二
    状态模式
    代码重构----使用java有限状态机来消除太多的if else判断
    断路器(CircuitBreaker)设计模式
    断路器之一:Hystrix 使用与分析
  • 原文地址:https://www.cnblogs.com/alvinyue/p/2085972.html
Copyright © 2011-2022 走看看