Hi Misha

Yes, it’s definitely possible. You can supply a custom SELECT clause with your 
dynamic attribute to Sphinx (this is for SphinxQL, not SQL) and then order by 
it. Sphinx has a bunch of functions - in this case, the IF statement should do 
the trick well enough:
http://sphinxsearch.com/docs/current.html#expressions
http://sphinxsearch.com/docs/current.html#expr-func-if

This is presuming item_photo_id and both picked_at columns are available in 
your index definition as attributes:

  Item.search :select => '*, IF(item_photo_id = 0, picked_at, 
variation_picked_at) AS combined_picked_at',
    :order => ‘combined_picked_at DESC'

Worth noting that NULLs are treated as zeros by Sphinx, hence the comparison in 
the IF statement.

Hope this helps!

— 
Pat

On 4 Nov 2014, at 3:37 am, Misha Slyusarev <[email protected]> wrote:

> I would like to be able ordering by a calculated field. Which is constructed 
> from a field in current table and if it's empty use a field from a associated 
> table.
> 
> I used to use case when in my regular Rails requests.
> 
> items.joins('left join item_variations iv on iv.item_id=items.id').
>       select('case when iv.item_photo_id is not null then iv.picked_at else 
> items.picked_at end as combined_picked_at')
> Now I'm looking for a way to use similar technique in Sphinx. Is it possible?
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Thinking Sphinx" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/thinking-sphinx.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Thinking Sphinx" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/thinking-sphinx.
For more options, visit https://groups.google.com/d/optout.

Reply via email to