views:

729

answers:

5

What's the best way to convert search terms entered by a user, into a query that can be used in a where clause for full-text searching to query a table and get back relevant results? For example, the following query entered by the user:

+"e-mail" +attachment -"word document" -"e-learning"

Should translate into something like:

SELECT * FROM MyTable WHERE (CONTAINS(*, '"e-mail"')) AND (CONTAINS(*, '"attachment"')) AND (NOT CONTAINS(*, '"word document"')) AND (NOT CONTAINS(*, '"e-learning"'))

I'm using a query parser class at the moment, which parses the query entered by users into tokens using a regular expression, and then constructs the where clause from the tokens.

However, given that this is probably a common requirement by a lot of systems using full-text search, I'm curious as to how other developers have approached this problem, and whether there's a better way of doing things.

A: 

Watch out for SQL injection

Cade Roux
he's not using dynamic sql
Al W
Sure looks like it to me.
Cade Roux
A: 

I realize it's a bit of a side-step from your original question, but have you considered moving away from SQL fulltext indexes and using something like Lucene/Solr instead?

TML
Yep, I'd like to move to Lucene at some point, though I've touched it in the past and although setting up the basics is relatively simple, getting it to do the same kind of things I'm doing now is a little more work, so I've put this on hold.
Mun
+4  A: 

This may not be exactly what you are looking for but it may offer you some further ideas.

http://www.sqlservercentral.com/articles/Full-Text+Search+(2008)/64248/

Brawndo
Thanks, that looks quite useful.
Mun
I have tried it and it isn't really that great.
Ronnie Overby
A: 

The easiest way to do this is to use dynamic SQL (I know, insert security issues here) and break the phrase into a correctly formatted string.

You can use a function to break the phrase into a table variable that you can use to create the new string.

mrdenny
A: 

A combination of GoldParser and Calitha should sort you out here.

This article: http://www.15seconds.com/issue/070719.htm has a googleToSql class as well, which does some of the translation for you.

davewasthere