/**
   * Handles aliases for field to make nicer URLs.
   *
   * @param $field_map
   *   An array keyed with aliases with the real Solr index field name as value.
   *
   * @return DrupalSolrQueryInterface
   *   The called object.
   */
  function addFieldAliases($field_map);

According to apachesolr.interfaces.inc, a call to DrupalSolrQueryInterface::addFieldAliases could look like:

$query->addFieldAliases(array('myalias' => 'real_solr_field_name'));

but the code in SolrBaseQuery::getSolrsortUrlQuery() expects the field map to be in the opposite order.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

fizk’s picture

Here's a patch.

fizk’s picture

Status: Active » Needs review
Nick_vh’s picture

Status: Needs review » Needs work

I'm not sure if this is actually a smart move to do an array flip.

I'd have to look deeper in this, could you also make some simpletest proving that the current behaviour is wrong? Maybe updating the documentation is better and we don't suddenly break assumptions?

fizk’s picture

FileSize
531 bytes

Well, we'll be breaking assumptions either way - some think the documentation is right, and others know it isn't.

I agree, changing the documentation is probably going to affect less people. Here's a patch.

fizk’s picture

Status: Needs work » Needs review
Nick_vh’s picture

Status: Needs review » Patch (to be ported)
FileSize
1.2 KB

Adding some more docs, I'm planning to update the whol einterface in #1897556: Update documentation for all the functions and make sure it reflects reality so this already helps. Could you actually clarify for me for what you are trying to implement this and it breaks?

Committed to 7.x1.x, thanks!

Nick_vh’s picture

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

I'm implementing hook_apachesolr_query_prepare() and calling addFieldAliases() so I can sort by field names that are prettier than the default field names.

/**
 * Implements hook_apachesolr_query_prepare().
 */
function msearch_apachesolr_query_prepare($query) {
    // Sort node creation date via "date"
    $query->addFieldAliases(array('date' => 'ds_created'));

    // Sort author username via "username"
    $query->addFieldAliases(array('username' => 'sort_name'));

    // Sort node type via "type"
    $query->addFieldAliases(array('type' => 'bundle'));

    // Sort node title via "title"
    $query->addFieldAliases(array('title' => 'sort_label'));
}

For anyone else reading this, please note the order should be the other way around (eg. $query->addFieldAliases(array('ds_created' => 'date')); is correct).

Nick_vh’s picture

So the drawback of this code working this way is that you can only have 1 alias per field?

pwolanin’s picture

Status: Patch (to be ported) » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.