Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do i make full text search search for partial words and individual words

Posted on 2011-09-14
3
Medium Priority
?
1,354 Views
Last Modified: 2012-08-13
I’m currently using full text search on 3 fields in my database, e.g. name, sku, details
However were having difficulty getting the search to work as expected e.g.
1)  We’re searching for whole lines and not each word as say google for example e.g. Lets say we have a detail of “the cat sat on the mat” then searching for “cat"  or “cat sat” will work but searching the “cat mat” won’t, so my question is, is there a way to have fts work smarter and break down the contents.
2)  partial word search don’t work as per the example above if I search for “ma” nothing will get returned but if I search for “mat” it will, is there a way around this?

My current stored procedure can be found below, and can any replies ideally include how it should be amended please

Thanks in advance
Webbo

DECLARE @FORMSOF as varchar(1000)
DECLARE @Phrase as varchar(1000)
SET @FORMSOF = 'FORMSOF (INFLECTIONAL, "' + @SearchText + '")'
SET @PHRASE =  '"' + @SearchText  + '"';
WITH Products AS
(
	SELECT ID, [Name], Sku, Origin, Price, ROW_NUMBER() over (ORDER BY ID) AS 'RowNumber'

	FROM mytable P
	WHERE 
		P.Active = 1
		and
		(
			CONTAINS(Name, @FORMSOF) --Search for forms of input text in product name
			OR CONTAINS(Detail, @FORMSOF) -- Search for forms of input text in product description
			OR CONTAINS(Origin, @PHRASE) -- Search for input text in Origin field
			OR SKU Like '%' + @SearchText + '%' -- Search for input text in Sku field
		)
)

SELECT * FROM Products

Open in new window

0
Comment
Question by:Webbo_1980
  • 2
3 Comments
 
LVL 22

Expert Comment

by:pivar
ID: 36534990
Hi,

How do you format @SearchText?

You need to have an op between each word, like

' "cat" AND "mat" '

With partial search you 'll need

' "cat" AND "ma*" '

/peter

0
 

Author Comment

by:Webbo_1980
ID: 36535082
at the minute the @searchText come in as 'the cat sat on the mat'

Is there a way in sql to split this to say something like replace every space with "* AND " ?

so
'the cat sat on the mat'

would then be
'the* AND cat* AND sat* AND on* AND the* AND mat'

and if so is this the best way to do it do you think?
0
 
LVL 22

Accepted Solution

by:
pivar earned 2000 total points
ID: 36535149
I would recommend you to build the searchtext where you interact with the user, that is, in your gui.

But you should be able to to something like

@SearchText = '"' + REPLACE(@SearchText, ' ', '*" AND "') + '*"'

This expect the text to be formatted with only one space between the words, and no other spaces.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question