2013-06-09 8 views
8

ho una stringa che contiene alcune parole chiave come questo $string = 'one, two, "phrase three words"' Sto cercando di fare una ricerca full-text utilizzando:MySql ricerca full-text in PHP utilizzando stringa di parole chiave contaning

SELECT *, MATCH (column) AGAINST ('$string') AS score, 
FROM table WHERE MATCH (column) AGAINST ('$string' IN BOOLEAN MODE) ORDER BY score DESC 

Quando MySql restituisce i risultati, la parola chiave "frase tre parole" non corrisponde a una frase, ma ogni parola da essa viene abbinata come una singola.

Come devo modificare la stringa o la query per ricevere i risultati in cui corrisponde l'intera frase? Ho provato con stripslashes() per la stringa, ma il risultato è lo stesso.

risposta

24

Come MySQL manuale dice:.

Una frase che è racchiuso all'interno di virgolette (“"”) caratteri partite solo le righe che contengono la frase alla lettera, come è stato digitato

Diamo un'occhiata alla tabella di esempio:

mysql> select * from articles; 
+----+-----------------------+------------------------------------------+ 
| id | title     | body          | 
+----+-----------------------+------------------------------------------+ 
| 1 | PostgreSQL Tutorial | DBMS stands for DataBase ...    | 
| 2 | How To Use MySQL Well | After you went through a ...    | 
| 3 | Optimizing MySQL  | In this tutorial we will show ...  | 
| 4 | 1001 MySQL Tricks  | 1. Never run mysqld as root. 2. ...  | 
| 5 | MySQL vs. YourSQL  | In the following database comparison ... | 
| 6 | MySQL Security  | When configured properly, MySQL ...  | 
+----+-----------------------+------------------------------------------+ 

mysql> SELECT * FROM articles WHERE MATCH (title,body) 
    AGAINST ('"database comparison"' IN BOOLEAN MODE); 

+----+-------------------+------------------------------------------+ 
| id | title    | body          | 
+----+-------------------+------------------------------------------+ 
| 5 | MySQL vs. YourSQL | In the following database comparison ... | 
+----+-------------------+------------------------------------------+ 

L'ordine conta, quando le parole sono quotate:

mysql> SELECT * FROM articles WHERE MATCH (title,body) 
    AGAINST ('"comparison database"' IN BOOLEAN MODE); 

Empty set (0.01 sec) 

Quando rimuoviamo le citazioni, cercherà per le righe, contenente le parole "database" o "confronto":

mysql> SELECT * FROM articles WHERE MATCH (title,body) 
    AGAINST ('database comparison' IN BOOLEAN MODE); 

+----+---------------------+------------------------------------------+ 
| id | title    | body          | 
+----+---------------------+------------------------------------------+ 
| 1 | PostgreSQL Tutorial | DBMS stands for DataBase ...    | 
| 5 | MySQL vs. YourSQL | In the following database comparison ... | 
+----+---------------------+------------------------------------------+ 

Ordine non ha importanza ora:

mysql> SELECT * FROM articles WHERE MATCH (title,body) 
    AGAINST ('comparison database' IN BOOLEAN MODE); 

+----+---------------------+------------------------------------------+ 
| id | title    | body          | 
+----+---------------------+------------------------------------------+ 
| 1 | PostgreSQL Tutorial | DBMS stands for DataBase ...    | 
| 5 | MySQL vs. YourSQL | In the following database comparison ... | 
+----+---------------------+------------------------------------------+ 

Se vogliamo ottenere le righe, contenenti la parola "PostgreSQL" o la frase "confronto dei database", dovremmo usare questa richiesta:

mysql> SELECT * FROM articles WHERE MATCH (title,body) 
    AGAINST ('PostgreSQL "database comparison"' IN BOOLEAN MODE); 

+----+---------------------+------------------------------------------+ 
| id | title    | body          | 
+----+---------------------+------------------------------------------+ 
| 1 | PostgreSQL Tutorial | DBMS stands for DataBase ...    | 
| 5 | MySQL vs. YourSQL | In the following database comparison ... | 
+----+---------------------+------------------------------------------+ 

fiddle

Assicurarsi, che le parole, si sta cercando, non sono nella list of stopwords, che vengono ignorati.

+0

Hi user4035, Capisco le nozioni di base di ricerca full-text e come funziona, il mio punto è quello di ottenere i risultati come nel vostro ultimo esempio, ma il problema è quando ho messo una stringa che contiene virgolette doppie contro. I risultati non contengono la frase completa, ma le parole nella frase. – Vasko

+0

@Vasko Questo è molto strano, perché per me quando inserisco le parole tra virgolette, MySQL cerca la frase esatta e non le parole. Per favore, copia la mia tabella di esempio dal violino e prova l'ultima query nel tuo sistema. Fammi sapere, come funziona. – user4035

+1

Ho usato http://sphinxsearch.com/ invece della ricerca FULLTEXT di mySql e ora l'applicazione è molto più reattiva. Grazie comunque per la tua risposta dettagliata. – Vasko

Problemi correlati