Solved

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

Posted on 2011-09-14
3
1,128 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql2016-WIn10: standard,for SQL servc-account.. 51 55
Can I skip a node in XML? 9 46
SQL query 7 50
SQL Server Serialization error 8 47
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …

738 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