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

时间:2011-02-10 10:42:51

标签: php mysql search relevance

  

可能重复:
  PHP MySQL Search And Order By Relevancy

您好,

我有一个包含多个列的表,其中包含名称,地址,公司等字段。让我们说有人搜索“microsoft john”。我想首先出现包含“microsoft”的结果,然后是包含john的结果。反之亦然,如果查询是“john microsoft”

我的PHP代码是:

$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;

问题是MYSQL按id排序结果...这很有趣,因为一些较高价值的结果可能会陷入堆栈深处。顺便说一句,phpmyadmin搜索有同样的缺陷。

请建议。

2 个答案:

答案 0 :(得分:2)

举个例子:

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

在您的代码中,这将使查询变得非常复杂。优点是,具有两个搜索词的项目都出现在仅匹配单个搜索词的项目之前。

另一种方法是在使用PHP检索记录时将记录排序到存储桶中。假设您在数组$search中有搜索项(按优先级递减排序):

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);
  }
}

现在您将结果存储在数组$results中。请注意,这演示了算法,它没有针对性能进行调整。

答案 1 :(得分:0)

我认为解决问题的最简单方法是按levenstein distance对结果进行排序。

像......那样的东西。

$queryappend="ORDER BY
   length(firstname) - levenshtein(FirstName, '".$searchitems[$i]."') +
   length(lastname)  - levenstein(LastName, '".$searchitems[$i]."')   +
   length(City)      - levenstein(City, '".$searchitems[$i]."')       +
   ...

尽管使用模式MORE SUITED来进行此类搜索可能是个好主意