sunspot/sunspot — pull request #314 by throwern (2012-11-13 19:43:00+00:00)
Title: order_by_function field query

I recently added the sort by function capability from Solr 3.1 to one of my projects. I thought I would share my code in case anyone is interested, and to get feedback on my implementation. I'm not sure if this is already being worked on but I couldn't find an open issue for it.

The commit adds an 'order_by_function' method to the field queries. It allows for any Solr FunctionQuery, nesting, and literal constants.

# For example, to order by field1 + (field2*field3):
#
# order_by_function :desc, :sum, :field1, [:product, :field2, :field3]
#
# ==== Parameters
# direction::
# :asc or :desc
# function_name::
# the function to run
# parameters::
# the parameters for this function.
# - Symbol for a field_name
# - Array for a nested function
# - String for a literal constant