【问题标题】:mysql full text search right indexmysql全文检索权索引
【发布时间】:2017-11-11 15:06:45
【问题描述】:

可以说我有表 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?

【问题讨论】:

    标签: mysql full-text-search innodb mariadb full-text-indexing


    【解决方案1】:

    选项 1 很好。它需要 4 个 FT 索引(每列一个,加上一个包含所有 3 列的索引)。不要重复ORDER BY

    ORDER BY toptitle_score DESC , 
             title_score    DESC , 
             subtext_score  DESC
    

    选项 2 不是一个可行的竞争者。它只需要 3 个索引(节省不多),但由于 OR 而速度要慢很多。

    选项 3...(选项 1,固定不变,加上...)

    您使用的ORDER BY 可能与您想要的“错误”。例如,它会将toptitle 中没有text 的所有行推到列表的末尾。也许你想要一些“加权”版本:

    ORDER BY
       9 * top_title_score  +
       3 * title_score      +
       1 * sub_text_score  DESC
    

    (9,3,1 相当随意。它表示如果“文本”在title 中出现超过 3 次,这比在top_title 中出现一次更重要——或者类似的东西.)

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 2013-03-12
      • 1970-01-01
      相关资源
      最近更新 更多