MySQL Match& amp;喜欢

时间:2014-02-04 09:42:09

标签: mysql match match-against

我用这种方式创建了全文索引:

CREATE FULLTEXT INDEX filter_index
ON posts (filter)

我的搜索代码目前是:

SELECT * FROM posts
WHERE MATCH(filter) AGAINST (?)

查询执行Ok,在我的表中我有以下结构。

ID | name   | from           | filter
 1   John     Paris - France | John Paris France
 2   Carey    Paris - France | Carey Paris France
 3   Maria    France - Vion  | Maria France Vion
 4   Alfredo  US New York    | Alfredo US New York
 5   JohnD    Colombia       | JohnD Colombia

如果我查询以下内容:

SELECT * FROM posts
    WHERE MATCH(filter) AGAINST ('France Vion')

这将首先得到结果: John Paris France & Carey Paris France仅在此之后Maria France Vion

这应该不同吗?首先是Maria France Vion,之后只有其他结果?

如何优化此操作?

2 个答案:

答案 0 :(得分:3)

试试这个

SELECT *, MATCH(filter) AGAINST('France Vion' IN BOOLEAN MODE) as relevance
FROM posts_locale 
WHERE MATCH(filter) AGAINST('France Vion' IN BOOLEAN MODE) 
ORDER BY relevance DESC

此外,如果您要搜索两个不同的字段并给予优先权。与搜索fromfilter一样,但优先考虑提交from

SELECT *, 
MATCH(filter) AGAINST('France Vion' IN BOOLEAN MODE) as relevance,
MATCH(`from`) AGAINST('France Vion' IN BOOLEAN MODE) as relevance_from
FROM posts 
WHERE MATCH(`from`, filter) AGAINST('France Vion' IN BOOLEAN MODE) 
ORDER BY (relevance_from * 2) + relevance DESC

这里的第二(2)个ORDER BY (relevance_from * 2) + relevance DESC将优先级提高一倍。在某些情况下,这可能很高。您可以尝试1.5或更低

之类的内容

答案 1 :(得分:0)

你也需要“询问”得分。

试试这个:

SELECT    *, MATCH(filter) AGAINST('{keyword}') as Relevance 
FROM      posts 
WHERE     MATCH(filter) AGAINST('{keyword}' 
ORDER BY  Relevance DESC