Comments

pwolanin’s picture

Version: 6.x-1.0-rc3 » 6.x-1.x-dev
Category: support » feature

current code does not support multiple sorts - the sort string parsing would need to be fixed up

Agileware’s picture

That's what I expected.

I have approached this multi-domain search requirement a different way now anyway.
Instead I add radio buttons to the search forms for searching all domains or a specific one. Then I just use the default score sort.
Not exactly the same result but works well. Probably even more user friendly.

Multiple sort fields would be a useful addition though at some point.

jcfiala’s picture

I have faked out the sort to allow multiple sort fields like this:

1) I create my own custom override of the Solr_Base_Query.php class. It inherits from Solr_Base_Query, and it's defined in the custom module's install:

function example_solr_service_install() {
  variable_set('apachesolr_query_class', array('example_solr_service', 'Example_Custom_Solr_Query'));
}

2) In the Example_Custom_Solr_Query object, I muck with the values it returns from get_solrsort, like so:

  public function get_solrsort() {
    return array(
      '#name' => $this->solrsort['#name'],
      '#direction' => $this->solrsort['#direction'] .',sort_title asc',
    );
  }

In this case, I'm sorting on the #name and #direction fed to apachesolr_search_execute, but I return a little extra to the direction. I've found that the function that calls get_solrsort() doesn't care what's in these fields or validate them in any way, it just puts them together with a space between them and sends them to the solr engine.

It's a hack, but it works.

(NB: Solr doesn't seem to like spaces around the commas separating the items you're sorting on.)

pwolanin’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev
pwolanin’s picture

Title: Is it possible to sort on multiple fields? » Support sorts on multiple fields
wouters_f’s picture

Maybe It could be usefull to get soem of the best sort solutions in the apachesolr_sort module.
I already added some functionalities here:
http://drupal.org/sandbox/wouters_frederik/1124756

pwolanin’s picture

The recent changes in 7.x to the query class should bring it much closer to handling multiple sorts

Basically, omeone just needs to go and make it handle multiple solrsort[]= params instead of a single string and handle merging too/from the internal params.

Agileware’s picture

The solution in #3 worked for me in D6, thanks for the idea.

ryandekker’s picture

This will get broken by subsequent calls to setSolrsort(), but it does work to get multiple sorts working in 7.x-1.x. It is still a bit clunky, in that $query->sortstring remains blank.


/**
 * Implements hook_apachesolr_query_alter().
 */
function example_apachesolr_query_alter($query) {
  // Use addParam() rather than setSolrsort() for multiple. 
  $query->addParam('sort', 'sort_title asc');
  $query->addParam('sort', 'score desc');

  // Becomes: sort_title asc,score desc
}

Nick_vh’s picture

Status: Active » Fixed

Fields that are set multiple now get indexed as a single to support the sorting functionality. Marking as fixed until reopened

Nick_vh’s picture

Status: Fixed » Closed (fixed)
Anonymous’s picture

Issue summary: View changes

Is there a possibility yet to achieve anything like in #9 (sending something like this to Solr: sort_title asc,score desc) ? Or could someone explain #10 in more detail?