views:

92

answers:

2

I have a table with two fields in an sql server database and my asp.net application calls a stored procedure with a '@SearchString' parameter and the stored procedure finds all records where the @Searchstring value is found in the concatenation of two fields in the table, call them 'Field1' and 'Field2'

So the logic looks like this(I have simplified the actual query):

CREATE PROCEDURE [dbo].[sp_FindMatches] @SearchString varchar(30)
AS
  SELECT * FROM Table1 WHERE Field1+Field2 LIKE @SearchString

I would like to improve this rather basic matching algorithm so that it is not so restrictive in the records it matches. For example if the user enters "DOG HOUSE" as a parameter, the rather basic logic in the existing SP will return records where it finds the exact string. I would like for it to also return records just with "DOG" and "HOUSE" even if the strings aren't exactly next to each other.

Even better if there was a way to rank the records in terms of 'best match' it would be even better, i.e. if "DOG HOUSE" is found it is an exact match, if "DOG" and "HOUSE" are found, second best match, if "dog but not 'house' or 'house' but not 'dog' third best etc.

Is there a generic algorithm that does much of what I want?

+2  A: 

You should look at Full Text Search. It's specificially designed to do exactly what you're asking for, and it does so very well.

The way you've implemented this using traditional TSQL will render any indexes on the affected columns entirely unusable.

And don't be scared off by Full-Text Search - it's suprisingly simple to set up.

Aaron Alton
A: 

I would create a view which concatenates the two columns, and use full text searching on that view through your stored procedure.

Nick