views:

68

answers:

4

Hi

I have a table full of domain names. I'd like to do a search that returns some kind of relevancy results on it. My problem, is that if I do a search for "cool" I want it to return "cooldomain.com", which a fulltext search, unless I'm doing it wrong, will not.

Are there any fulltext options I'm unaware of that will accomplish this? If not, how would I go about doing it?

A: 

Good old WHERE domain LIKE '%cool%' will match, but doesn't return "relevancy" -- I'm not sure how you'd define that.

Alex Martelli
the problme with that is that LIKE '%cool%' wouldn't use the available index, slowing down the search significantly if I ended up with a big enough table.
erik.wiffin
A: 

Try something like

SELECT * FROM domains WHERE domain_name LIKE 'cool%';
"cooldomain.com" was just an example. LIKE 'cool%' wont match "awesomecool.com".
erik.wiffin
You could also process each domain by storing substrings of length 1 to the length of the full domain in another table, linking each substring back to the main domain. This would require more processing when storing each domain, but would clearly make searching quite fast. for cooldomain.com (for example), you'd store 'c','o',...,'co','oo',...,'coo',...,'cool','oold',..., etc..., making matching an exercise of only exact matching, rather than pattern matching. Like a search index, but for chars of the string.
A: 

I'd use LIKE here, fulltext search is for matching against full words or expressions, query expansion, etc. And I think the * operator can only be used as a suffix when using MATCH, so you'll miss imcool.com...

I think you'll have to gather more information to make a relevancy sorting.

Edit: If you want to use an index, you can also store the words of the domain in another column, and use the power of fulltext search on this one...

streetpc
*sigh* was hoping for a better solution than that.I've got a seperate column, but it's user submitted, so I can't guarantee that they'll get it right.
erik.wiffin
A: 

You will need to set a FULLTEXT index to your table and then do something like this:

SELECT *, MATCH(domain_name) AGAINST ('{search term here}' IN BOOLEAN MODE) as Relevance FROM domains WHERE MATCH (domain_name) AGAINST('{search term here}' IN BOOLEAN MODE) ORDER BY Relevance DESC