Full Text Indexing SQL

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.

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 installedIF EXISTS (  SELECT *    FROM sys.fulltext_catalogs   WHERE name = N'my_catalog_name')    DROP FULLTEXT CATALOG my_catalog_nameGO CREATE FULLTEXT CATALOG my_catalog_nameGOIF 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_nameGOCREATE 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 AUTOGOALTER 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