太阳黑子搜索排序
我将 Sunspot (https://github.com/sunspot/sunspot) 与 Rails 一起使用。
这是我的模型:
class Item < ActiveRecord::Base
searchable do
boolean :red
boolean :blue
boolean :green
...
end
end
考虑以下搜索:
Item.search
any_of do
with :red, true
with :blue, true
with :green, true
end
end
如何对结果进行排序:包含所有颜色的项目,后跟包含 2 种颜色的项目,后跟包含 1 种颜色的项目?
注意:这只是一个搜索示例。答案应该考虑颜色的所有可能的搜索组合。
更新 1
按颜色数量排序将不起作用。例如,假设您有以下项目:
- 绿色/蓝色
- 绿色/红色/黑色
如果您搜索绿色和蓝色,则项目 2 将出现在项目 1 之前。
I'm using Sunspot (https://github.com/sunspot/sunspot) with Rails.
Here's my model:
class Item < ActiveRecord::Base
searchable do
boolean :red
boolean :blue
boolean :green
...
end
end
Consider the following search:
Item.search
any_of do
with :red, true
with :blue, true
with :green, true
end
end
How can I order the results like this: items containing all of the colors followed by items containing 2 of the colors followed by items containing 1 of the colors?
Note: This is just one example search. The answer should consider all possible search combinations for the colors.
Update 1
Ordering by number of colors won't work. For example, say you have the items:
- green/blue
- green/red/black
If you search for green and blue, item 2 will come before item 1.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
丑陋,但可能会解决问题:
然后,重新索引后:
希望这会有所帮助。
Ugly, but would probably do the trick:
Then, after re-indexing:
Hopefully, this helps.
哦!我不认为还有其他颜色在起作用......感谢您的更新。在这种情况下,另一种选择是将对象的所有颜色代码(名称)折叠到可搜索块的一个
text
字段中,然后在该字段上运行全文搜索,使用所有内容所需的颜色作为关键字。获得更多匹配的对象将获得最高的相关性分数,并且将首先返回。类似这样的:然后,您的搜索例程将如下所示:
当在“绿蓝色”上搜索时,上面的 item1 将获得完全匹配,并且将首先出现。
Oh! I didn't think that there where other colors at play... Thanks for the update. In this case, another option would be to collapse all of the object's color codes (names) in one
text
field in the searchable block, and then run a full-text search on on that field, using all of the needed colors as keyword. Object that would get more matches would get highest relevancy score, and would be returned first. Something like:Then, your search routine would look like:
item1 above would get exact match when searched on "green blue", and will come up first.