我正在开发一种视频存档,wfsu.org / Dimensions,我正在尝试使用mysql和php为相关视频提供一个好的查询/算法组合。该数据库具有标题,关键字,描述,类别和生产者的另一个标准化1:m表。我有一个简单的算法,但如果任何有智慧的人看到它,他们会发现它产生了一组非常糟糕的“相关”视频。任何想法或帮助将不胜感激!!
每个请求这里是我正在使用的简单算法:
//if the segment isnt a generic dimensions use this query that makes sure they're in the same category
if($segType != 2)
{
$query = "SELECT `title`, `description`, `air_date`, `keywords`, `post_id`, `img_filename`
FROM `archive_post`
WHERE `segment_type` = $segType
AND `post_id` != $id
AND NOW() > ADDTIME(`air_date`, '20:0:0')
ORDER BY `air_date` DESC LIMIT 5";
}
else //otherwise we want a query that checks to see if there are any similar keywords
{
$query = "SELECT `title`, `description`, `air_date`, `keywords`, `post_id`, `img_filename`
FROM `archive_post`
WHERE (";
$kwArray = preg_split("/[\s,-]+/", mysql_real_escape_string($keywords));
foreach($kwArray as $kw)
{
$query .= "`keywords` LIKE '%$kw%' OR";
}
$query = substr($query, 0, -3);
$query .= ")
AND `post_id` != $id
AND NOW() > ADDTIME(`air_date`, '20:0:0')
ORDER BY `air_date` DESC LIMIT 5";
}
$result = $dbConnection->runQuery($query);
if(mysql_num_rows($result) == 0) //if we can't find any 'related' videos what do?
{
}
else
{
while($row = mysql_fetch_array($result))
{
$moreTitle = $row['title'];
$moreID = $row['post_id'];
$moreDescription = cleanDescription($row['description']);
$moreDescription = substr($moreDescription, 0, 50).'...';
$moreDate = strtotime( $row['air_date'], time() );
$moreDate = date( "F j, Y" , $moreDate );
$relatedVideos .= "<li> <a href='viewvideo.php?num=$moreID'></a><h3>$moreTitle</h3>
<div class='featuredStory'><span class='featuredDate'>$moreDate</span> ⋅ $moreDescription</div></li>";
}
}
答案 0 :(得分:0)
我要回答我的问题,以便任何处于类似困境的人都会面包屑。我查看了mysql的全文功能。首先,我发现它仅用于myISAM,在阅读之后,我发现与当前的innodb相比,它不太理想。
我最终对我想要搜索的3个关键部分的索引进行了全文查询,并且它完全符合我的需求!