Solved

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

Posted on 2011-09-14
3
954 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 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now