ruby on rails - Conditional order_by depending on data in Sunspot -
i wondering if there way sort using kind of if/then semantics sort order.
the basic requirement trying adress this:
- my records have contact_again_time time attribute can
nil. - the list should sorted in way records have
contact_again_timecome first orderedcontact_again_timedescending. - the rest
contact_again_timenil should ordered created_at in ascending.
so off top of head solve materializing order field index having searchable this:
integer :sort_key contact_again_time.nil? ? (created_at.to_i * -1) : contact_again_time.to_i end and order_by(:sort_key, :desc) want. way putting part of sort logic inside model , more importantly inside lucene index.
something i'd rather avoid feels rather wrong.
in solr, can adding sort parameter query: sort=contact_again_time desc,created_at asc
whenever contact_again_time present, sort in descending order (and use created_at break ties, irrelevant you). when created_at present sorted in ascending order.
this should translate in straight-forward manner sunspot.
update: if want sort sort=contact_again_time asc,created_at asc documents contact_again_time missing first, mention in comment below. push these documents end, add sortmissinglast="true" contact_again_time in solr schema.xml. if created_at can absent in documents , desire same behavior, should add sortmissinglast too.
Comments
Post a Comment