Feeds:
Posts
Comments

Archive for the ‘Full-Text Search’ Category

Full-Text Search is one of the best features shipped with SQL Server 2005. It has all the capabilities that we need for the complex searching scenarios. However, Full- Text Search keep getting the new features in each version of SQL Server. In SQL Server 2012, Full-Text search came up with a brilliant feature called Custom proximity search. Given below are the capabilities of the search.

  1. You can define the maximum number of non search terms or maximum distance between first and last search term
  2. You can also define that search must follow the specific order.

Before I embark on the example, I would like to create a sample to demonstrate this excellent feature.
Sample :

USE AdventureWorks2012
GO
--DROP TABLE [dbo].[tbl_Comments]
--GO
CREATE TABLE [dbo].[tbl_Comments]
(
	[ID] [int] NOT NULL CONSTRAINT [PK_tbl_comments] PRIMARY KEY CLUSTERED ,
	[Comments] [varchar](1000) NULL
 )
GO
INSERT INTO [dbo].[tbl_Comments]
VALUES (1,'This is a demonstration of custom proximity term in Full-Text Search at raresql.com')
GO
--OUTPUT

Please note that once you create the sample, you have to create a Full-Text Index for column “Comments” in the above mentioned table. This article will help you to create Full-Text Index.

Example 1 : (Core functionality)
In this example, we will search two words (proximity and raresql) in the comments column in the sample table. The distance between both words is 6. However, we will increase and decrease the distance between words and observe the output.

--First of all, lets put the the distance 6 and observe the output.
USE AdventureWorks2012
GO
SELECT * FROM [tbl_Comments]
WHERE CONTAINS([Comments], 'NEAR((proximity, raresql), 6)')
--OUTPUT

custom proximity.1.1

--lets reduce the the distance from 6 to 5 words and observe the output.
USE AdventureWorks2012
GO
SELECT * FROM [tbl_Comments]
WHERE CONTAINS([Comments], 'NEAR((proximity, raresql), 5)')
--OUTPUT

custom proximity.1.2

As you can see that once you made the distance less than the actual, it is unable to search. Make sure that you provide whether actual distance or more.

Example 2 : (Implement Order)
In this example, we will search again two words (proximity and raresql) in the comments column.
However, we change the order, but it will not affect the result set until unless you will not forcefully implement the order. By default, it does not implement the order in the search.

--First of all, lets put the the distance 6 and reverse the search words.
--However make the sort order to FALSE (By default, it is false, it is optional to write in the code).
USE AdventureWorks2012
GO
SELECT * FROM [tbl_Comments]
WHERE CONTAINS([Comments], 'NEAR((raresql,proximity), 6,FALSE)')
--OUTPUT

custom proximity.1.3

--Lets put the the distance between the word is 6 and reverse the search words.
--However make the order to TRUE.
USE AdventureWorks2012
GO
SELECT * FROM [tbl_Comments]
WHERE CONTAINS([Comments], 'NEAR((raresql,Search), 6,TRUE)')
--OUTPUT

custom proximity.1.4

As you can see that, first of all, I did not implement the ORDER and made it false. In this case it returned the result set because ORDER did not affect the query. However, once I implemented the search order, it did not return the record because the words are in the opposite order in the comments column of the table. Please make a note that some languages follow left to right direction and vice versa. Custom proximity implement the sort search in the same direction.

Example 3 : (Implement Operator)
In custom proximity search, you have limited numbers of operators like AND, OR & AND NOT operator. However, you can use the combination of any of them. Given below are the samples.

--First of all, let try the AND operator.
USE AdventureWorks2012
GO
SELECT * FROM [tbl_Comments]
WHERE CONTAINS([Comments], 'NEAR((raresql,proximity), 6) And Full-Text')
GO
--OUTPUT

custom proximity.1.5

--Lets try OR operator
USE AdventureWorks2012
GO
SELECT * FROM [tbl_Comments]
WHERE CONTAINS([Comments], 'NEAR((raresql,proximity), 5) OR Full-Text')
GO
--OUTPUT

custom proximity.1.7

--Lets implement AND NOT operator.
USE AdventureWorks2012
GO
SELECT * FROM [tbl_Comments]
WHERE CONTAINS([Comments], 'NEAR((raresql,proximity), 6) AND NOT sample')
GO
--OUTPUT

custom proximity.1.6

As you can see, I implemented three different operators and it works accordingly. However you can use any number of combinations according to your business requirements.

Let me know if you have implemented Custom proximity search in the real world.

Advertisements

Read Full Post »