Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

cchacholiades's avatar

Full text search

The last days I've been trying to figure out how to implement a full text search in my Laravel 4.1 application.

I found that full text search is available in InnoDB from mysql version 5.6 and above. I am currently running mysql 5.6.15 on my localhost configuration and having problems to get this working.

I have three tables declared (category, brand, product), all of them set to InnoDB engine. In my products migration table I have set:

DB::statement('ALTER TABLE `products` ADD FULLTEXT search(name, description)');

Initially I've tried to run a query on the products table which matches the search terms against the name and description fields on products table. I have a ProductsSearchService that has a search function like so to run that query:

public function search($data = array()) {
    $exp = explode(' ', $data);
    $s = '';
    $c = 1;

    foreach ($exp as $e)
   {
      $s .= "+$e*";
      if ($c + 1 == count($exp))
        $s .= ' ';
      $c++;
    }
    
    $query = "MATCH (name, description) AGAINST ('$s' IN BOOLEAN MODE)";
    $products = Product::whereRaw($query)->get();
    
    return $products;
  }

Although, this returns with an error if the table's engine is InnoDB:

SQLSTATE[HY000]: General error: 1191 Can't find FULLTEXT index matching the column list (SQL: select * from `products` where MATCH (name, description) AGAINST ('+product*' IN BOOLEAN MODE))

The above error is gone if I change my table's default engine to MYISAM. Additionally, the categories and brands table are connected to the products table with foreign keys.

1. Since MYISAM does not support foreign keys how can I get this to work?

2. Also, is there a way to adjust the search query so that it will match these two tables as well (categories and brands)?

0 likes
11 replies
bashy's avatar

Try this?

DB::statement('ALTER TABLE products ADD FULLTEXT fulltext_index(name, description)');
cchacholiades's avatar

Ok, i think i figured it out. On my migration I also had

$table->dropIndex('fulltext_index');

I removed it and it's working now. Is there a way to match the other two tables as well?

bashy's avatar

Ah good stuff. Match other tables for searching?

I can see this

I have three tables declared (category, brand, product)

cchacholiades's avatar

Yes. Is it possible to match values in other tables as well? The categories and brands tables have foreign keys on the products

bashy's avatar
bashy
Best Answer
Level 65

Yeah I don't think it's possible to full text search across two tables but I haven't looked into it that much. Maybe use a dedicated search tool? (Sphinx etc)

stickman373's avatar

I think as @bashy said you might need to use a dedicated search tool, something like SphinxSearch to search multiple tables full text with weighting. I've done this before and it's not too bad to setup.

cchacholiades's avatar

Hm, I'll have a look at this. Thanks for your replies, although it would be nice if that was somehow achievable.

bashy's avatar

Don't think the full text search can span across two tables, that's all. Sounds like your search is quite a big part of the app so I'd look into a search tool for it.

Sphinx etc.

Please or to participate in this conversation.