public function SearchQuery::addScore

7.x search.extender.inc public SearchQuery::addScore($score, $arguments = array(), $multiply = FALSE)

Adds a custom score expression to the search query.

Score expressions are used to order search results. If no calls to addScore() have taken place, a default keyword relevance score will be used. However, if at least one call to addScore() has taken place, the keyword relevance score is not automatically added.

Also note that if you call orderBy() directly on the query, search scores will not automatically be used to order search results. Your orderBy() expression can reference 'calculated_score', which will be the total calculated score value.

Parameters

$score: The score expression, which should evaluate to a number between 0 and 1. The string 'i.relevance' in a score expression will be replaced by a measure of keyword relevance between 0 and 1.

$arguments: Query arguments needed to provide values to the score expression.

$multiply: If set, the score is multiplied with this value. However, all scores with multipliers are then divided by the total of all multipliers, so that overall, the normalization is maintained.

Return value

object The updated query object.

1 call to SearchQuery::addScore()
SearchQuery::execute in drupal-7.x/modules/search/search.extender.inc
Executes the search.

File

drupal-7.x/modules/search/search.extender.inc, line 420
Search query extender and helper functions.

Class

SearchQuery
Do a query on the full-text search index for a word or words.

Code

public function addScore($score, $arguments = array(), $multiply = FALSE) {
  if ($multiply) {
    $i = count($this->multiply);
    // Modify the score expression so it is multiplied by the multiplier,
    // with a divisor to renormalize.
    $score = "CAST(:multiply_$i AS DECIMAL) * COALESCE(( " . $score . "), 0) / CAST(:total_$i AS DECIMAL)";
    // Add an argument for the multiplier. The :total_$i argument is taken
    // care of in the execute() method, which is when the total divisor is
    // calculated.
    $arguments[':multiply_' . $i] = $multiply;
    $this->multiply[] = $multiply;
  }

  $this->scores[] = $score;
  $this->scoresArguments += $arguments;

  return $this;
}