La ricerca full-text può sembrare rotta quando la testi su un dataset minuscolo, e l'esempio qui sotto è la perfetta illustrazione del perché:
-- drop table testproduct; CREATE TABLE testproduct ( Id VARCHAR(16), prod_name TEXT, ProductIdType VARCHAR(8), PRIMARY KEY (Id), FULLTEXT (prod_name) ) ENGINE=MyISAM; insert into testproduct (id,prod_name,productidtype) values ('B00005N5PF','one pen and a good price for a pen','ASIN'); insert into testproduct (id,prod_name,productidtype) values ('B570J5XS3C',null,'ASIN'); insert into testproduct (id,prod_name,productidtype) values ('C00ZZ5N5PF','let us get rid of some noise','ASIN'); insert into testproduct (id,prod_name,productidtype) values ('D00LL5N5PA','four score and seven years ago our fore...','ASIN'); insert into testproduct (id,prod_name,productidtype) values ('EEEZZ5N5PF','he has a harpoon','ASIN'); insert into testproduct (id,prod_name,productidtype) values ('C01ZZ5N5PF','and then we','ASIN'); insert into testproduct (id,prod_name,productidtype) values ('B00ZZ5N5PF','he has a pen in his pocket not a banana','ASIN'); insert into testproduct (id,prod_name,productidtype) values ('C02ZZ5N5PF','went to the store','ASIN'); insert into testproduct (id,prod_name,productidtype) values ('C03ZZ5N5PF','and decided that we should buy some','ASIN'); insert into testproduct (id,prod_name,productidtype) values ('C04ZZ5N5PF','fruit cups or fruit or berries or pebbles','ASIN'); insert into testproduct (id,prod_name,productidtype) values ('C037Z5N5PF','then he and her she and it','ASIN'); insert into testproduct (id,prod_name,productidtype) values ('C04K95N5PF','threw some daggers and a harpoon','ASIN'); insert into testproduct (id,prod_name,productidtype) values ('D88895N5PF','more noise and some of this','ASIN'); insert into testproduct (id,prod_name,productidtype) values ('D34595N5PF','this article about harpoons really drills into the throwing of harpoon or harpoons to those that deserve a harpoon','ASIN'); insert into testproduct (id,prod_name,productidtype) values ('D12395N5PF','and there we go','ASIN'); La ricerca full-text si basa sulla varietà delle parole per filtrare il "rumore" ripetuto. Con solo una manciata di righe, l'indice ha ben poco su cui lavorare, quindi i risultati sembrano scarsi. Prova a sottoporle la tua intera raccolta prima di trarre conclusioni, e tieni presente che esistono impostazioni che regolano la lunghezza minima di parola che il motore proverà a indicizzare.
Stop Words
MySQL include liste di stop word integrate per varie lingue. Sono parole comuni e poco significative che la ricerca salta del tutto. La lista è compilata nel server, ma puoi sovrascriverla come descritto nel manuale:
Per sovrascrivere l'elenco predefinito di stopword, imposta la variabile di sistema ft_stopword_file. Il valore della variabile deve essere il percorso del file contenente l'elenco delle stopword, oppure la stringa vuota per disabilitare il filtraggio delle stopword. Il server cerca il file nella directory dei dati, a meno che non venga fornito un percorso assoluto per specificare una directory diversa. Dopo aver modificato il valore di questa variabile o il contenuto del file delle stopword, riavvia il server e ricostruisci i tuoi indici FULLTEXT.
Alcune query di esempio
-- select * from testproduct SELECT * FROM testproduct WHERE MATCH(prod_name) AGAINST('score' IN BOOLEAN MODE); SELECT * FROM testproduct WHERE MATCH(prod_name) AGAINST('harpoon' IN BOOLEAN MODE); SELECT * FROM testproduct WHERE MATCH(prod_name) AGAINST('banana' IN BOOLEAN MODE); SELECT * FROM testproduct WHERE MATCH(prod_name) AGAINST('years' IN BOOLEAN MODE); Per richiedere la corrispondenza di più parole, combinane con l'operatore +:
SELECT id,prod_name, match( prod_name ) AGAINST ( '+harpoon +article' IN BOOLEAN MODE ) AS relevance FROM testproduct ORDER BY relevance DESC Passando alla modalità linguaggio naturale ottieni un vero peso di rilevanza nella colonna relevance:
SELECT id,prod_name, match( prod_name ) AGAINST ( '+harpoon +article' IN NATURAL LANGUAGE MODE) AS relevance FROM testproduct ORDER BY relevance DESC +------------+--------------------------------------------------------------------------------------------------------------------+--------------------+ | id | prod_name | relevance | +------------+--------------------------------------------------------------------------------------------------------------------+--------------------+ | D34595N5PF | this article about harpoons really drills into the throwing of harpoon or harpoons to those that deserve a harpoon | 3.6207125186920166 | | EEEZZ5N5PF | he has a harpoon | 1.2845110893249512 | | C04K95N5PF | threw some daggers and a harpoon | 1.2559525966644287 | |------------+--------------------------------------------------------------------------------------------------------------------+--------------------+ Il merito della tecnica con più parole va a questa discussione — grazie, spencer.