简体   繁体   中英

php mysql search fulltext

Table structure:

Companies: id, name, city, branch, keywords, country

The keywords field contains the name city and branch together, I now just use a LIKE with wildcards for example '%music london%' will return 'musicfactory london'

But it is not precise enough for example if the user wants to search Vini & Magraine in London (a restaurant) and the input was 'vini londen' it was not found, however if the user inserts 'magraine london' it will get found.

So the problem is prob the order the keywords are, now I'm planning to converting to a fulltext search, I've created the index on the keywords field, also is the keywords field a smart idea?

My primary question is, I created a fulltext search with this query:

SELECT id, name, branch FROM companies WHERE MATCH(keywords) AGAINST(? IN BOOLEAN MODE) AND country = ? LIMIT 7

However it ain't returning the correct results? What is the best and most of all working solution?

Thanks for reading.

This is a good break down of standard match against operators. It should clarify HOW to write your query:

http://dev.mysql.com/doc/refman/5.5/en/fulltext-boolean.html

If I read your question correctly you're asking if a "keywords" column in the companies table is a good idea - I say it depends on how you populate it but sure, it's not bad. I used a collective keyword column that contained all standard text from the collection of relevant fields before and it worked out well.

A couple other notes -

1) I think your LIKE example is flawed and by that rule I'm surprised you got any results. % is a wildcard that is directional. so '%music london%' should NOT return 'musicfactory london'. you would need to write that out as WHERE( keywords LIKE '%music%' AND keywords LIKE '%london%') for that to work.

2) full text searches can only be used on MyISAM tables, so if you use a framework that depends on innoDB you will need to create a relational workaround. (or any other reason why you would use innoDB)

3) match against has a default minimum character count of 4 by default ini settings. You will need to set that lower in your config, then rebuild your full text indices if you need a lower min character range. Consider words like "art" and you'll know why it is suggested to lower this value. Otherwise you may have to write a conditional to switch between match against and LIKE mode for shorter strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM