简体   繁体   中英

Sort by Relevance in a basic mysql search engine

i have a table in mysql. it contains links, titles, descriptions and keywords for about one hundred websites. Not every site has a keywords field. i crawled Apple.com on a web crawler i wrote and i indexed the majoirty of apple's sites.

i linked the table up to my search engine and i had a problem. the code i was using doesn't sort by relevance. by that, i mean if a user types in 'Apple' into the search engine, it will return fairly random results. if i type in 'buy ipad' the ipad would come up instead of the apple store. i want the query to scan all the fields to see how many times each user input is in the field and then rank by most relevant. *f there is a clash, the higher id wins...basically.

btw, if i typed in iphone or ipad, the correct results would show. i have two different queries for seperate and together terms and would love to also know how to mix them together to have 1 query.

   Terms together:
   $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 ('".implode(' ', $terms )." in boolean mode);


     }


       Terms Seperate
       $query = " SELECT * FROM scan WHERE ";
   $terms = array_map('mysql_real_escape_string', $terms);
   $i = 0;
   foreach ($terms as $each) {
   if ($i++ !== 0) {
   $query .= "or";
   }
   $query.= "Match(title, description, keywords, link) Against ('".implode(' ', $terms )." in boolean mode);


    }

You need to use an alias for your MATCH statement.

Select *, MATCH(tags) AGAINST ('search query' IN BOOLEAN MODE) as relevance
from scan HAVING relevance >0 ORDERY BY relevance DESC

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