full-text mysql search in rails

痞子三分冷 提交于 2019-12-21 12:31:41

问题


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?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/5147827/full-text-mysql-search-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!