mysql布尔搜索加权列

时间:2011-07-12 22:35:26

标签: php mysql

我在mysql中有三个表启用了全文搜索:

标题(标题,网址),标题(标题,网址)和内容(内容,网址)。

我的问题是:我希望网站的标题,标题列比标题更重,标题列比内容更重要。我可以使用布尔搜索来执行此操作,其中每个术语都是必需的,如下所示:

SELECT * FROM title WHERE MATCH (title)
AGAINST('+search' IN BOOLEAN MODE)

1 个答案:

答案 0 :(得分:2)

MATCH ...AGAINST返回一个数字,MySQL会记住where子句的结果,所以如果你在1个表上有3个独立的FT索引,你可以做一个:

SELECT * FROM tablename WHERE 
  MATCH (title)    AGAINST('search')
  OR MATCH (headings) AGAINST('search')
  OR MATCH (content) AGAINST('search')
ORDER BY 
  (MATCH (title) AGAINST('search') * 3) + 
  (MATCH (headings) AGAINST('search') * 2) +
  MATCH (content) AGAINST('content') DESC

或者可能需要的重量。

或者,在布尔模式下,同一个表,您可以使用:

SELECT * FROM tablename WHERE 
  MATCH (title)    AGAINST('>>>search'   IN BOOLEAN MODE)
  OR MATCH (headings) AGAINST('>>search' IN BOOLEAN MODE )
  OR MATCH (content) AGAINST('>search'   IN BOOLEAN MODE)
ORDER BY 
  MATCH (title) AGAINST('>>>search'   IN BOOLEAN MODE) + 
  MATCH (headings) AGAINST('>>search' IN BOOLEAN MODE) +
  MATCH (content) AGAINST('>search'   IN BOOLEAN MODE)

对于需要searchterm的搜索,同样的技巧:

 MATCH (title) AGAINST('+>>>search'   IN BOOLEAN MODE)

如果您有3个单独的表,则必须将权重存储在某个地方,无论是在某些JOIN中,还是通过单独的查询(但是现在就这样做):

SELECT *, MATCH (title) AGAINST('+>>>search'   IN BOOLEAN MODE) as weight
FROM title
WHERE     MATCH (title) AGAINST('+>>>search'   IN BOOLEAN MODE)

SELECT *, MATCH (headings) AGAINST('+>>search'   IN BOOLEAN MODE) as weight
FROM headings
WHERE     MATCH (headings) AGAINST('+>>search'   IN BOOLEAN MODE)

SELECT *, MATCH (content) AGAINST('+search'   IN BOOLEAN MODE) as weight
FROM content
WHERE     MATCH (content) AGAINST('+search'   IN BOOLEAN MODE)