Here's what i want to do:
Ex: let's assume I have a blog. Then someone searches for "php". The results would appear that way:
I actually did this with a class in PHP but it uses a lot of UNIONS (a lot!) and grows with the size of the search subject. So I'm worried about performance and DOS issues. Does anybody has a clue on this?
Probably this approach of doing a weighted search / results is suitable for you:
SELECT *, IF( `name` LIKE "searchterm%", 20, IF(`name` LIKE "%searchterm%", 10, 0) ) + IF(`description` LIKE "%searchterm%", 5, 0) + IF(`url` LIKE "%searchterm%", 1, 0) AS `weight` FROM `myTable` WHERE ( `name` LIKE "%searchterm%" OR `description` LIKE "%searchterm%" OR `url` LIKE "%searchterm%" ) ORDER BY `weight` DESC LIMIT 20
It uses a select subquery to provide the weight for ordering the results. In this case three fields searched over, you can specify a weight per field. It's probably less expensive than unions and probably one of the faster ways in plain MySQL only.
If you've got more data and need results faster, you can consider using something like Sphinx or Lucene.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With