﻿{"id":299,"date":"2014-10-16T09:30:50","date_gmt":"2014-10-16T08:30:50","guid":{"rendered":"http:\/\/www.codereview.co\/?p=299"},"modified":"2014-10-15T14:44:08","modified_gmt":"2014-10-15T13:44:08","slug":"sql-server-freetext-command-of-expanded-search","status":"publish","type":"post","link":"http:\/\/www.codereview.co\/index.php\/sql-server-tutorials\/sql-server-freetext-command-of-expanded-search\/","title":{"rendered":"SQL Server FREETEXT command of expanded search"},"content":{"rendered":"<p><span style=\"color: #000000;\"><strong>Scenario<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\">I have been reading about the Full Text Search capabilities in SQL Server 2005 and your tips have been a big help to improve how we query our data.\u00a0 Thus far converting our core queries to leverage Full Text Search has been a big help.\u00a0 I have been seeing information about the FREETEXT command and I would like to learn how to use this FREETEXT command versus the CONTAINS command.\u00a0 Should I use the FREETEXT command over the CONTAINS command or vice versa?\u00a0 Can you also provide some examples of using the FREETEXT command to learn about the variety of capabilities available?<\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>\u00a0<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>Solution<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\">Yes &#8211; Let&#8217;s take a look at the comparison first and then we can work through a few different examples.\u00a0 The FREETEXT command is another alternative to access the data indexed by Full Text Search.\u00a0 In general the FREETEXT command provides the ability to search for a matched term based on the meaning of the terms as opposed to the exact character string.\u00a0 At a high level, this commands finds matches based on separating the string into individual words, determining inflectional versions of the word and using a thesaurus to expand or replace the term to improve the search.<\/span><\/p>\n<p><span style=\"color: #000000;\">Now let&#8217;s compare the FREETEXT functionality with the CONTAINS command.\u00a0 The CONTAINS command uses exact match or fuzzy logic to perform the matches with a single word or a phrase.\u00a0 In addition, the words near another word can be found as well as performing a weighted match of multiple words where each word has a weight as compared to the others that are searched.\u00a0 Check out CONTAINS (Transact-SQL) for a explanation on the CONTAINS command.<\/span><\/p>\n<p><span style=\"color: #000000;\">Depending on the search you are performing dictates which Full Text Search command you should use.\u00a0 Keep in mind that FREETEXT and CONTAINS are only two of the four commands available.\u00a0 The other two commands are CONTAINSTABLE and FREETEXTTABLE.\u00a0 The comparison between the four commands will be saved for a future tip since it is fairly involved explanation that should include examples.\u00a0<\/span><\/p>\n<p><span style=\"color: #000000;\">Until that point in time, here is one data point to consider: according to SQL Server 2005 Books Online FREETEXT (Transact-SQL) &#8220;Full-text queries using FREETEXT are less precise than those full-text queries using CONTAINS. The SQL Server full-text search engine identifies important words and phrases. No special meaning is given to any of the reserved keywords or wildcard characters that typically have meaning when specified in the &lt;contains_search_condition&gt; parameter of the CONTAINS predicate.&#8221;\u00a0 Based on my testing, when the basic terms are queried with either command similar results are returned, so the precise factor for simple queries seems less of an issue.\u00a0 For complex searches the CONTAINS command wins hands down with the ability to use wild cards, NEAR statements, etc. As such, if the flexibility of the search is built into the front end application then the highest level of flexibility on the back end, between the FREETEXT and CONTAINS commands, tips the scales toward the CONTAINS command.<\/span><\/p>\n<p><span style=\"color: #000000;\"><strong><em>FREETEXT Examples <\/em><\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\">Let&#8217;s take a look at the command reference and a few FREETEXT examples to use as a point of reference and comparison with the other Full Text Search.<\/span><\/p>\n<table width=\"595\">\n<tbody>\n<tr>\n<td><span style=\"color: #000000;\"><strong>FREETEXT Command Syntax<\/strong><\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color: #000000;\">FREETEXT ( { column_name | (column_list) | * } , &#8216;freetext_string&#8217; [ , LANGUAGE language_term ] )<\/span><span style=\"color: #000000;\"><em>Source &#8211; SQL Server 2005 Books Online (FREETEXT (Transact-SQL))<\/em><\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color: #000000;\"><strong>Simple FREETEXT Command<\/strong><\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color: #000000;\">USE AdventureWorks; GO SELECT * FROM Production.Product WHERE FREETEXT(*, &#8216;nut screw washer&#8217;); GO \u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color: #000000;\"><strong>Literal Search with the FREETEXT Command<\/strong><\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color: #000000;\">USE AdventureWorks; GO SELECT * FROM Production.Product WHERE FREETEXT(*, &#8216;&#8221;flat washer&#8221;&#8216;); GO \u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color: #000000;\"><strong>FREETEXT Command with Input Parameters<\/strong><\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"color: #000000;\">USE AdventureWorks; GO DECLARE @SearchWord nvarchar(30); SET @SearchWord = N&#8217;nut screw washer&#8217;; SELECT * FROM Production.Product WHERE FREETEXT(*, @SearchWord); GO<\/span><span style=\"color: #000000;\"><em>*** NOTE *** &#8211; As a point of reference, use an nvarchar data type to prevent an implicit conversion.<\/em><\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span style=\"color: #000000;\">\u00a0<\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>Thanks for reading this article,<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\"><strong>Next steps :<\/strong><\/span><\/p>\n<ol>\n<li><span style=\"color: #000000;\"><b><strong>Add this article to your database toolkit<\/strong><\/b><\/span><\/li>\n<li><span style=\"color: #000000;\"><strong>Share this with your colleagues because Sharing is Learning<\/strong><\/span><\/li>\n<li><span style=\"color: #000000;\"><strong>Comment below if you need any assistance<\/strong><\/span><\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Scenario I have been reading about the Full Text Search capabilities in SQL Server 2005 and your tips have been a big help to improve how we query our data.\u00a0 Thus far converting our core queries to leverage Full Text Search has been a big help.\u00a0 I have been seeing information about the FREETEXT command [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[32,3],"tags":[156,157,155,21],"_links":{"self":[{"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/posts\/299"}],"collection":[{"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/comments?post=299"}],"version-history":[{"count":4,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/posts\/299\/revisions"}],"predecessor-version":[{"id":365,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/posts\/299\/revisions\/365"}],"wp:attachment":[{"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/media?parent=299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/categories?post=299"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.codereview.co\/index.php\/wp-json\/wp\/v2\/tags?post=299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}