1

In my module I have this implementation where I have a hook_search_execute() function which can be used for rewriting/extending default Drupal search. This function calls for executeFirstPass() method and adds to the query the following $first->addExpression('SUM(i.score * t.count)', 'calculated_score');

When I'm trying to add my sorting as following $query->orderBy('calculated_score', 'ASC');, I have an error.

However if I add $query->orderBy('n.title', 'ASC'); or $query->orderBy('n.created', 'ASC'); everything is fine and is sorting as it should be.

Does anyone have any ideas why this is happens?

Community
  • 1
  • 1
Vlad Stratulat
  • 1,296
  • 1
  • 10
  • 24
  • Could you post the `PDOException` error you're getting? – Clive Jan 16 '12 at 10:26
  • There's no such error. When I'm logged of I have an `Error. The website encountered an unexpected error. Please try again later.` message and theme isn't loaded so I have only this message and user log in form. If I'm logged in, I don't have any error but site forwarding me to users profile (that's really strange). – Vlad Stratulat Jan 16 '12 at 10:33
  • Does your anonymous user role have permissions to use search? – Clive Jan 16 '12 at 10:36
  • Yes. I've wrote that with `$query->orderBy('n.title', 'ASC');` or `$query->orderBy('n.created', 'ASC');` everything is fine. – Vlad Stratulat Jan 16 '12 at 10:38
  • In [`executeFirstPass()`](http://api.drupal.org/api/drupal/modules--search--search.extender.inc/function/SearchQuery%3A%3AexecuteFirstPass/7) method you can find that this function already add `->orderBy('calculated_score', 'DESC')` method, and maybe when I'm trying to add it again in my `hook_search_execute()` for second time it show this error!? – Vlad Stratulat Jan 16 '12 at 10:41
  • 1
    Once you've run `$find = $query->limit(10)->execute();` check what's in `$query->getQueryString()` and see if you can find any errors in the MySQL that's been produced. That's probably a good place to start – Clive Jan 16 '12 at 10:47

1 Answers1

0

After all my research I came only to this crappy solution.

In modules/search/search.extender.inc file we have the following code in line 437 (depends on Drupal version).

....
// Convert scores to an expression.
$this->addExpression('SUM(' . implode(' + ', $this->scores) . ')', 'calculated_score', $this->scoresArguments);

if (count($this->getOrderBy()) == 0) {
  // Add default order after adding the expression.
  $this->orderBy('calculated_score', 'DESC');
}
....

I turned this code into:

....
// Convert scores to an expression.
$this->addExpression('SUM(' . implode(' + ', $this->scores) . ')', 'calculated_score', $this->scoresArguments);

if (count($this->getOrderBy()) == 0) {
  if ($_GET['orderby'] == 'relevance' && $_GET['orderdir'] == 'ASC') {
    $dir = 'ASC';
  }
  else {
    $dir = 'DESC';
  }
  // Add default order after adding the expression.
  $this->orderBy('calculated_score', $dir);
}
....

Please feel free to propose clean solution.

Vlad Stratulat
  • 1,296
  • 1
  • 10
  • 24