parseSortString() in Solr_Base_Query.php uses preg_match() to check against available sorts, like so (around line 540):
$fields = implode('|', array_keys($this->available_sorts));
if (preg_match('/^(?:(' . $fields . ') (asc|desc),?)+$/', $sortstring, $matches)) {
Nowhere are available_sorts escaped however, so attempting to sort on a function like geodist() doesn't work immediately like you would assume it should. For example, this doesn't work because the parens are applied to the regex without being escaped:
$query->setAvailableSort('geodist()', array('title' => 'Distance', 'default' => 'asc'));
$query->setSolrsort('geodist()', 'asc');
Whereas this does:
$query->setAvailableSort(preg_quote('geodist()'), array('title' => 'Distance', 'default' => 'asc'));
$query->setSolrsort('geodist()', 'asc');
This escaping should probably happen somewhere in Solr_Base_Query though.
Note: Since none of the core sorts have parens and geospatial querying is still a mostly custom thing, this is definitely low priority.
Comments
Comment #1
wintersieck commentedOne possible way of fixing attached (might be a cleaner way though?)
Comment #2
janusman commentedI can say that the patch worked for me; I can now do this:
on a hook_query_prepare() implementation, and the request on my Solr log shows:
... &pt=52.936157680%2C-2.163181068&sort=geodist%28%29%20asc& ...I'm marking it as 'needs work' however, since I think it needs a comment or two just to explain what's going on.
Comment #3
wintersieck commentedRe-rolled the patch with a couple comments.
Comment #4
chertzog#3 works for me.
Comment #5
nick_vhWe need unit tests for this
Comment #6
nick_vhAdded some unit tests
Comment #7
nick_vhAdded some fixes to the base class also and removed the watchdog entry as it really did not belong there
Comment #8
nick_vhAllow function sorts to be added without a problem was committed, still needs backport to 6.x-3.x.