SQL2005全文检索

         全文检索是指计算机索引程序通过扫描文章中的每一个词,对每一个词建立一个索引,指明该词在文章中出现的次数和位置,当用户查询时,检索程序就根据事先建立的索引进行查找,并将查找的结果反馈给用户的检索方式。这个过程类似于通过字典中的检索字表查字的过程。

1、允许数据库使用全文索引,
在SQL Server Management Studio中选择要操作的数据库的右键菜单中的属性,在属性窗口中的Files页面,有Use full-text indexing,勾选这个复选框就可以了。
SQL2005全文检索_第1张图片

2、创建full-text catalog

create  fulltext catalog catalogname

运行完此命令,会在sql的安装目录下产生一个缓存文件夹:

SQL2005全文检索_第2张图片
3、创建唯一索引,对要进行全文检索的表主键创建唯一索引

create   unique   index  indexname  on  talbename(columnname)

4、创建全文索引
根据之前的full-text catalog和unique index在同一表上创建全文索引

create  fulltext  index   on  tablename(column1,colunmn2,)
key   index  indexname  on  catalogname
with  change_tracking auto

5、使用全文检索函数contains,
完成上面的一系列工作后,就可以在查询中使用全文索引函数contains

where   contains ( column ' "a" and "b" not "c" ' )
where   contains ( column ' "abc" ' )
where   contains ( column ' "a" and "b" and "c" ' )
where   contains ( column ' "a" near "b" ' )
where   contains ( column ' formsof(inflectional, "happy") ' )
matches "happy", "happier", "happiest", "happily".

contains ( column ' isabout("computer" weight(0.5), "software" weight(2.0),
"development" weight(10.0)) rankmethod inner product
' )

之前,简单介绍如何为SQL2005普通文本字段创建全文检索,这次是针对存储在数据库中的文件内容进行全文检索,创建cataglog和主键唯一索引和之前是一样的,不同的是在创建全文检索引时,有点不同,由于是以二进制方式存储的文件内容,因此需要文件的扩展名作为辅助,在创建索引时,需要指定这个存储文件扩展名的字段:

CREATE  FULLTEXT  INDEX   ON   [ tablename ]
(
    
[ textfield ]                           -- Full-text index column name 
        TYPE  COLUMN   [ FileExtension ]      -- Name of column that contains file type information
        Language  0X0                   -- 0X0 is LCID for neutral language
)
KEY   INDEX  [IndexName]  ON  [CatelogName]  -- Unique index
WITH  CHANGE_TRACKING AUTO             -- Population type


这种对文件内容进行全文检索,SQL 2005主要针对两种字段类型:VarBinary(Max), Image. 在更改了字段类型后,全文检索就不能正常工作了,检索不出想要的数据,比如:原来是Image,更改为VarBinary(MAX),此时需要重新创建全文索引就可以了,其他的不用做任何修改。另外,SQL 2005全文检索还支持以下字段类型:char, varchar, nvarchar, xml.其中,XML和VarBinary(MAX), Image类同。

另外,在删除已经存在的full-text catalog,重新创建同名的catalog, 会得到下面的错误提示:

File   ' sysft_CatalogName '  cannot be reused until after the  next   BACKUP   LOG  operation.


简单的解决办法是运行如下命令:

BACKUP   LOG   < Dbname >   to   disk = "c:\ temp . log "

这样可以通过全文检索搜索到文件内容中包含关键字的记录了,这种情况是在文件内容存储在数据库中时解决方案,如果,文件本身存储在磁盘上,如何进行全文检索呢?我知道一个解决方案是通过Lucene.Net来实现。

你可能感兴趣的:(sql2005)