Search code examples
ruby-on-railsthinking-sphinx

Thinking Sphinx- order by year and relevance


I'm using ThinkingSphinx as search engine on webpage. What I need is to sort search result by year and relevance.

As order: "DATE_FORMAT(created_at, '%Y') DESC" doesn't work and Time segments aren't useful at all for what I want, I'm getting out of ideas.


Solution

  • The YEAR function does what you need, but it's worth noting that you'll need to use it in the SELECT clause, give the result an alias, and then refer to that alias in your ORDER clause.

    This is presuming you're using TS v3:

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