我想實現與MySQL基本的全文搜索。MySQL的全文搜索on Rails的
我寫了這個遷移:
def self.up
execute 'ALTER TABLE photos ENGINE = MyISAM'
execute 'CREATE FULLTEXT INDEX fulltext_photos ON photos (place, info)'
end
def self.down
execute 'ALTER TABLE photos ENGINE = InnoDB'
execute 'DROP INDEX fulltext_photos ON photos'
end
這是我的模型:
def self.search(*args)
options = args.extract_options!
find_by_sql [ "SELECT * FROM photos WHERE MATCH (place, info) AGAINST (?)", options[:query] ]
end
的問題是,這種代碼總是返回一個空數組。
例如:
% Photo.find(:first) => Photo id: 1, place: "Baceno", info: "Era immerso in erba alta." ... % Photo.search(:all, :query => 'baceno') => []
這個遷移腳本效果很好,但記住要恢復self.down方法中的順序,在將表更改回InnoDB之前必須刪除索引,否則您會失敗。 – 2014-06-12 04:06:35