mysql全文索引权重

发布于 2022-09-11 14:38:50 字数 74 浏览 14 评论 0

mysql建立全文索引时,能对不同字段设置不同权重吗?比如有title和content的字段,想让title的权重比content要高

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以可爱出名 2022-09-18 14:38:50

可参考 http://m.lao8.org/a1602 文章,通过SQL来解决权重问题

吃素的狼 2022-09-18 14:38:50

没有直接权重设定, 但你可以通过两条索引加权来完成, 参考以下的代码:

drop table if exists articles ;

CREATE TABLE articles (
id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
title VARCHAR(200),
content TEXT,
FULLTEXT (title),
FULLTEXT (content)

) ENGINE=InnoDB;

INSERT INTO articles (title, content) VALUES
('MySQL Tutorial','This database tutorial ...'),
("How To Use MySQL",'After you went through a ...'),
('Optimizing Your Database','In this database tutorial ...'),
('MySQL vs. YourSQL','When comparing databases ...'),
('MySQL Security','When configured properly, MySQL ...'),
('Database, Database, Database','database database database'),
('1001 MySQL Tricks','1. Never run mysqld as root. 2. ...'),
('MySQL Full-Text Indexes', 'MySQL fulltext indexes use a ..');   




-- 让 content 有三倍权值
SELECT id, title, content, MATCH (content)  AGAINST ('database' IN BOOLEAN MODE)
AS score1, MATCH (title)  AGAINST ('database' IN BOOLEAN MODE)
AS score2, (MATCH (content)  AGAINST ('database' IN BOOLEAN MODE))*3+(MATCH (title)  AGAINST ('database' IN BOOLEAN MODE)) as score3 FROM articles ORDER BY score1*3+score2 DESC;

得到结果如下:

| id | title                        | content                                | score1              | score2             | score3             |
|  6 | Database, Database, Database | database database database          |  0.5443480610847473 | 1.0874286890029907 | 2.7204728722572327 |
|  3 | Optimizing Your Database     | In this database tutorial ...       | 0.18144935369491577 | 0.3624762296676636 | 0.9068242907524109 |
|  1 | MySQL Tutorial               | This database tutorial ...          | 0.18144935369491577 |                  0 | 0.5443480610847473 |
|  2 | How To Use MySQL             | After you went through a ...        |                   0 |                  0 |                  0 |
|  4 | MySQL vs. YourSQL            | When comparing databases ...        |                   0 |                  0 |                  0 |
|  5 | MySQL Security               | When configured properly, MySQL ... |                   0 |                  0 |                  0 |
|  7 | 1001 MySQL Tricks            | 1. Never run mysqld as root. 2. ... |                   0 |                  0 |                  0 |
|  8 | MySQL Full-Text Indexes      | MySQL fulltext indexes use a ..     |                   0 |                  0 |                  0 |
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文