Take the 2-minute tour ×

I am building a search engine. And when a user types their search into my engine, my script only really checks for words in the statement, and if they match the keywords...the result is returned. But what if iPad is searched and Apple, because is is a higher rank, is returned before the official iPad website in Apple.com. I have begun using full text search but it still is a bit edgy. Any ideas? 

$query = " SELECT * FROM scan WHERE ";

$terms = array_map('mysql_real_escape_string', $terms);
$i = 0; 
foreach ($terms as $each) {
    if ($i++ !== 0){
        $query .= " AND "; 
    $query .= "Match(title, description, keywords, link) Against ('%{$each}%') ";
share|improve this question
add comment

1 Answer

when doing full text searching, you don't need to add each keyword to its own "match against" statement in the where clause. You can just say Match(title, description, keywords, link) Against ('ipod apple'). It will do the matching of each word in the "against" part and rank them accordingly. You can also do boolean matches where you can weight specific words using key symbols like "+" (+SomeWord) for making that word required in the result, "-" (-SomeWord) for making that word not allowed in the result...etc.

share|improve this answer
Have you got any code I could use to see what you mean. Or take my code and edit for it to work –  Reseed Grey Sep 28 '12 at 17:49
Use Against ('".implode(' ',$terms)."') –  Loz Cherone Sep 28 '12 at 17:57
Doesn't work, would thre be a function which calculates the amount of times the word appears in designated fields and then returns them in a higher order –  Reseed Grey Sep 28 '12 at 18:08
@ReseedGrey what lawrence wrote is correct. How is it not working? is the query throwing an error? Check out the boolean page I linked to in the answer, it gives some pretty good examples. The ranking is how fulltext works. You add a fulltext index on a column, the db indexes each word and how often it appears, the query will rank rows based on how often search words appear. You can put the "match against" in the select part of the query to get back that ranking. –  Jonathan Kuhn Sep 28 '12 at 18:16
It just doesn't seem to make any difference. Can you post an edit of my code below to show me what you mean? –  Reseed Grey Sep 28 '12 at 18:29
add comment

Your Answer


