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

wintersieck’s picture

Category: feature » bug
Status: Active » Needs review
StatusFileSize
new739 bytes

One possible way of fixing attached (might be a cleaner way though?)

janusman’s picture

Status: Needs review » Needs work

I can say that the patch worked for me; I can now do this:

   $query->setAvailableSort('geodist()', array('title' => 'Distance', 'default' => 'asc'));
   $query->setSolrSort('geodist()', 'asc');

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.

wintersieck’s picture

Status: Needs work » Needs review
StatusFileSize
new962 bytes

Re-rolled the patch with a couple comments.

chertzog’s picture

#3 works for me.

nick_vh’s picture

Status: Needs review » Needs work

We need unit tests for this

nick_vh’s picture

Status: Needs work » Needs review
StatusFileSize
new4.79 KB

Added some unit tests

nick_vh’s picture

Added some fixes to the base class also and removed the watchdog entry as it really did not belong there

nick_vh’s picture

Version: 7.x-1.x-dev » 6.x-3.x-dev
Status: Needs review » Patch (to be ported)

Allow function sorts to be added without a problem was committed, still needs backport to 6.x-3.x.