我的网站上需要Mysql全文搜索实现。我的意图是,如果用户输入搜索词,大多数相关结果应该首先出现,其他结果应该是下一步。
SELECT pr.*
, MATCH (pr.title) AGAINST ('Samsung Galaxy S6' IN BOOLEAN MODE) AS relevance_1
, MATCH (pr.description, pr.tags) AGAINST ('Samsung Galaxy S6' IN BOOLEAN MODE) AS relevance_2
, MATCH (category.name, category.description) AGAINST ('Samsung Galaxy S6' IN BOOLEAN MODE) AS relevance_3
FROM products AS pr
LEFT JOIN category ON category.id = pr.category_id
WHERE MATCH (pr.title, pr.description, pr.tags, category.name, category.description) AGAINST ('Samsung Galaxy S6' IN BOOLEAN MODE)
ORDER BY (relevance_1 * 3) + (relevance_2 * 2) + relevance_3 DESC
这是我的sqlfiddle link。
答案 0 :(得分:0)
在全文搜索中,您需要在您的小提琴中使用FULLTEXT
索引我可以看到表products
有2个索引,而对于您有3个复合索引的产品,其他一个category
列title, description, tags
和类别,你有复合索引涉及2列,在你的select语句中你引用一个列标题(别名为relev_1),这将导致错误
无法找到与列列表匹配的FULLTEXT索引
因为它无法仅为标题列找到索引,因此您必须指定MATCH()
语句中定义了全文索引的所有列,与WHERE
子句相同的问题其中包括来自表格产品的3列和来自类别的2列,您已在单个MATCH
语句中定义它们
MATCH(pr.title,pr.description,pr.tags,category.name, category.description)
这又是错误的,您必须根据全文索引定义指定2个匹配语句,这些匹配语句的条件取决于您希望它们如何使用AND/OR
所以最终的工作查询将类似于
SELECT pr.*
, MATCH (pr.title,pr.description, pr.tags) AGAINST ('Samsung Galaxy S6' IN BOOLEAN MODE) AS relevance_2
, MATCH (category.name, category.description) AGAINST ('Samsung Galaxy S6' IN BOOLEAN MODE) AS relevance_3
FROM products AS pr
LEFT JOIN category ON category.id = pr.category_id
WHERE MATCH (pr.title, pr.description, pr.tags) AGAINST ('Samsung Galaxy S6' IN BOOLEAN MODE)
OR
MATCH (category.name, category.description) AGAINST ('Samsung Galaxy S6' IN BOOLEAN MODE)
ORDER BY (relevance_2 * 2) + relevance_3 DESC