tags:

views:

59

answers:

4

Is there a short way to check if a list of strings is %LIKE% a given string?

For example:

book animal a

Would all satisfy this condition for "A Book about Animals"?

I know that I could write out Title Like '%book%' AND '%animal%' LIKE Title, etc. but that seems unwieldy.

I'm using this for an autocomplete, where I return results where every word is in the result.

A: 

You should look at http://lucene.apache.org/solr/, it let's you index your data, and then can give a REST API that you can call to populate an autocomplete dropdown.

mlathe
thanks for the tip.. right now though i'm kind of on a deadline so i'd like to get this working with mysql
babonk
A: 

As far as I know, you have two options:

1) Perform a query with lots of WHERE clauses like you have.

2) Maintain a copy of the column with all of the words sorted. Then you can sort the search tokens and perform the query:

SELECT * FROM Table WHERE Title_sorted LIKE "%a%animal%book%"
Mike Axiak
And yes, you can also use SOLR, but in the absence of that you can do option (2) listed.
Mike Axiak
Option 2 requires the 3 words in that order.
KennyTM
Sorry I didn't reply to this a while ago. There's a reason you maintain a separate, sorted column ;)
Mike Axiak
A: 

MySQL has a full-text search index, I recommend you look into that to get efficient results. Going with Lucene might be overkill for just an autocomplete.

Jan Fabry
Does it make sense to use FULLTEXT search on an autocomplete? I would think you need perfect %LIKE% precision on an autocomplete..
babonk
A: 

mate... make the searchable columns into "fulltext" colums and then do

SELECT * FROM books WHERE MATCH(fulltext_column) AGAINST('animal book a')

best way. :) see http://devzone.zend.com/article/1304

Thomas Clayson
Does it make sense to use FULLTEXT search on an autocomplete? I would think you need perfect %LIKE% precision on an autocomplete..
babonk
for what you want. You don't want perfect precision - you want to be able to match the words in any order - like is too restrictive for that.
Thomas Clayson
My concern is that FULLTEXT has limitation like stopwords, min length, etc. While LIKE is perfect in precision.. so i was thinking itd be better fit for autocomplete
babonk
You can edit the stop word list using the ft_stopword_file variable (or set it to '' to index all words as long or longer than ft_min_word_len) if that suits your needs better. You can also change the minimum indexed word length using the ft_min_word_len variable, which exists for the same reason. (taken from http://stackoverflow.com/questions/2320838/mysql-fulltext-stopwords-rationale)
Thomas Clayson
Problem with doing that is that I use FULLTEXT elsewhere on the site, where I do want those options set that way
babonk
google? I don't know, you're getting into unknown territory here haha. Anyway, the min word limit is 4 characters, are users really going to hit on specific options with less than 4 characters? if you use `MATCH(column) AGAINST('+search +terms +preceeded +with +plus +signs' IN BOOLEAN MODE)` then you will include all search terms. So just replace all " "s with " +" :)
Thomas Clayson
I would want the autocomplete to work with words shorter than 4.. I realize now that my unwieldy LIKE way is the only option. I'll check your answer as correct though just because it would work with most people
babonk