Mysql full-text index weight

can mysql set different weights for different fields when building a full-text index? For example, if you have fields with title and content, you want the weight of title to be higher than that of content

.

refer to http://m.lao8.org/a1602 article to solve the weight problem through SQL


there is no direct weight setting, but you can do it by weighting two indexes, see the following code:

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;

the results are as follows:

| 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 |
Menu