full-text search with leading wildcards

Hi folks,

I'm looking to take the functionality of LIKE pattern matching, i.e.

SELECT fieldname FROM table WHERE field LIKE '%foo%'

and return identical hits using full-text search.  Unfortunately, full-text search in SQL Server does not allow the use of leading wildcards, so I'm looking for alternatives.

Has anyone used Lucene (http://lucene.apache.org/java/docs/index.html) or any other full-text search applications with SQL Server?
LVL 1
hennessymAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bchoorCommented:
did u check this out: http://sqlblogcasts.com/blogs/simons/archive/2005/11/07/Wildcard-search-in-SQL-Server-Full-Text.aspx

note that the wildcard (*) cannot be used as a prefix. so "foo*" will work "*foo" or "*foo*" won't work. Also, notice the double quotes - so fulltext search knows that it is a wildcard search
0
Anthony PerkinsCommented:
bchoor,

Perhaps you did not read the full question:
>>Unfortunately, full-text search in SQL Server does not allow the use of leading wildcards, so I'm looking for alternatives.<<
0
bchoorCommented:
sorry, I caught it a little after my post - I was doing some research on it, but haven't come up with anything so far.
0
hennessymAuthor Commented:
Also, I tried the approach described in the most popular answer here: http://stackoverflow.com/questions/3400/how-do-you-get-leading-wildcard-full-text-searches-to-work-in-sql-server

...but it didn't work for me.
0
Mark WillsTopic AdvisorCommented:
Leading wildcards are shocking things to have to deal with in SQL Server (well most databases really).

The hit on performance is potentially disasterous and that is the main reason why DB's try hard to avoid them.

If searching for single words in a phrase, then might be best to examine the search string first and decide if a "LIKE" query, or even a patternmatch is more worthwhile than a "contains" type query.

and no, I havent used other tools like Lucene, but have heard about it, along with a few others. Wiki (sigh) has a few to consider (scroll down the bottom) : http://en.wikipedia.org/wiki/Full_text_search

As far as wildcards are concerned, then sure you can use them, but NOT as a partial match on a whole word other than a suffix. Have a look at the following examples (below).

So, where does that leave us ? Use the "like" if needing a wildcard search on start of a string, else use FTS. Or, see if you can reengineer you database / solution to use one of the third party packages.

So, hat being the bearer of unwanted news, but it aint going to happen quite the way you would ideally like...



USE AdventureWorks;      
GO

-- adventureworks should already be set up. if not, you may need to allow FTS to happen.
-- first check to see if the sample FTS has been established...

SELECT fulltext_catalog_id, name FROM sys.fulltext_catalogs 
GO 

-- some other useful things...

-- sp_help_fulltext_tables;
-- sp_help_fulltext_columns;
-- EXECUTE sp_fulltext_catalog  'ftCatalog',  'start_full';


-- if no FTS then create the catalogue and the FTS index on one of the sample tables

CREATE FULLTEXT CATALOG ftCatalog AS DEFAULT;
GO

-- now the FTS index

CREATE FULLTEXT INDEX ON Production.Document
(
    Title                                      --Full-text index column name 
)
KEY INDEX PK_Document_DocumentID ON ftCatalog  --Unique index from Production.Document
WITH CHANGE_TRACKING AUTO                      --Population type
GO

-- right, now we can start playing / testing

SELECT * 
FROM production.document
WHERE contains(title,'bracket')
GO

SELECT *
FROM Production.document 
WHERE FREETEXT(title, 'bracket') -- nb freetext doesnt do wildcards, so we wont bother too much.
GO

-- lets use a wildcard at the end - note how we now need to encapsulate in quotes

SELECT * 
FROM production.document
WHERE contains(title,'"brack*"')
GO

-- lets use a wildcard at the start

SELECT * 
FROM production.document
WHERE contains(title,'"*bracket"')
GO

-- lets use a wildcard at the start and end of a partial word

SELECT * 
FROM production.document
WHERE contains(title,'"*brack*"')
GO

-- lets use a wildcard at the start of a partial word

SELECT * 
FROM production.document
WHERE contains(title,'"*racket"')
GO

-- now use the "like" condition

Select *
FROM production.document where title like '%racket%'
GO

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.