Click here to Skip to main content
15,881,559 members
Articles / Programming Languages / SQL

Full Text Search in SQL

Rate me:
Please Sign up or sign in to vote.
4.93/5 (12 votes)
5 Jul 2015CPOL4 min read 30.4K   16   1
How to do full text search in SQL

Introduction

Executing complex queries against character-based data on the SQL tables can be accomplished using Full Text Queries across SQL as well as Azure SQL databases. Many a times, this type of requirement arises and we search for options for the implementation. One such scenario, I would like to share one such requirement. Suppose, we want users to search records from a table with column FirstName. Now, if users would like to search multiple entries to the search criteria, then how would the query go for search? Interestingly, FTS will do that for us..done
Now let's see what steps we need to follow in order to accomplish and execute Full Text search queries.

Implementation

The very first thing we need to do before at least writing the query, is to create Catalogs. Now what is a catalog, this catalog will be a warehouse which will contain all the Indexes (FTS indexes). Select the database to which you would like to add the FTS catalog and move to the storage, expand it and you will find: Full Text Catalogs and Full Text Stoplist.
Now the new thing Full Text Stoplist is an interesting concept.

The stoplist is actually a list of words which restricts the SQL to allow them in the FTS indexes, now we will discuss FTS Index next. Now how the stoplist works is, the words mentioned in the stoplist are avoided and not added into the indexes from search criteria’s text.

Thus, the below images show how to add a catalog:

FTS1

The next step after selecting the New Full-Text Catalog, we get a dialog box, which asks for FTS catalog name. With Accent sensitivity, by default Sensitive. More about accent sensitivity here.

fts2

Now, one thing to note here is as far as I implemented, FTS user interface as shown above is not permissible. So to add catalog, we need to query the script. The query goes as below:

SQL
CREATE FULLTEXT CATALOG FTS_DEMO
WITH ACCENT_SENSITIVITY = OFF

Now, we have catalog ready for the Full Text Search. Now it’s time to add index to the tables required. First, let's discuss what Indexes are and how they behave. These are allowed on a table, i.e., one index per table and at most 1024 columns are supported per table. On the basis on these indexes, the FTS can be applied and queried using the columns in the FTS index. Let's see how to add an FTS Index.

Using the User Interface

FTS3

Just as we see the interface says directly the options to define an index on the table Customer. The following images will follow the steps through.

Index1

This creates a unique index on the Customer table, thus PK is prefixed. This states that we cannot create another index on the table.

Index2

The above dialog states that we need to check in order to select the columns which we wish to allow into the FTS index. Here, I select both the columns. Then:

Index3

This dialog states that the changes to the table columns are tracked automatically and the indexes are populated automatically.

Index4

The above dialog asks to select the catalog into which the indexes for the table are going to be added. Here, we select the catalog we created and then click next.

Index5 Index6

Then click Next and Finish. Thus FTS index is created for the table Customer.

Now to create the Index using query script, we need to just write and run one query as below:

SQL
CREATE FULLTEXT INDEX ON dbo.[Customer]
(CustName, CustEmail)
KEY INDEX PK_Customer_Index
ON FTS_DEMO
WITH STOPLIST = SYSTEM

Thus this would create the Index for us and do the same if we follow the above mentioned steps through images. Here, one important thing to note is the KEY INDEX should be the primary unique key for that table created. To get the name, you can type and execute the following:

SQL
sp_help [TABLENAME]

This will give you the name as PK_***** something like this. This is very much required as we may panic on getting an error saying:

A full-text search key must be a unique, non-nullable, single-column index which is not offline, is not defined on a non-deterministic or imprecise nonpersisted computed column, does not have a filter, and has maximum size of 900 bytes. Choose another index for the full-text key.

Now, we are all set to carry on with our query and expect the desired results. The query goes like:

SQL
SELECT * FROM dbo.[Customer_BMW]
WHERE CONTAINS(Cust_Email,'gmail')

IndexRes2

This is how the syntax goes as above which is simple, this uses the keyword provided by FTS, i.e., CONTAINS as it takes one keyword to find a match from the records.

Another is the FREETEXT. What this does is it separates the strings into separate words and then based on it makes a search using the meaning of the words. Using the CONTAINS, we can use multiple string entries using OR or AND like below:

SQL
SELECT * FROM dbo.[Customer_BMW]
WHERE CONTAINS(Cust_Email,'gmail OR yahoo')

The result goes as below:

IndexRes

SQL
SELECT * FROM dbo.[Customer_BMW]
WHERE CONTAINS(Cust_Email,'gmail AND suraj')

The result goes as below:

IndexRes3

Conclusion

Thus, this is how simple and easy Full Text Search implementation is. This will be very handy if such requirement comes up. I hope this helps the readers. Please post queries if you have any.

References

Of course the references are the integral part to be shared.

Happy reading and learning. :)

This article was originally posted at http://surajpassion.in/full-text-search-in-sql

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Suvendu Shekhar Giri6-Aug-15 0:15
professionalSuvendu Shekhar Giri6-Aug-15 0:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.