PHP的加权搜索算法

时间:2011-09-19 15:44:47

标签: php mysql algorithm search weighted

问题

我在表格中有5个项目 [1] ,每个项目有4个属性(红色,绿色,蓝色,黄色)。
每个属性的得分均为1到9 [2]

在我的网站上执行搜索时,用户可以通过为每个属性提供1到9之间的分数来指定每个属性与搜索结果的相关程度。

我应该使用什么算法来根据用户偏好计算和排序结果?

资源

[1] - CREATE TABLE items( id INT NOT NULL AUTO_INCREMENT , name VARCHAR(128) , red INT , green INT , blue INT , yellow INT , PRIMARY KEY (id) );

[2] - INSERT INTO items (NAME, red, green, blue, yellow) VALUES ('Random 1', 4, 1, 9, 4), ('Random 2', 1, 1, 2, 9), ('Random 3', 5, 7, 6, 3), ('Random 4', 2, 2, 8, 1);

4 个答案:

答案 0 :(得分:3)

将搜索标点符号计算为字段并按其对查询进行排序

SELECT *, (red * @rw) AS w1, (green * @gw) AS w2, (blue* @bw) AS w3, (yellow * @yw) AS w4, (w1 + w2 + w3 + w4) AS result 
  FROM items ORDER BY result DESC; 

@rw =>红色重量,@ bw =>蓝色重量等...

答案 1 :(得分:1)

只是总结了

<?php
require('connect.php') //your db connection data
$redWeight=$_REQUEST['howeverYouPassedTheRedWeighting'];
$blueWeight=$_REQUEST['howeverYouPassedTheBlueWeighting'];
$yellowWeight=$_REQUEST['howeverYouPassedTheYelloWeighting'];
$greenWeight=$_REQUEST['howeverYouPassedTheGreenWeighting'];
$query="SELECT name, ( red*$redWeight+green*$greenWeight+blue*$blueWeight+yellow*$yellowWeight ) AS value FROM items ORDER BY value DESC";
$res=mysql_query($query);
//etc.
?>

答案 2 :(得分:1)

很抱歉,但我没有直接回答。这是一个非常有趣的话题。您可以使用与欧氏距离或Pearson相关性相关的内容。您可以在与集体智慧相关的书籍中找到更多信息。

当然,实现这些内容会更加困难,但您的结果我们会更加准确和准确。我推荐这些书:

Algorithms of the Intelligent Web

Programming Collective Intelligence: Building Smart Web 2.0 Applications

答案 3 :(得分:0)

我不确定我的意思是否正确,但是如何:

$user_defined_relevance = array ('red' => 1, 'blue' => 3, 'green' => 2, 'yellow' => 7);
arsort($user_defined_relevance); //this sorts the array by value descending while maintaining index association

$query = 'SELECT * FROM items ORDER BY '.implode(array_keys($user_defined_relevance), ',  DESC');
//will make SELECT * FROM items ORDER BY yellow DESC, blue DESC, green DESC, red DESC;
mysql_query($query);
 ...