Solved

text searching a field  like google

Posted on 2008-06-15
14
323 Views
Last Modified: 2009-03-26
i want to be able to search an ntext field similar to the way search engines work.  maybe a stored procedure that would take as a parameter a search string and return a select statement of those that matched

example:  a search for 'red orange apple'  --> would match on a ntext field with the phrase "the recipe calls for a red apple, but you could substitute an orange"

ideally it would also handle OR and quotes as well  (just like google)

im sure this has been done before.  are there some examples already written?
0
Comment
Question by:spathiphylum
  • 6
  • 6
14 Comments
 
LVL 2

Expert Comment

by:howyue
ID: 21790813
for ur red apple orange case u can:

SELECT 'the recipe calls for a red apple, but you could substitute an orange' COL1 INTO #Temp

SELECT * FROM #Temp
WHERE CONVERT(VARCHAR(MAX), COL1) LIKE REPLACE('%Red Apple Orange%', ' ', '%')
0
 

Author Comment

by:spathiphylum
ID: 21790860
the words aren't necessarily in the same order  (i.e. i would want a search for 'red apple orange' to return the same results as 'red orange apple'  or 'orange apple red') the same way most search engines work i think.   would your solution solve that?
0
 
LVL 2

Expert Comment

by:howyue
ID: 21791383
refer to this thread
http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_21501493.html

attached are my example, hope that is helpful..
anyway this is the first time i'm such search function too ...
CREATE TABLE CT (

	ID INT IDENTITY(1,1) PRIMARY KEY,

	COL1 NTEXT

)
 

INSERT INTO CT

SELECT 'the recipe calls for a red apple, but you could substitute an orange'
 

EXEC sp_fulltext_database 'enable'

EXEC sp_fulltext_catalog 'test_ct_catalog', 'create'

EXEC sp_fulltext_table 'CT', 'create', 'test_ct_catalog', 'PK__CT__41B8C09B'

EXEC sp_fulltext_column CT, COL1, 'add'

EXEC sp_fulltext_catalog 'test_ct_catalog', 'start_full'
 

SELECT * FROM CT 

WHERE CONTAINS(Col1, ' "an" OR "the" OR "apple" ')

Open in new window

0
 
LVL 2

Expert Comment

by:howyue
ID: 21791985
i think this would be exactly what u wan:
SELECT *
FROM CT
WHERE FREETEXT(Col1, 'orange apple red')
0
 

Author Comment

by:spathiphylum
ID: 21792491
this is the error i get

Msg 7653, Level 16, State 1, Line 1
'PK__CT__41B8C09B' is not a valid index to enforce a full-text search key. A full-text search key must be a unique, non-nullable, single-column index which is not offline, is not defined on a non-deterministic or imprecise nonpersisted computed column, and has maximum size of 900 bytes. Choose another index for the full-text key.

also, this won't deal with quotes will it?
0
 
LVL 2

Expert Comment

by:howyue
ID: 21792865
oh PK__CT__41B8C09B is my primary key name for table CT.
when u create the testing table at ur computer, it would gives u a different name if u don specify the name.
do check what is the primary key name for ur table CT and replace it:
EXEC sp_fulltext_table 'CT', 'create', 'test_ct_catalog', 'YourPrimaryKeyName'
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:spathiphylum
ID: 21794032
great.  it selects the right row, but it also selects the same row when i choose this:

SELECT * FROM CT WHERE FREETEXT(Col1, 'orange red blue')

even though 'blue' does not exist in the text.  it seems like it's doing an OR but the behavior of most search engines is to use AND

also it doesnt seem like it understands quotes bc this also returns the same result  

SELECT * FROM CT WHERE FREETEXT(Col1, 'blah "red raddish"')

even though the phrase "red raddish" doesnt exist in the text

ideas?

thanks
0
 
LVL 2

Expert Comment

by:howyue
ID: 21794097
the logic seems very fuzzy ..
u would expect the query to search for string that contains all 3 words of 'orange', 'red' & 'blue' ?
0
 

Author Comment

by:spathiphylum
ID: 21796223
thats how most search engines work, right?  if you google "sql server 2005" you get fewer results than if you google just "server"  suggesting the more terms you add the more specific you want it.
0
 
LVL 2

Expert Comment

by:howyue
ID: 21800405
ur question is more to the "logically how should i do it" than "technically how do i do it".. anyway if u wan the apply the method that is using "AND" word and returns result with all ur input keywords mayb u can try the following loop to generete the SQL, in whcih if u wish to implement it u wil hv to do it with a dynamic SQL.
DECLARE @Str VARCHAR(200)

DECLARE @Where VARCHAR(MAX)

DECLARE @EndPos INT

SET @Str = 'the recipe calls for a red apple, but you could substitute an orange'

WHILE NOT LEN(@Str) = 0 BEGIN

	SET @EndPos = CHARINDEX(' ', @Str)

	IF @EndPos = 0 SET @EndPos = LEN(@Str)

	SELECT @Where = ISNULL(@Where + ' AND ', 'WHERE ') + 'COLUMNNAME LIKE ''%' + SUBSTRING(@Str, 0, @EndPos) + '%''' + CHAR(13)

	SELECT @Str = LTRIM(SUBSTRING(@Str, @EndPos + 1, LEN(@Str)))

END
 

PRINT @Where

Open in new window

0
 

Author Comment

by:spathiphylum
ID: 21843582
so is there no facility to handle quotes and general boolean logic like Google?  surely this has been done before
0
 

Accepted Solution

by:
spathiphylum earned 0 total points
ID: 21865197
also this code seems to cut off the last character of the last word.  simple fix, no?
0
 
LVL 1

Expert Comment

by:buri8128
ID: 23989321
There is a CLR for google syntax questions in a article from sqlservercentral http://www.sqlservercentral.com/articles/Full-Text+Search+(2008)/64248/
I have just read not used it yet. I have a project where I might use it though.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Calculate values in an array 18 33
Calculated columns 13 61
Near realtime alert if SQL Server services stop. 20 53
SQL Query resolving a string conversion issue 26 37
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

910 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

19 Experts available now in Live!

Get 1:1 Help Now