开发者

mysql boolean search weighting columns

I have three tables in mysql which have full text search enabled:

title (title, url), headings (headings, url), and content (content, url).

My question is this: I wanted weight the url column more than the title, title column more than the headings, and the headings more than the content. Could I do this with b开发者_开发技巧oolean searches where each term is required, like so:

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


MATCH ...AGAINST returns a number, and MySQL remembers the result of the where clause, so if you have 3 separate FT-indexes on 1 table, you could just do a:

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

Or whatever the weights may need to be.

Alternatively, in Boolean mode, same table, you could use:

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)

For searches where the searchterm is required, same trick:

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

If you have 3 separate tables, you'll have to store the weight somewhere, either in some JOIN, or by separate queries (however you do it now):

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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