Search code examples
phpmysqlmatchmatch-against

Using CONCAT in MATCH AGAINST


I'm having a problem with a search page. I have a list of products which I want to search

the search must include:

  • product name - produse.produs
  • category - categorii.categorie
  • variant name - variante.nume_varianta
  • variant code - variante.nume_varianta

                    SELECT 
        produse.*, variante.*, categorii.categorie 
        FROM produse
        LEFT JOIN variante ON variante.id_produs=produse.id_produs
        LEFT JOIN categorii ON categorii.id_categorie=produse.id_categorie
        LEFT JOIN produse_valori ON produse_valori.id_produs=produse.id_produs
        WHERE MATCH (CONCAT_WS( produse.produs, variante.varianta_cod ,categorii.categorie, variante.nume_varianta))  AGAINST ( '.$keyword.' IN BOOLEAN MODE ) and produse.activ=1 
        GROUP BY produse.id_produs
    

But no results I get this error in phpMyAdmin:

#1064 - You have an error in your SQL syntax

Solution

  • Can i use a concat sintax (like the one i used) in a match against?

    No, you must not use CONCAT_WS() inside MATCH(). Just name the columns.

    The arguments to MATCH() must be the columns in a FULLTEXT index. All the columns defined for the index, in the same order as they are defined in the index.

    A FULLTEXT index, like any index, can only include columns from one table. There are no indexes in MySQL that span multiple tables.

    If you want to search multiple tables, you need a FULLTEXT index in each table. And you need one MATCH() for each table.

    SELECT 
    produse.*, variante.*, categorii.categorie 
    FROM produse
    LEFT JOIN variante ON variante.id_produs=produse.id_produs
    LEFT JOIN categorii ON categorii.id_categorie=produse.id_categorie
    LEFT JOIN produse_valori ON produse_valori.id_produs=produse.id_produs
    WHERE
    produse.activ=1 
    AND (
        MATCH(produse.produs) AGAINST('.$keyword.' IN BOOLEAN MODE) 
        OR MATCH(variante.varianta_cod, variante.nume_varianta) AGAINST('.$keyword.' IN BOOLEAN MODE) 
        OR MATCH(categorii.categorie) AGAINST('.$keyword.' IN BOOLEAN MODE) 
    )
    GROUP BY produse.id_produs
    

    If you need a fulltext index that spans tables, you'd have to use a different fulltext indexing tool like Sphinx Search or Apache Solr. These tools can index the results of any SELECT query, including queries with joins.