0
我想写一个相对简单的算法搜索字符串的几个属性搜索和排名结果
给出了一些数据:
一些数据:
1: name: 'Josh', location: 'los angeles'
2: name: 'Josh', location: 'york'
搜索字符串:“josh york”
结果应该是[2,1],因为该查询字符串会两次击中第二条记录,并且第一个r ecord一次。
这里假设不区分大小写是安全的。
所以这里是我到目前为止,在红宝石/活动记录:
query_string = "josh new york"
some_attributes = [:name, :location]
results = {}
query_string.downcase.split.each do |query_part|
some_attributes.each do |attribute|
find(:all, :conditions => ["#{attribute} like ?", "%#{query_part}%"]).each do |result|
if results[result]
results[result] += 1
else
results[result] = 1
end
end
end
end
results.sort{|a,b| b[1]<=>a[1]}
我有这种方法的问题是它会产生大量的查询(query_string.split.length * some_attributes。长度)。
我可以通过减少查询次数吗?
我很喜欢在ruby中进行排序,虽然如果这可以以某种方式堵塞到SQL中,那也不错。
我想任何“全功能”的文本搜索“引擎”是矫枉过正的搜索一个表中的两列。在这里寻求简单/轻量。 – jsharpe 2009-09-24 14:19:14