Search with LIKE or MATCH..AGAINST in two columns

14

I have a classified system where I search with PHP and MySQL using LIKE in two fields:

campo1 like '' or campo2 like ''
The problem is when I look for something like "Green car" it does not display anything for "car" being contained in field 1 and "green" in field 2, or even car come with a word in the middle before the "Green".

Could someone give me a hint how to improve this search?

I tried to use:

SELECT description, title , MATCH ( title, description ) AGAINST ( '$busca' ) AS Score    
FROM qtc_ads WHERE MATCH ( title, description ) AGAINST ( '$busca' )
ORDER BY Score DESC LIMIT 50

But MySQL informs me that the fields do not support this type of search and I do not know if this is the best way.

SQL that I currently use to fetch from my system:

SELECT * FROM 'qtc_ads' AS 'qtc_ad' WHERE 'status' = 1 AND 'title' LIKE '%l200%' (1)
    
asked by anonymous 16.04.2014 / 20:26

2 answers

16

A rather painful possibility, but with a more complete result, is to use PHP to split your search into separate words, and generate the WHERE clause for you:

<?php

   $pesquisa = 'carro verde amassado ';

   // Aqui você pode juntar vários campos no concat.
   $campo = 'CONCAT( title, " ", description, " ", author)';
   // Ou usar um só, mas nesse caso talvez compense um LIKE tradicional
   // $campo = 'title';

   $palavras = explode( ' ', $pesquisa ); // dividindo as palavras pelo espaço
   $palavras = array_filter($palavras); // eliminando ítens vazios

   $where = '';
   $cola = 'WHERE ';

   foreach ($palavras as $palavra) {
      $palavra = trim($palavra); //Removendo espaços em branco
      $palavra = mysql_real_escape_string($palavra); //Precisa da conexao com o banco!
      $where .= $cola.campo.' LIKE "%'.$palavra.'%" ';
      $cola = 'AND ';
   }

   echo htmlentities( $where );
?>

And the result will be:

WHERE
   CONCAT( title, " ", description, " ", author) LIKE "%carro%" AND
   CONCAT( title, " ", description, " ", author) LIKE "%verde%" AND
   CONCAT( title, " ", description, " ", author) LIKE "%amassado%"

(line breaks added for easier reading)

In this way the search will find all these results:

O carro amassado era verde
A carroça tinha abacates verdes amassados
Verde carro, amassado tu és

Note that although some lines do not have exact results, it's better to have more things than the user can not find what they need. Keep in mind, however, that the price you pay for complexity is a slower search. LIKE and indexes do not work well together.

    
16.04.2014 / 20:51
0

My opinion is to have only one LIKE in your query, and you can still keep two fields in your system, where a field would be the object ("Car", "Moto", ..., ...) and in the other field would be a characteristic ("Green", "Old", ..., ...), then when you go to query, you would concatenate those two fields to put in your query.

Ex: value_um = object; value_dois = characteristic; query_value = onevalue + twovalue field LIKE "'+ value_query +"'% ";

Or leave it to be and use the percentage (%) on like on both sides, that you will surely find something in either field one or field two.

I hope it helps.

    
16.04.2014 / 20:43