Index: apachesolr.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr/apachesolr.module,v
retrieving revision 1.1.2.12.2.150
diff -u -p -r1.1.2.12.2.150 apachesolr.module
--- apachesolr.module	30 Jun 2009 10:57:24 -0000	1.1.2.12.2.150
+++ apachesolr.module	30 Jun 2009 19:44:12 -0000
@@ -970,10 +970,15 @@ function apachesolr_modify_query(&$query
     $function_name = $module . '_apachesolr_modify_query';
     $function_name($query, $params, $caller);
   }
+  // TODO: The query object should hold all the params.
   // Add array of fq parameters.
   if ($query && ($fq = $query->get_fq())) {
     $params['fq'] = $fq;
   }
+  // Add sort if present.
+  if ($query && ($sort = $query->get_solrsort())) {
+    $params['sort'] = $sort;
+  }
 }
 
 /**
@@ -1347,7 +1352,12 @@ function apachesolr_l($text, $path, $opt
   $options += array(
     'attributes' => array(),
     'html' => FALSE,
+    'query' => array(),
   );
+  if (is_array($options['query'])) {
+    $exclude = array('q' => 1, 'page' => 1, 'filters' =>1, 'solrsort' =>1) + $options['query'];
+    $options['query'] += array_diff_key($_GET, $exclude);
+  }
   // Don't need this, and just to be safe.
   unset($options['attributes']['title']);
   // Double encode + characters for clean URL Apache quirks.
@@ -1459,7 +1469,7 @@ interface Drupal_Solr_Query_Interface {
    */
   function get_path();
 
-  /**
+ /**
    * Return any query string for use in the l function.
    *
    * @see l()
@@ -1474,12 +1484,15 @@ interface Drupal_Solr_Query_Interface {
   /**
    * Set the solrsort.
    *
-   * @param string raw string to set the sort to
+   * @param $sortstring
+   *  Raw string to set the sort to - optionally use aliased field names.
    */
   function set_solrsort($sortstring);
 
   /**
    * Get the solrsort.
+   *
+   * Returns the non-urlencode, non-aliased sort field and direction
    */
   function get_solrsort();
 
