搜索标签并按排名排序

时间:2011-02-08 14:27:36

标签: php mysql phpmyadmin

tags-table有三列web, tags, rank

此表已启用FULL TEXT SEARCH

我可以按标签搜索并通过此查询获取网站列表

SELECT * FROM tags-table WHERE MATCH (tags) AGAINST ('search,searchengine,search,web' IN BOOLEAN MODE)

但我希望按其排名来排序结果。我该怎么办?

我也尝试了这个,但它给了别的东西。

SELECT * FROM tags-table WHERE MATCH (tags) AGAINST ('search,searchengine,search,web' IN BOOLEAN MODE) ORDER BY ABS(rank) 

rank列填充各个站点的排名。例如,google已获得rank 1facebook获得rank2,而yahoo已获得rank 4,就像剩余网站一样。

tags-table




   web          |    tags                                       |     rank

-----------------------------------------------------------------------------------
    google.com   |search,google,searchengine,engine,web          |     1
    facebook.com |facebook,social,networking,friends,community   |     2
    youtube.com  |video,youtube,videos,entertainment,media       |     3
    yahoo.com    |yahoo,search,email,news,searchengine           |     4

1 个答案:

答案 0 :(得分:0)

没有魔术专栏rank。但是你可以像这样计算相关性(或你称之为排名):

SELECT 
    *,
    MATCH (tags) AGAINST ('search searchengine search web') AS relevance
FROM 
    tags-table 
WHERE 
    MATCH (tags) AGAINST ('+search +searchengine +search +web' IN BOOLEAN MODE) 
ORDER BY 
    relevance DESC

请注意,第一个MATCH AGAINST未运行IN BOOLEAN MODE。这是因为IN BOOLEAN MODE几乎总是返回1.