Full Text Index helps to perform complex queries against character data. These queries can include words or phrase searching.
We can create a full-text index on a table or indexed view in a database. Only one full-text index is allowed per table or indexed view.
The index can contain up to 1024 columns.
To create an Index, follow the steps:
- Create a Full-Text Catalog
- Create a Full-Text Index
- Populate the Index
Background
When we want to use the full text index service we need to start the service first else it will raise the errors. So let's start with how to start the service and use the Full text index step by step.
Step 1
(
Id Int Primary Key Identity(1,1),
Title Varchar(500),
[Desc]Varchar(max)
)
Insert some rows in the created table.
Step 2
Now we will create full text index on our database and table for providing search in table. So create the Full Text Catalog on our database by using following query.CREATE FULLTEXT CATALOG FTSearch
In the query above we have created FullText Catalog with the name FTSearch.
Now we will create full text index on our database and table for providing search in table. So create the Full Text Catalog on our database by using following query.CREATE FULLTEXT CATALOG FTSearch
In the query above we have created FullText Catalog with the name FTSearch.
Step 3
Now we will create full text index on our table Tbl_Search but for that we require the unique key id or primary key id so find the id of unique or primary key by using following command.SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
This command will display the all constraint names on tables present in our database from the output. Copy the Tbl_Search constraint name for creating a full text index on TblSearch.CREATE FULLTEXT INDEX ON Tbl_Search
Now we will create full text index on our table Tbl_Search but for that we require the unique key id or primary key id so find the id of unique or primary key by using following command.SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
This command will display the all constraint names on tables present in our database from the output. Copy the Tbl_Search constraint name for creating a full text index on TblSearch.CREATE FULLTEXT INDEX ON Tbl_Search
(Title, [Desc] LANGUAGE 1033)
KEY INDEX PK__Tbl_Sear__3214EC0700551192
ON FTSearch
In the above statement you can see we are creating a FullText Index on table name with a parameter; this parameter is nothing but the column name of the table on which we want to create the full text index and language 1033 denotes the language English.
In the above statement you can see we are creating a FullText Index on table name with a parameter; this parameter is nothing but the column name of the table on which we want to create the full text index and language 1033 denotes the language English.
Step 4
Now it's time to search the records in a specified indexed table. For that we can write the queries like below.Select * from Tbl_Search Where Contains(Title,'Asp.Net')
Now it's time to search the records in a specified indexed table. For that we can write the queries like below.Select * from Tbl_Search Where Contains(Title,'Asp.Net')
Select * from Tbl_Search Where Freetext([Desc],'Asp.Net')
In the preceding queries you can see we have given a where clause with column name which is the column we want to search and what we want to search. The preceding queries retrive the rows of a table which contain ASP.Net in title column and the second query will retrieve the rows of ASP.Net in the desc column.
In some cases your queries gives an error like fdhost cannot be started. That means your FullTextIndex Service is not started; for that you have to first start the Demon Launcher for FullTextIndex service.
Step 5
For starting FullTextIndex service go to SQL Server Tool->SQL Server Configuration Manage->Service->FullTextSearch Demon Launcher if it is stopped then start this service for performing search operation with a contains and FreeText clause and restart the SQL Server instance.
Conclusion
In this way we can use the FullTextIndex on our database.
In the preceding queries you can see we have given a where clause with column name which is the column we want to search and what we want to search. The preceding queries retrive the rows of a table which contain ASP.Net in title column and the second query will retrieve the rows of ASP.Net in the desc column.
In some cases your queries gives an error like fdhost cannot be started. That means your FullTextIndex Service is not started; for that you have to first start the Demon Launcher for FullTextIndex service.
Step 5
For starting FullTextIndex service go to SQL Server Tool->SQL Server Configuration Manage->Service->FullTextSearch Demon Launcher if it is stopped then start this service for performing search operation with a contains and FreeText clause and restart the SQL Server instance.
Conclusion
In this way we can use the FullTextIndex on our database.
example:
- e-business—searching for a product on a website:
SELECT product_id FROM products WHERE CONTAINS(product_description, ”Snap Happy 100EZ” OR FORMSOF(THESAURUS,’Snap Happy’) OR ‘100EZ’) AND product_cost < 200 ;
- Recruitment scenario—searching for job candidates that have experience working with SQL Server:SELECT candidate_name,SSN
FROM candidates WHERE CONTAINS(candidate_resume,”SQL Server”) AND candidate_division =DBA;
A. Creating a unique index, a full-text catalog, and a full-text index
USE AdventureWorks2012; GO CREATE UNIQUE INDEX ui_ukJobCand ON HumanResources.JobCandidate(JobCandidateID); CREATE FULLTEXT CATALOG ft AS DEFAULT; CREATE FULLTEXT INDEX ON HumanResources.JobCandidate(Resume) KEY INDEX ui_ukJobCand WITH STOPLIST = SYSTEM; GOCreating a full-text index on several table columns
USE AdventureWorks2012; GO CREATE FULLTEXT CATALOG production_catalog; GO CREATE FULLTEXT INDEX ON Production.ProductReview ( ReviewerName Language 1033, EmailAddress Language 1033, Comments Language 1033 ) KEY INDEX PK_ProductReview_ProductReviewID ON production_catalog; GOCreating a full-text index with a search property list without populating itUSE AdventureWorks2012; GO CREATE FULLTEXT INDEX ON Production.Document ( Title Language 1033, DocumentSummary Language 1033, Document TYPE COLUMN FileExtension Language 1033 ) KEY INDEX PK_Document_DocumentID WITH STOPLIST = SYSTEM, SEARCH PROPERTY LIST = DocumentPropertyList, CHANGE_TRACKING OFF, NO POPULATION; GOLater, at an off-peak time, the index is populated:ALTER FULLTEXT INDEX ON Production.Document SET CHANGE_TRACKING AUTO; GO