I've been working on a live search feature for my site. I take a bunch of table values (i.e. bands, genres, albums, etc.) and I put them in their respective tables, as well as a search table for indexing. My main question is how do I make it more accurate.
Here is my mysql query (NOTE: the results are the same with or without Desc):
$search = "
(SELECT * FROM `search`
WHERE `search_name` LIKE '%$search_term%'
ORDER BY '%$search_term%' DESC
LIMIT 0, 8)";
Here is an example of the database:
Search ID | Search Name | Type |
----------------------------------------------------------
8 | Big Deal (What's He Done Lately?) | Album |
12 | Henry's Funeral Shoe | Band |
Problem is, is that when I type say H into the search parameter, I expect Henry's Funeral Shoe to be at the top, but instead I get Big Deal (What's He Done Lately?) before it, because it contains H and it is searched before the more appropriate one is.
So my question: Is there a MySQL function that can sort through the table and find the most relevant results and weigh them against those less relevant?
A basic search is easy. Good search is hard.
When setting up a search, you need to understand the data and what users will be searching for. In your case you want items starting with the terms to be returned first (I assume).
MySQL is not the best platform for search in general, but what you can do is 2 search. The first is:
search_name LIKE '$search_term%'
Note the missing % in front of the search term. These results are the higher ranked ones.
Secondly you should use:
search_name LIKE '%$search_term%'
These are your lower ranked results. Any results that are also in the higher rank list should be removed from the lower ranked list.
Finally you should combine the result sets making sure not to accidental mix up the ranks.
While not perfect nor adjustable, that might help in a basic search field.