思考狮身人面像 - 按年份和相关性排序

时间:2014-03-13 11:45:21

标签: ruby-on-rails thinking-sphinx

我在网页上使用ThinkingSphinx作为搜索引擎。我需要的是按年份和相关性对搜索结果进行排序。

由于order: "DATE_FORMAT(created_at, '%Y') DESC"不起作用,时间段对我想要的东西根本没用,所以我没有想法。

2 个答案:

答案 0 :(得分:0)

YEAR函数可以满足您的需要,但值得注意的是,您需要在SELECT子句中使用它,给结果一个别名,然后引用该别名在你的ORDER条款中。

这假设你正在使用TS v3:

Model.search 'foo',
  :select => '*, YEAR(created_at) as created_at_year, weight() as weight',
  :order  => 'created_at_year DESC, weight DESC'

答案 1 :(得分:0)

使用Sphinx 2.1.6在OSX上思考Sphinx V2的工作代码

:sphinx_select => "*, YEAR(created_at) as created_at_year", :order => "created_at_year DESC, weight() DESC")

使用Sphinx 2.0.6在BSD上思考Sphinx V2的代码

:sphinx_select => "*, YEAR(created_at) as created_at_year, weight() as my_weight", :order => "created_at_year DESC, my_weight DESC") # can't be aliased as weight!