Index: apachesolr_search.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr/apachesolr_search.module,v
retrieving revision 1.1.2.6.2.107
diff -u -p -r1.1.2.6.2.107 apachesolr_search.module
--- apachesolr_search.module	30 Jun 2009 15:10:47 -0000	1.1.2.6.2.107
+++ apachesolr_search.module	30 Jun 2009 19:44:12 -0000
@@ -185,20 +185,14 @@ function apachesolr_search_view($type = 
  * @throws Exception
  */
 function apachesolr_search_execute($keys, $filters, $solrsort, $base_path = '', $page = 0, $caller = 'apachesolr_search') {
-  // Validate sort parameter
-  // TODO: move this to the query class.
-  if (strlen($solrsort) && preg_match('/^([a-z0-9_]+ (asc|desc)(,)?)+$/i', $solrsort)) {
-    $params['sort'] = $solrsort;
-  }
-  else {
-    $solrsort = '';
-    $params = array();
-  }
+
+  $params = array();
   // This is the object that knows about the query coming from the user.
   $query = apachesolr_drupal_query($keys, $filters, $solrsort, $base_path);
   if (empty($query)) {
     throw new Exception(t('Could not construct a Solr query in function apachesolr_search_search()'));
   }
+
   // This is the object that does the communication with the solr server.
   $solr = apachesolr_get_solr();
   $params += apachesolr_search_basic_params($query);
@@ -777,7 +771,7 @@ function apachesolr_search_form_search_f
     $querystring = '';
     if ($apachesolr_has_searched) {
       $query = apachesolr_current_query();
-      $querystring = $query->get_url_querystring();
+      $querystring = implode('&', $query->get_url_querystring());
     }
 
     $form['basic']['apachesolr_search']['#tree'] = TRUE;
@@ -832,7 +826,7 @@ function apachesolr_search_form_search_s
     $keys = str_replace('+', '%2B', $keys);
   }
   if (!empty($fv['apachesolr_search']['retain-filters']) && $fv['apachesolr_search']['querystring']) {
-    $querystring = $fv['apachesolr_search']['querystring'] . '&retain-filters';
+    $querystring = $fv['apachesolr_search']['querystring'] . '&retain-filters=1';
   }
   $form_state['redirect'] = array($base . $keys, $querystring);
   if ($keys == '' && $querystring == '') {
Index: Solr_Base_Query.php
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/apachesolr/Solr_Base_Query.php,v
retrieving revision 1.1.4.38
diff -u -p -r1.1.4.38 Solr_Base_Query.php
--- Solr_Base_Query.php	30 Jun 2009 10:57:23 -0000	1.1.4.38
+++ Solr_Base_Query.php	30 Jun 2009 19:44:12 -0000
@@ -65,14 +65,14 @@ class Solr_Base_Query implements Drupal_
    * used for filter queries, e.g. array('#name' => 'uid', '#value' => 0)
    * for anonymous content.
    */
-  protected $fields;
+  protected $fields = array();
 
   /**
    * The complete filter string for a query.  Usually from $_GET['filters']
    * Contains name:value pairs for filter queries.  For example,
    * "type:book" for book nodes.
    */
-  protected $filterstring;
+  protected $filterstring = '';
 
   /**
    * A mapping of field names from the URL to real index field names.
@@ -87,19 +87,23 @@ class Solr_Base_Query implements Drupal_
   /**
    * The search keywords.
    */
-  protected $keys;
+  protected $keys = '';
 
   /**
    * The search base path.
    */
-  protected $base_path;
+  protected $base_path = '';
 
   /**
    * Apache_Solr_Service object
    */
   protected $solr;
 
-  protected $available_sorts;
+  protected $available_sorts = array();
+
+  protected $solrsort = '';
+
+  protected $extra_queryvalues = array();
 
   /**
    * @param $solr
@@ -123,11 +127,11 @@ class Solr_Base_Query implements Drupal_
     $this->solr = $solr;
     $this->keys = trim($keys);
     $this->filterstring = trim($filterstring);
-    $this->solrsort = trim($sortstring);
-    $this->base_path = $base_path;
-    $this->id = ++self::$idCount;
     $this->parse_filters();
     $this->available_sorts = $this->default_sorts();
+    $this->set_solrsort($sortstring);
+    $this->base_path = $base_path;
+    $this->id = ++self::$idCount;
   }
 
   function __clone() {
@@ -234,7 +238,17 @@ class Solr_Base_Query implements Drupal_
   }
 
   public function set_solrsort($sortstring) {
-    $this->solrsort = trim($sortstring);
+    // Validate sort parameter
+    $fields = implode('|', array_keys($this->available_sorts));
+    // Substitute any field aliases with real field names.
+    $sortstring = strtr(trim($sortstring), $this->field_map);
+    // Score is a special case - it's the default sort.
+    if ('score asc' == $sortstring) {
+      $sortstring = '';
+    }
+    if (('' == $solrsort) || preg_match('/^(('. $fields .') (asc|desc),?)+$/', $solrsort)) {
+      $this->solrsort = $sortstring;
+    }
   }
 
   public function get_solrsort() {
@@ -254,7 +268,7 @@ class Solr_Base_Query implements Drupal_
    */
   protected function default_sorts() {
     return array(
-      'relevancy' => array('name' => t('Relevancy'), 'default' => 'asc'),
+      'score' => array('name' => t('Relevancy'), 'default' => 'asc'),
       'sort_title' => array('name' => t('Title'), 'default' => 'asc'),
       'type' => array('name' => t('Type'), 'default' => 'asc'),
       'sort_name' => array('name' => t('Author'), 'default' => 'asc'),
@@ -266,14 +280,14 @@ class Solr_Base_Query implements Drupal_
    * Return filters and sort in a form suitable for a query param to url().
    */
   public function get_url_querystring() {
-    $querystring = '';
+    $queryvalues = array();
     if ($fq = $this->rebuild_fq(TRUE)) {
-      $querystring = 'filters='. rawurlencode(implode(' ', $fq));
+      $queryvalues['filters'] = implode(' ', $fq);
     }
     if ($this->solrsort) {
-      $querystring .= ($querystring ? '&' : '') .'solrsort='. rawurlencode($this->solrsort);
+      $queryvalues['solrsort'] = strtr($this->solrsort, array_flip($this->field_map));
     }
-    return $querystring;
+    return $queryvalues;
   }
 
   public function get_fq() {
