mysql全文检索权索引
mysql full text search right index
假设我有 table posts
与此列:
top_totle,title,sub_title,text
我需要对所有此列进行全文搜索并按相关性排序,其中 top_title 需要比标题等更重要
所以我有 2 个相同的问题,为此创建索引的最佳方法是什么以及如何格式化查询以最好地支持该索引?
索引选项:
我可以为这一列的所有内容创建组合全文索引,或者为每一列创建单独的索引
哪个是首选方式?
选项 1:
SELECT
title,
MATCH (top_title) AGAINST ('text' IN BOOLEAN MODE) as toptitle_score,
MATCH (title) AGAINST ('text' IN BOOLEAN MODE) as title_score,
MATCH (sub_text) AGAINST ('text' IN BOOLEAN MODE) as sub_text_score,
FROM
`posts`
WHERE
MATCH (top_title,title , sub_text ) AGAINST ('text' IN BOOLEAN MODE)
and `posts`.`deleted_at` IS NULL
AND `published_at` IS NOT NULL
Order by toptitle_score desc,
Order by title_score desc ,
Order by subtext_score desc
选项 2:
SELECT
title,
MATCH (top_title) AGAINST ('text' IN BOOLEAN MODE) as toptitle_score,
MATCH (title) AGAINST ('text' IN BOOLEAN MODE) as title_score,
MATCH (sub_text) AGAINST ('text' IN BOOLEAN MODE) as sub_text_score,
FROM
`posts`
WHERE
(MATCH (top_title) AGAINST ('text' IN BOOLEAN MODE)
or MATCH (title) AGAINST ('text' IN BOOLEAN MODE)
or MATCH (sub_text) AGAINST ('text' IN BOOLEAN MODE))
and `posts`.`deleted_at` IS NULL
AND `published_at` IS NOT NULL
Order by toptitle_score desc,
Order by title_score desc ,
Order by subtext_score desc
选项 3:
is there some smarter way?
选项 1 不错。它需要 4 个 FT 索引(每列一个,加上所有 3 列一个)。不要重复 ORDER BY
:
ORDER BY toptitle_score DESC ,
title_score DESC ,
subtext_score DESC
选项 2 不是可行的竞争者。它只需要 3 个索引(节省不多),但由于 OR
.
而慢很多
选项 3...(选项 1,固定不变,加上...)
您使用的 ORDER BY
可能是您想要的 "wrong"。例如,它会将 toptitle
中没有 text
的任何行推到列表的末尾。也许你想要一些 "weighted" 版本:
ORDER BY
9 * top_title_score +
3 * title_score +
1 * sub_text_score DESC
(9,3,1比较随意,说的是'text'在title
出现3次以上,比在[=18出现一次更重要=]——或类似的东西。)
假设我有 table posts
与此列:
top_totle,title,sub_title,text
我需要对所有此列进行全文搜索并按相关性排序,其中 top_title 需要比标题等更重要
所以我有 2 个相同的问题,为此创建索引的最佳方法是什么以及如何格式化查询以最好地支持该索引?
索引选项: 我可以为这一列的所有内容创建组合全文索引,或者为每一列创建单独的索引
哪个是首选方式? 选项 1:
SELECT
title,
MATCH (top_title) AGAINST ('text' IN BOOLEAN MODE) as toptitle_score,
MATCH (title) AGAINST ('text' IN BOOLEAN MODE) as title_score,
MATCH (sub_text) AGAINST ('text' IN BOOLEAN MODE) as sub_text_score,
FROM
`posts`
WHERE
MATCH (top_title,title , sub_text ) AGAINST ('text' IN BOOLEAN MODE)
and `posts`.`deleted_at` IS NULL
AND `published_at` IS NOT NULL
Order by toptitle_score desc,
Order by title_score desc ,
Order by subtext_score desc
选项 2:
SELECT
title,
MATCH (top_title) AGAINST ('text' IN BOOLEAN MODE) as toptitle_score,
MATCH (title) AGAINST ('text' IN BOOLEAN MODE) as title_score,
MATCH (sub_text) AGAINST ('text' IN BOOLEAN MODE) as sub_text_score,
FROM
`posts`
WHERE
(MATCH (top_title) AGAINST ('text' IN BOOLEAN MODE)
or MATCH (title) AGAINST ('text' IN BOOLEAN MODE)
or MATCH (sub_text) AGAINST ('text' IN BOOLEAN MODE))
and `posts`.`deleted_at` IS NULL
AND `published_at` IS NOT NULL
Order by toptitle_score desc,
Order by title_score desc ,
Order by subtext_score desc
选项 3:
is there some smarter way?
选项 1 不错。它需要 4 个 FT 索引(每列一个,加上所有 3 列一个)。不要重复 ORDER BY
:
ORDER BY toptitle_score DESC ,
title_score DESC ,
subtext_score DESC
选项 2 不是可行的竞争者。它只需要 3 个索引(节省不多),但由于 OR
.
选项 3...(选项 1,固定不变,加上...)
您使用的 ORDER BY
可能是您想要的 "wrong"。例如,它会将 toptitle
中没有 text
的任何行推到列表的末尾。也许你想要一些 "weighted" 版本:
ORDER BY
9 * top_title_score +
3 * title_score +
1 * sub_text_score DESC
(9,3,1比较随意,说的是'text'在title
出现3次以上,比在[=18出现一次更重要=]——或类似的东西。)