Chinaunix首页 | 论坛 | 博客
  • 博客访问: 592710
  • 博文数量: 207
  • 博客积分: 10128
  • 博客等级: 上将
  • 技术积分: 2440
  • 用 户 组: 普通用户
  • 注册时间: 2004-10-10 21:40
文章分类

全部博文(207)

文章存档

2009年(200)

2008年(7)

我的朋友

分类: Mysql/postgreSQL

2009-04-14 12:36:26

This is one of those posts that’s for me to remember (cause I always forget it) and perhaps could come in handy for you too. From time to time I need to setup Full Text Indexing (yes I know there’s lots of alternative) and I hate using the IDE if I don’t have to.

Setting Up Full Text Indexing

sp_fulltext_database 'enable' –-need to have SP3 installed

IF EXISTS (
 SELECT *
   FROM sys.fulltext_catalogs
  WHERE name = N'my_catalog_name'
)
    DROP FULLTEXT CATALOG my_catalog_name
GO

CREATE FULLTEXT CATALOG my_catalog_name
GO

IF EXISTS(
    SELECT *
      FROM sys.fulltext_indexes
      JOIN sys.tables
        ON sys.tables.object_id = sys.fulltext_indexes.object_id
     WHERE sys.tables.name = 'table_name'
)
  DROP FULLTEXT INDEX ON table_name
GO

CREATE FULLTEXT INDEX ON table_name (column1,column2,column3)
    KEY INDEX PK_table_name –-this is the name of the PK, not the column
    ON table_name
    WITH CHANGE_TRACKING AUTO
GO

ALTER FULLTEXT INDEX ON table_name
    START FULL POPULATION

This will start population of the index. There’s a whole bunch of ways to query it, but the easiest is using FREETEXT and FREETEXTTABLE.

Querying

The easiest query is something like this:

    SELECT table_name.column1, table_name.column2,
       KEY_TBL.RANK
    FROM table_name INNER JOIN
       FREETEXTTABLE(table_name, search_column, 'query value') AS KEY_TBL
       ON table_name.primary_key = KEY_TBL.[KEY]
    ORDER BY Rank DESC
阅读(910) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~