Mysql
 SQL >> Database >  >> RDS >> Mysql

Perché ft_min_word_len non ha effetto su MySQL in Ubuntu

Ho individuato il problema e la soluzione. Le impostazioni full-text erano corrette fin dall'inizio: volevo cercare con almeno 2 caratteri e avevo impostato ft_min_word_len = 2.

Il sintomo

Proseguendo nei test, ho notato che alcune parole di due caratteri venivano trovate e altre ignorate. Per esempio:

CREATE TABLE articles ( id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, title VARCHAR(200), body TEXT, FULLTEXT (title,body) ); INSERT INTO articles (title,body) VALUES ('MySQL Tutorial','DBMS stands for DataBase ...'), ('How To Use MySQL Well','After you went through a ...'), ('Optimizing MySQL','In this tutorial we will show ...'), ('1001 MySQL Tricks','1. Never run mysqld as root. 2. ...'), ('MySQL vs. YourSQL','In the following database comparison ...'), ('MySQL Security','When configured properly, MySQL ...'); mysql> SELECT * FROM articles WHERE MATCH (title,body) AGAINST ('use'); Empty set (0.00 sec) mysql> SELECT * FROM articles WHERE MATCH (title,body) AGAINST ('the'); Empty set (0.00 sec) mysql> SELECT * FROM articles WHERE MATCH (title,body) AGAINST ('vs.'); Empty set (0.00 sec) 

Questa invece funziona:

mysql> SELECT * FROM articles -> WHERE MATCH (title,body) AGAINST ('run'); +----+-------------------+-------------------------------------+ | id | title | body | +----+-------------------+-------------------------------------+ | 4 | 1001 MySQL Tricks | 1. Never run mysqld as root. 2. ... | +----+-------------------+-------------------------------------+ 

La causa reale: ft_stopword_file

Il colpevole era un altro: ft_stopword_file, cioè l'elenco delle parole ignorate nelle ricerche. La lista predefinita è documentata qui e contiene proprio termini comuni come "use", "the" e "vs".

Per consentire la ricerca di qualunque parola di almeno due caratteri:

  • imposta ft_min_word_len a 2;
  • nel file di configurazione di MySQL (su Debian /etc/mysql/my.cnf) aggiungi ft_stopword_file='path/to/stopword_file.txt', che puoi anche lasciare vuoto.

Un dettaglio fondamentale: dopo queste modifiche MySQL va riavviato e, se cambi ft_min_word_len, devi anche ricostruire l'indice (oppure riparare la tabella) perché il nuovo valore abbia effetto.