ruby on rails mysql全文相关性得分数组

时间:2014-07-20 18:59:14

标签: mysql ruby-on-rails ruby full-text-search match

我有一个(mysql-)表,其中包含两列的全文索引 - >名字,中间名。

当我使用SQl时

SELECT firstname, middlename, MATCH (firstname, middlename) as score AGAINST ('Peter') FROM database.tbl_names where MATCH (firstname, middlename) AGAINST ('Peter')

我得到3列 - >名字,中间名和分数。

但是当我尝试使用红宝石时

TblName.find(:all, :select => "firstname, middlename, match(firstname, middlename) against ('Peter')as score", :order => 'score desc', :conditions => ['match(firstname, middlename) against (?)', 'Peter'], :limit => 20)

我只得到一个包含两个"列的数组"但没有"匹配列"

<TblName firstname: "Peter", middlename: "">

我在整个星期天搜索了一个尝试过,并没有找到&#34; match-column&#34;的解决方案。在阵列中。

有人能帮助我吗?

提前致谢!!!

2 个答案:

答案 0 :(得分:1)

Rails 4版本:

Model.select("*, MATCH(title) AGAINST ('keyword') AS score").where("MATCH(title) AGAINST('keyword')").reorder("score DESC").first.score

答案 1 :(得分:0)

Rails不会在返回的数组中显示“score”属性(因为它未在模型中初始化)。您必须使用属性调用来查找它。

尝试:

TblName.find(:all, :select => "firstname, middlename, match(firstname, middlename) against ('Peter') as score", :order => 'score desc', :conditions => ['match(firstname, middlename) against (?)', 'Peter'], :limit => 20).first.score

看看你是否能以这种方式得分。

您还可以使用以下方法获取该记录的属性列表(包括分数):

TblName.find(:all, :select => "firstname, middlename, match(firstname, middlename) against ('Peter') as score", :order => 'score desc', :conditions => ['match(firstname, middlename) against (?)', 'Peter'], :limit => 20).first.attributes

如果您想一次查看所有记录的属性,可以这样做:

TblName.find(:all, :select => "firstname, middlename, match(firstname, middlename) against ('Peter') as score", :order => 'score desc', :conditions => ['match(firstname, middlename) against (?)', 'Peter'], :limit => 20).collect!(&:attributes)