Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

full-text mysql search in rails

I'm trying to add a simple mysql full-text search to a small table <2000 entries.

Please don't tell me to install solr, or any other search gems. I've tried to run them and it seems to be one issue after another. I'll get around to it one day, but that day is not today.

I need to add an add_index migration, but when I run

add_index :users, :name, :fulltext

I get an error. - undefinded method 'key'.

I can't seem to find any documentation anywhere which explains how to make a fulltext mysql search in rails.

What is the correct statement I'm supposed to use for the add_index, and once that is done, do I have to anything special in my model to use the fulltext search?

like image 290
pedalpete Avatar asked Feb 28 '11 21:02

pedalpete


2 Answers

Maybe an late answer but someone else might search for this like I just did.

This worked for me.

add_index :users, :name, name: 'name', type: :fulltext

Or for multiply columns

add_index :users, [:name, :description], name: 'name_description', type: :fulltext
like image 63
aross Avatar answered Oct 24 '22 23:10

aross


You can do the following in your migration

execute "CREATE FULLTEXT INDEX fulltext_name ON users (name)"

I would then suggest following this blog post about creating rake tasks and a schema.rb file that is compatible with this index type.

UPDATE: rigth link to blog post

like image 36
Pan Thomakos Avatar answered Oct 24 '22 23:10

Pan Thomakos