php search engine key words ranking

ⅰ亾dé卋堺 提交于 2019-12-23 06:08:19

问题


I am new to php and mysql. In my project I have to develop search engine for multiple keywords on multiple columns and also require the keyword ranking and relevency.

For example if I have three words hi, hello, hey and want to search on fields like subject, message, reference, textbody. in this case the row who has more keywords ranking will come first and lowest ranker row goes last. will any one guide me how to implement such search engine. my database is about 4 million and is growing rapidly.


回答1:


Use Full text search :

<?PHP

    //SET THE SEARCH TERM
    $term = "Search Term";


    $sql = "SELECT *, MATCH(subject, message,reference,textbody) AGAINST('". $term ."') as score FROM pages WHERE MATCH (subject, message,reference,textbody) AGAINST('". $term ."') ORDER BY score DESC";
    $query = mysql_query($sql);

    //BUILD A LIST OF THE RESULTS
    while($result = mysql_fetch_assoc($query)) {
       // your stuff for showing result
    }

?>

Note: you should have FULLTEXT indexing on the columns subject, message,reference,textbody




回答2:


You can use the AND or OR operators with like keyword, depending on what you want the search to return.

SELECT * FROM table WHERE subject LIKE %$param1% AND another_col LIKE %$param2% .......;

OR

SELECT * FROM table WHERE subject LIKE %$param1% OR another_col LIKE %$param2% .......;


来源:https://stackoverflow.com/questions/9029306/php-search-engine-key-words-ranking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!