duanmianhong4893 2011-02-10 10:42
浏览 42
已采纳

如何通过单词优先级的相关性来命令mysql搜索结果[重复]

Possible Duplicate:
PHP MySQL Search And Order By Relevancy

Hi,

I have a table with several columns containing fields like name, address, company etc. Lets say someone search for "microsoft john". I want the results containing "microsoft" should appear first, then results containing john. vice versa if query is "john microsoft"

My php code is:

$searchitems=explode(" ", $trimmed);
//print_r($searchitems);
$so = $_GET['so']=='2'?"2":"1";

$clause = $so=='2'?"AND":"OR";
include("dbconnect.php");
// Build SQL Query

$query = "select FirstName,LastName,course,Department,batch,City,companyjob,companylocation,
    companyposition,coursename,institutename,coursename2,institutename2,coursename3,
    institutename3 from alumni WHERE ";
for($i=0;$i<count($searchitems);$i++)
{
    $queryappend .= "(FirstName LIKE '".$searchitems[$i]."%' OR LastName LIKE '".$searchitems[$i]."%'
    OR City LIKE '".$searchitems[$i]."%' OR CountryorRegion LIKE '".$searchitems[$i]."%'
    OR companyjob LIKE '".$searchitems[$i]."%' OR companylocation LIKE '".$searchitems[$i]."%'
    OR coursename LIKE '".$searchitems[$i]."%' OR institutename LIKE '".$searchitems[$i]."%'
    OR coursename2 LIKE '".$searchitems[$i]."%' OR institutename2 LIKE '".$searchitems[$i]."%')";
    if($i<count($searchitems)-1) $queryappend .= $clause;

}
$query .=$queryappend;

The problem is MYSQL is ordering the results by id... This makes it funny, because some higher valued results may be stuck deep in the stack. btw, phpmyadmin search has the same flaw.

Please suggest.

  • 写回答

2条回答 默认 最新

  • doumengxue7371 2011-02-10 11:10
    关注

    As an example:

    SELECT
      FirstName,
      LastName,
      IF (FirstName LIKE '%Microsoft%' || LastName LIKE '%Microsoft%', 1, 0) AS One,
      IF (FirstName LIKE '%John%' || LastName LIKE '%John%', 1, 0) AS Two
    FROM alumni
    ORDER BY One DESC, Two DESC
    

    In your code, this will make the query pretty complicated. The advantage is, that items with both search term appear before items that match only a single search term.

    An alternative is sorting the records into buckets while retrieving them using PHP. Assuming you have the search terms in an array $search (ordered by descending priority):

    while ($record = mysql_fetch_array($result))
    {
      $total = join(' ', $record);
      $found = false;
      foreach ($search as $term)
      {
        if (strpos($total, $term) !== false)
        {
          $buckets[$term][] = $record;
          $found = true;
          break;
        }
      }
      if (!$found)
      {
        $results[] = $record;
      }
    }
    foreach (array_reverse($search) as $term)
    {
      if (isset($buckets[$term]))
      {
        $result = array_merge($buckets[$term], $result);
      }
    }
    

    Now you have the results in array $results. Note that this demonstrates the algorithm, it it not tuned for performance.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算