Question

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.

Was it helpful?

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'

OTHER TIPS

Working code for Thinking Sphinx V2 on OSX with Sphinx 2.1.6

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

Code for Thinking Sphinx V2 on BSD with Sphinx 2.0.6

: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!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top