MySQL/PHP全文搜索1个单词的相似性


MySQL/PHP fulltext search 1 word similarity

我有一个客户想要管理网站中任何页面的SEO标题/描述,所以我考虑制作一个模块,他可以在其中输入URL/TITLE/DESCRIPTION,网站会检查是否有该页面的条目,如果有,则显示它。

我试过这个:

$url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$q_seo = $conexio->query('SELECT modulseo.titol, modulseo.descripcio, MATCH(modulseo.url) AGAINST("'.$url.'") AS score FROM modulseo WHERE MATCH(modulseo.url) AGAINST("'.$url.'") ORDER BY score DESC LIMIT 0,1');
if(mysql_num_rows($q_seo)>0)
{
    $d_seo = mysql_fetch_array($q_seo);
    $titol = $d_seo['titol'];
    $descripcio = $d_seo['descripcio'];
}
else
{
    $titol = 'default title';
    $descripcio = 'default description';
}

这显然不起作用,因为我相信全文搜索只适用于匹配不同的单词,而不是一个单词和另一个单词之间的相似性,因为客户端可以在后端URL中输入以下内容:

  • http://www.domain.com/index.php
  • http://domain.com/index.php
  • http://www.domain.com/index.php?language=fr
  • http://domain.com/index.php?language=fr
  • 等等

然后网站的用户可以访问这些url中的任何一个,所以必须有一种方法将任何url和客户端在后端访问的任何url进行匹配。

有关于如何做到这一点的线索吗?

使用LIKE %varible%进行搜索这是一个较慢的查询,但它可以工作。

你的表引擎必须是MyISAM,或者如果你是MySQL-v 5.6+,你可以使用带有全文索引的InnoDB

mysql> select * from test;
+------+------+------------------------------+
| ids  | name | last_name                    |
+------+------+------------------------------+
|    0 | 0    | http://domain.com/index.php  |
|    1 | 0    | http://domain2.com/index.php |
|    2 | 0    | http://domain3.com/index.php |
+------+------+------------------------------+
3 rows in set (0.00 sec)
mysql> select * from test where last_name like '%http://domain.com/index.php%';
+------+------+-----------------------------+
| ids  | name | last_name                   |
+------+------+-----------------------------+
|    0 | 0    | http://domain.com/index.php |
+------+------+-----------------------------+
1 row in set (0.00 sec)