annotate core/modules/search/src/SearchQuery.php @ 1:1a348b17ec81

Logo and header background
author Chris Cannam
date Thu, 30 Nov 2017 14:56:35 +0000
parents 4c8ae668cc8c
children 7a779792577d
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\search;
Chris@0 4
Chris@0 5 use Drupal\Core\Database\Query\Condition;
Chris@0 6 use Drupal\Component\Utility\Unicode;
Chris@0 7 use Drupal\Core\Database\Query\SelectExtender;
Chris@0 8 use Drupal\Core\Database\Query\SelectInterface;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Search query extender and helper functions.
Chris@0 12 *
Chris@0 13 * Performs a query on the full-text search index for a word or words.
Chris@0 14 *
Chris@0 15 * This query is used by search plugins that use the search index (not all
Chris@0 16 * search plugins do, as some use a different searching mechanism). It
Chris@0 17 * assumes you have set up a query on the {search_index} table with alias 'i',
Chris@0 18 * and will only work if the user is searching for at least one "positive"
Chris@0 19 * keyword or phrase.
Chris@0 20 *
Chris@0 21 * For efficiency, users of this query can run the prepareAndNormalize()
Chris@0 22 * method to figure out if there are any search results, before fully setting
Chris@0 23 * up and calling execute() to execute the query. The scoring expressions are
Chris@0 24 * not needed until the execute() step. However, it's not really necessary
Chris@0 25 * to do this, because this class's execute() method does that anyway.
Chris@0 26 *
Chris@0 27 * During both the prepareAndNormalize() and execute() steps, there can be
Chris@0 28 * problems. Call getStatus() to figure out if the query is OK or not.
Chris@0 29 *
Chris@0 30 * The query object is given the tag 'search_$type' and can be further
Chris@0 31 * extended with hook_query_alter().
Chris@0 32 */
Chris@0 33 class SearchQuery extends SelectExtender {
Chris@0 34
Chris@0 35 /**
Chris@0 36 * Indicates no positive keywords were in the search expression.
Chris@0 37 *
Chris@0 38 * Positive keywords are words that are searched for, as opposed to negative
Chris@0 39 * keywords, which are words that are excluded. To count as a keyword, a
Chris@0 40 * word must be at least
Chris@0 41 * \Drupal::config('search.settings')->get('index.minimum_word_size')
Chris@0 42 * characters.
Chris@0 43 *
Chris@0 44 * @see SearchQuery::getStatus()
Chris@0 45 */
Chris@0 46 const NO_POSITIVE_KEYWORDS = 1;
Chris@0 47
Chris@0 48 /**
Chris@0 49 * Indicates that part of the search expression was ignored.
Chris@0 50 *
Chris@0 51 * To prevent Denial of Service attacks, only
Chris@0 52 * \Drupal::config('search.settings')->get('and_or_limit') expressions
Chris@0 53 * (positive keywords, phrases, negative keywords) are allowed; this flag
Chris@0 54 * indicates that expressions existed past that limit and they were removed.
Chris@0 55 *
Chris@0 56 * @see SearchQuery::getStatus()
Chris@0 57 */
Chris@0 58 const EXPRESSIONS_IGNORED = 2;
Chris@0 59
Chris@0 60 /**
Chris@0 61 * Indicates that lower-case "or" was in the search expression.
Chris@0 62 *
Chris@0 63 * The word "or" in lower case was found in the search expression. This
Chris@0 64 * probably means someone was trying to do an OR search but used lower-case
Chris@0 65 * instead of upper-case.
Chris@0 66 *
Chris@0 67 * @see SearchQuery::getStatus()
Chris@0 68 */
Chris@0 69 const LOWER_CASE_OR = 4;
Chris@0 70
Chris@0 71 /**
Chris@0 72 * Indicates that no positive keyword matches were found.
Chris@0 73 *
Chris@0 74 * @see SearchQuery::getStatus()
Chris@0 75 */
Chris@0 76 const NO_KEYWORD_MATCHES = 8;
Chris@0 77
Chris@0 78 /**
Chris@0 79 * The keywords and advanced search options that are entered by the user.
Chris@0 80 *
Chris@0 81 * @var string
Chris@0 82 */
Chris@0 83 protected $searchExpression;
Chris@0 84
Chris@0 85 /**
Chris@0 86 * The type of search (search type).
Chris@0 87 *
Chris@0 88 * This maps to the value of the type column in search_index, and is usually
Chris@0 89 * equal to the machine-readable name of the plugin or the search page.
Chris@0 90 *
Chris@0 91 * @var string
Chris@0 92 */
Chris@0 93 protected $type;
Chris@0 94
Chris@0 95 /**
Chris@0 96 * Parsed-out positive and negative search keys.
Chris@0 97 *
Chris@0 98 * @var array
Chris@0 99 */
Chris@0 100 protected $keys = ['positive' => [], 'negative' => []];
Chris@0 101
Chris@0 102 /**
Chris@0 103 * Indicates whether the query conditions are simple or complex (LIKE).
Chris@0 104 *
Chris@0 105 * @var bool
Chris@0 106 */
Chris@0 107 protected $simple = TRUE;
Chris@0 108
Chris@0 109 /**
Chris@0 110 * Conditions that are used for exact searches.
Chris@0 111 *
Chris@0 112 * This is always used for the second step in the query, but is not part of
Chris@0 113 * the preparation step unless $this->simple is FALSE.
Chris@0 114 *
Chris@0 115 * @var DatabaseCondition
Chris@0 116 */
Chris@0 117 protected $conditions;
Chris@0 118
Chris@0 119 /**
Chris@0 120 * Indicates how many matches for a search query are necessary.
Chris@0 121 *
Chris@0 122 * @var int
Chris@0 123 */
Chris@0 124 protected $matches = 0;
Chris@0 125
Chris@0 126 /**
Chris@0 127 * Array of positive search words.
Chris@0 128 *
Chris@0 129 * These words have to match against {search_index}.word.
Chris@0 130 *
Chris@0 131 * @var array
Chris@0 132 */
Chris@0 133 protected $words = [];
Chris@0 134
Chris@0 135 /**
Chris@0 136 * Multiplier to normalize the keyword score.
Chris@0 137 *
Chris@0 138 * This value is calculated by the preparation step, and is used as a
Chris@0 139 * multiplier of the word scores to make sure they are between 0 and 1.
Chris@0 140 *
Chris@0 141 * @var float
Chris@0 142 */
Chris@0 143 protected $normalize = 0;
Chris@0 144
Chris@0 145 /**
Chris@0 146 * Indicates whether the preparation step has been executed.
Chris@0 147 *
Chris@0 148 * @var bool
Chris@0 149 */
Chris@0 150 protected $executedPrepare = FALSE;
Chris@0 151
Chris@0 152 /**
Chris@0 153 * A bitmap of status conditions, described in getStatus().
Chris@0 154 *
Chris@0 155 * @var int
Chris@0 156 *
Chris@0 157 * @see SearchQuery::getStatus()
Chris@0 158 */
Chris@0 159 protected $status = 0;
Chris@0 160
Chris@0 161 /**
Chris@0 162 * The word score expressions.
Chris@0 163 *
Chris@0 164 * @var array
Chris@0 165 *
Chris@0 166 * @see SearchQuery::addScore()
Chris@0 167 */
Chris@0 168 protected $scores = [];
Chris@0 169
Chris@0 170 /**
Chris@0 171 * Arguments for the score expressions.
Chris@0 172 *
Chris@0 173 * @var array
Chris@0 174 */
Chris@0 175 protected $scoresArguments = [];
Chris@0 176
Chris@0 177 /**
Chris@0 178 * The number of 'i.relevance' occurrences in score expressions.
Chris@0 179 *
Chris@0 180 * @var int
Chris@0 181 */
Chris@0 182 protected $relevance_count = 0;
Chris@0 183
Chris@0 184 /**
Chris@0 185 * Multipliers for score expressions.
Chris@0 186 *
Chris@0 187 * @var array
Chris@0 188 */
Chris@0 189 protected $multiply = [];
Chris@0 190
Chris@0 191 /**
Chris@0 192 * Sets the search query expression.
Chris@0 193 *
Chris@0 194 * @param string $expression
Chris@0 195 * A search string, which can contain keywords and options.
Chris@0 196 * @param string $type
Chris@0 197 * The search type. This maps to {search_index}.type in the database.
Chris@0 198 *
Chris@0 199 * @return $this
Chris@0 200 */
Chris@0 201 public function searchExpression($expression, $type) {
Chris@0 202 $this->searchExpression = $expression;
Chris@0 203 $this->type = $type;
Chris@0 204
Chris@0 205 // Add query tag.
Chris@0 206 $this->addTag('search_' . $type);
Chris@0 207
Chris@0 208 // Initialize conditions and status.
Chris@0 209 $this->conditions = new Condition('AND');
Chris@0 210 $this->status = 0;
Chris@0 211
Chris@0 212 return $this;
Chris@0 213 }
Chris@0 214
Chris@0 215 /**
Chris@0 216 * Parses the search query into SQL conditions.
Chris@0 217 *
Chris@0 218 * Sets up the following variables:
Chris@0 219 * - $this->keys
Chris@0 220 * - $this->words
Chris@0 221 * - $this->conditions
Chris@0 222 * - $this->simple
Chris@0 223 * - $this->matches
Chris@0 224 */
Chris@0 225 protected function parseSearchExpression() {
Chris@0 226 // Matches words optionally prefixed by a - sign. A word in this case is
Chris@0 227 // something between two spaces, optionally quoted.
Chris@0 228 preg_match_all('/ (-?)("[^"]+"|[^" ]+)/i', ' ' . $this->searchExpression, $keywords, PREG_SET_ORDER);
Chris@0 229
Chris@0 230 if (count($keywords) == 0) {
Chris@0 231 return;
Chris@0 232 }
Chris@0 233
Chris@0 234 // Classify tokens.
Chris@0 235 $in_or = FALSE;
Chris@0 236 $limit_combinations = \Drupal::config('search.settings')->get('and_or_limit');
Chris@0 237 // The first search expression does not count as AND.
Chris@0 238 $and_count = -1;
Chris@0 239 $or_count = 0;
Chris@0 240 foreach ($keywords as $match) {
Chris@0 241 if ($or_count && $and_count + $or_count >= $limit_combinations) {
Chris@0 242 // Ignore all further search expressions to prevent Denial-of-Service
Chris@0 243 // attacks using a high number of AND/OR combinations.
Chris@0 244 $this->status |= SearchQuery::EXPRESSIONS_IGNORED;
Chris@0 245 break;
Chris@0 246 }
Chris@0 247
Chris@0 248 // Strip off phrase quotes.
Chris@0 249 $phrase = FALSE;
Chris@0 250 if ($match[2]{0} == '"') {
Chris@0 251 $match[2] = substr($match[2], 1, -1);
Chris@0 252 $phrase = TRUE;
Chris@0 253 $this->simple = FALSE;
Chris@0 254 }
Chris@0 255
Chris@0 256 // Simplify keyword according to indexing rules and external
Chris@0 257 // preprocessors. Use same process as during search indexing, so it
Chris@0 258 // will match search index.
Chris@0 259 $words = search_simplify($match[2]);
Chris@0 260 // Re-explode in case simplification added more words, except when
Chris@0 261 // matching a phrase.
Chris@0 262 $words = $phrase ? [$words] : preg_split('/ /', $words, -1, PREG_SPLIT_NO_EMPTY);
Chris@0 263 // Negative matches.
Chris@0 264 if ($match[1] == '-') {
Chris@0 265 $this->keys['negative'] = array_merge($this->keys['negative'], $words);
Chris@0 266 }
Chris@0 267 // OR operator: instead of a single keyword, we store an array of all
Chris@0 268 // OR'd keywords.
Chris@0 269 elseif ($match[2] == 'OR' && count($this->keys['positive'])) {
Chris@0 270 $last = array_pop($this->keys['positive']);
Chris@0 271 // Starting a new OR?
Chris@0 272 if (!is_array($last)) {
Chris@0 273 $last = [$last];
Chris@0 274 }
Chris@0 275 $this->keys['positive'][] = $last;
Chris@0 276 $in_or = TRUE;
Chris@0 277 $or_count++;
Chris@0 278 continue;
Chris@0 279 }
Chris@0 280 // AND operator: implied, so just ignore it.
Chris@0 281 elseif ($match[2] == 'AND' || $match[2] == 'and') {
Chris@0 282 continue;
Chris@0 283 }
Chris@0 284
Chris@0 285 // Plain keyword.
Chris@0 286 else {
Chris@0 287 if ($match[2] == 'or') {
Chris@0 288 // Lower-case "or" instead of "OR" is a warning condition.
Chris@0 289 $this->status |= SearchQuery::LOWER_CASE_OR;
Chris@0 290 }
Chris@0 291 if ($in_or) {
Chris@0 292 // Add to last element (which is an array).
Chris@0 293 $this->keys['positive'][count($this->keys['positive']) - 1] = array_merge($this->keys['positive'][count($this->keys['positive']) - 1], $words);
Chris@0 294 }
Chris@0 295 else {
Chris@0 296 $this->keys['positive'] = array_merge($this->keys['positive'], $words);
Chris@0 297 $and_count++;
Chris@0 298 }
Chris@0 299 }
Chris@0 300 $in_or = FALSE;
Chris@0 301 }
Chris@0 302
Chris@0 303 // Convert keywords into SQL statements.
Chris@0 304 $has_and = FALSE;
Chris@0 305 $has_or = FALSE;
Chris@0 306 // Positive matches.
Chris@0 307 foreach ($this->keys['positive'] as $key) {
Chris@0 308 // Group of ORed terms.
Chris@0 309 if (is_array($key) && count($key)) {
Chris@0 310 // If we had already found one OR, this is another one AND-ed with the
Chris@0 311 // first, meaning it is not a simple query.
Chris@0 312 if ($has_or) {
Chris@0 313 $this->simple = FALSE;
Chris@0 314 }
Chris@0 315 $has_or = TRUE;
Chris@0 316 $has_new_scores = FALSE;
Chris@0 317 $queryor = new Condition('OR');
Chris@0 318 foreach ($key as $or) {
Chris@0 319 list($num_new_scores) = $this->parseWord($or);
Chris@0 320 $has_new_scores |= $num_new_scores;
Chris@0 321 $queryor->condition('d.data', "% $or %", 'LIKE');
Chris@0 322 }
Chris@0 323 if (count($queryor)) {
Chris@0 324 $this->conditions->condition($queryor);
Chris@0 325 // A group of OR keywords only needs to match once.
Chris@0 326 $this->matches += ($has_new_scores > 0);
Chris@0 327 }
Chris@0 328 }
Chris@0 329 // Single ANDed term.
Chris@0 330 else {
Chris@0 331 $has_and = TRUE;
Chris@0 332 list($num_new_scores, $num_valid_words) = $this->parseWord($key);
Chris@0 333 $this->conditions->condition('d.data', "% $key %", 'LIKE');
Chris@0 334 if (!$num_valid_words) {
Chris@0 335 $this->simple = FALSE;
Chris@0 336 }
Chris@0 337 // Each AND keyword needs to match at least once.
Chris@0 338 $this->matches += $num_new_scores;
Chris@0 339 }
Chris@0 340 }
Chris@0 341 if ($has_and && $has_or) {
Chris@0 342 $this->simple = FALSE;
Chris@0 343 }
Chris@0 344
Chris@0 345 // Negative matches.
Chris@0 346 foreach ($this->keys['negative'] as $key) {
Chris@0 347 $this->conditions->condition('d.data', "% $key %", 'NOT LIKE');
Chris@0 348 $this->simple = FALSE;
Chris@0 349 }
Chris@0 350 }
Chris@0 351
Chris@0 352 /**
Chris@0 353 * Parses a word or phrase for parseQuery().
Chris@0 354 *
Chris@0 355 * Splits a phrase into words. Adds its words to $this->words, if it is not
Chris@0 356 * already there. Returns a list containing the number of new words found,
Chris@0 357 * and the total number of words in the phrase.
Chris@0 358 */
Chris@0 359 protected function parseWord($word) {
Chris@0 360 $num_new_scores = 0;
Chris@0 361 $num_valid_words = 0;
Chris@0 362
Chris@0 363 // Determine the scorewords of this word/phrase.
Chris@0 364 $split = explode(' ', $word);
Chris@0 365 foreach ($split as $s) {
Chris@0 366 $num = is_numeric($s);
Chris@0 367 if ($num || Unicode::strlen($s) >= \Drupal::config('search.settings')->get('index.minimum_word_size')) {
Chris@0 368 if (!isset($this->words[$s])) {
Chris@0 369 $this->words[$s] = $s;
Chris@0 370 $num_new_scores++;
Chris@0 371 }
Chris@0 372 $num_valid_words++;
Chris@0 373 }
Chris@0 374 }
Chris@0 375
Chris@0 376 // Return matching snippet and number of added words.
Chris@0 377 return [$num_new_scores, $num_valid_words];
Chris@0 378 }
Chris@0 379
Chris@0 380 /**
Chris@0 381 * Prepares the query and calculates the normalization factor.
Chris@0 382 *
Chris@0 383 * After the query is normalized the keywords are weighted to give the results
Chris@0 384 * a relevancy score. The query is ready for execution after this.
Chris@0 385 *
Chris@0 386 * Error and warning conditions can apply. Call getStatus() after calling
Chris@0 387 * this method to retrieve them.
Chris@0 388 *
Chris@0 389 * @return bool
Chris@0 390 * TRUE if at least one keyword matched the search index; FALSE if not.
Chris@0 391 */
Chris@0 392 public function prepareAndNormalize() {
Chris@0 393 $this->parseSearchExpression();
Chris@0 394 $this->executedPrepare = TRUE;
Chris@0 395
Chris@0 396 if (count($this->words) == 0) {
Chris@0 397 // Although the query could proceed, there is no point in joining
Chris@0 398 // with other tables and attempting to normalize if there are no
Chris@0 399 // keywords present.
Chris@0 400 $this->status |= SearchQuery::NO_POSITIVE_KEYWORDS;
Chris@0 401 return FALSE;
Chris@0 402 }
Chris@0 403
Chris@0 404 // Build the basic search query: match the entered keywords.
Chris@0 405 $or = new Condition('OR');
Chris@0 406 foreach ($this->words as $word) {
Chris@0 407 $or->condition('i.word', $word);
Chris@0 408 }
Chris@0 409 $this->condition($or);
Chris@0 410
Chris@0 411 // Add keyword normalization information to the query.
Chris@0 412 $this->join('search_total', 't', 'i.word = t.word');
Chris@0 413 $this
Chris@0 414 ->condition('i.type', $this->type)
Chris@0 415 ->groupBy('i.type')
Chris@0 416 ->groupBy('i.sid');
Chris@0 417
Chris@0 418 // If the query is simple, we should have calculated the number of
Chris@0 419 // matching words we need to find, so impose that criterion. For non-
Chris@0 420 // simple queries, this condition could lead to incorrectly deciding not
Chris@0 421 // to continue with the full query.
Chris@0 422 if ($this->simple) {
Chris@0 423 $this->having('COUNT(*) >= :matches', [':matches' => $this->matches]);
Chris@0 424 }
Chris@0 425
Chris@0 426 // Clone the query object to calculate normalization.
Chris@0 427 $normalize_query = clone $this->query;
Chris@0 428
Chris@0 429 // For complex search queries, add the LIKE conditions; if the query is
Chris@0 430 // simple, we do not need them for normalization.
Chris@0 431 if (!$this->simple) {
Chris@0 432 $normalize_query->join('search_dataset', 'd', 'i.sid = d.sid AND i.type = d.type AND i.langcode = d.langcode');
Chris@0 433 if (count($this->conditions)) {
Chris@0 434 $normalize_query->condition($this->conditions);
Chris@0 435 }
Chris@0 436 }
Chris@0 437
Chris@0 438 // Calculate normalization, which is the max of all the search scores for
Chris@0 439 // positive keywords in the query. And note that the query could have other
Chris@0 440 // fields added to it by the user of this extension.
Chris@0 441 $normalize_query->addExpression('SUM(i.score * t.count)', 'calculated_score');
Chris@0 442 $result = $normalize_query
Chris@0 443 ->range(0, 1)
Chris@0 444 ->orderBy('calculated_score', 'DESC')
Chris@0 445 ->execute()
Chris@0 446 ->fetchObject();
Chris@0 447 if (isset($result->calculated_score)) {
Chris@0 448 $this->normalize = (float) $result->calculated_score;
Chris@0 449 }
Chris@0 450
Chris@0 451 if ($this->normalize) {
Chris@0 452 return TRUE;
Chris@0 453 }
Chris@0 454
Chris@0 455 // If the normalization value was zero, that indicates there were no
Chris@0 456 // matches to the supplied positive keywords.
Chris@0 457 $this->status |= SearchQuery::NO_KEYWORD_MATCHES;
Chris@0 458 return FALSE;
Chris@0 459 }
Chris@0 460
Chris@0 461 /**
Chris@0 462 * {@inheritdoc}
Chris@0 463 */
Chris@0 464 public function preExecute(SelectInterface $query = NULL) {
Chris@0 465 if (!$this->executedPrepare) {
Chris@0 466 $this->prepareAndNormalize();
Chris@0 467 }
Chris@0 468
Chris@0 469 if (!$this->normalize) {
Chris@0 470 return FALSE;
Chris@0 471 }
Chris@0 472
Chris@0 473 return parent::preExecute($query);
Chris@0 474 }
Chris@0 475
Chris@0 476 /**
Chris@0 477 * Adds a custom score expression to the search query.
Chris@0 478 *
Chris@0 479 * Score expressions are used to order search results. If no calls to
Chris@0 480 * addScore() have taken place, a default keyword relevance score will be
Chris@0 481 * used. However, if at least one call to addScore() has taken place, the
Chris@0 482 * keyword relevance score is not automatically added.
Chris@0 483 *
Chris@0 484 * Note that you must use this method to add ordering to your searches, and
Chris@0 485 * not call orderBy() directly, when using the SearchQuery extender. This is
Chris@0 486 * because of the two-pass system the SearchQuery class uses to normalize
Chris@0 487 * scores.
Chris@0 488 *
Chris@0 489 * @param string $score
Chris@0 490 * The score expression, which should evaluate to a number between 0 and 1.
Chris@0 491 * The string 'i.relevance' in a score expression will be replaced by a
Chris@0 492 * measure of keyword relevance between 0 and 1.
Chris@0 493 * @param array $arguments
Chris@0 494 * Query arguments needed to provide values to the score expression.
Chris@0 495 * @param float $multiply
Chris@0 496 * If set, the score is multiplied with this value. However, all scores
Chris@0 497 * with multipliers are then divided by the total of all multipliers, so
Chris@0 498 * that overall, the normalization is maintained.
Chris@0 499 *
Chris@0 500 * @return $this
Chris@0 501 */
Chris@0 502 public function addScore($score, $arguments = [], $multiply = FALSE) {
Chris@0 503 if ($multiply) {
Chris@0 504 $i = count($this->multiply);
Chris@0 505 // Modify the score expression so it is multiplied by the multiplier,
Chris@0 506 // with a divisor to renormalize. Note that the ROUND here is necessary
Chris@0 507 // for PostgreSQL and SQLite in order to ensure that the :multiply_* and
Chris@0 508 // :total_* arguments are treated as a numeric type, because the
Chris@0 509 // PostgreSQL PDO driver sometimes puts values in as strings instead of
Chris@0 510 // numbers in complex expressions like this.
Chris@0 511 $score = "(ROUND(:multiply_$i, 4)) * COALESCE(($score), 0) / (ROUND(:total_$i, 4))";
Chris@0 512 // Add an argument for the multiplier. The :total_$i argument is taken
Chris@0 513 // care of in the execute() method, which is when the total divisor is
Chris@0 514 // calculated.
Chris@0 515 $arguments[':multiply_' . $i] = $multiply;
Chris@0 516 $this->multiply[] = $multiply;
Chris@0 517 }
Chris@0 518
Chris@0 519 // Search scoring needs a way to include a keyword relevance in the score.
Chris@0 520 // For historical reasons, this is done by putting 'i.relevance' into the
Chris@0 521 // search expression. So, use string replacement to change this to a
Chris@0 522 // calculated query expression, counting the number of occurrences so
Chris@0 523 // in the execute() method we can add arguments.
Chris@0 524 while (($pos = strpos($score, 'i.relevance')) !== FALSE) {
Chris@0 525 $pieces = explode('i.relevance', $score, 2);
Chris@0 526 $score = implode('((ROUND(:normalization_' . $this->relevance_count . ', 4)) * i.score * t.count)', $pieces);
Chris@0 527 $this->relevance_count++;
Chris@0 528 }
Chris@0 529
Chris@0 530 $this->scores[] = $score;
Chris@0 531 $this->scoresArguments += $arguments;
Chris@0 532
Chris@0 533 return $this;
Chris@0 534 }
Chris@0 535
Chris@0 536 /**
Chris@0 537 * Executes the search.
Chris@0 538 *
Chris@0 539 * The complex conditions are applied to the query including score
Chris@0 540 * expressions and ordering.
Chris@0 541 *
Chris@0 542 * Error and warning conditions can apply. Call getStatus() after calling
Chris@0 543 * this method to retrieve them.
Chris@0 544 *
Chris@0 545 * @return \Drupal\Core\Database\StatementInterface|null
Chris@0 546 * A query result set containing the results of the query.
Chris@0 547 */
Chris@0 548 public function execute() {
Chris@0 549 if (!$this->preExecute($this)) {
Chris@0 550 return NULL;
Chris@0 551 }
Chris@0 552
Chris@0 553 // Add conditions to the query.
Chris@0 554 $this->join('search_dataset', 'd', 'i.sid = d.sid AND i.type = d.type AND i.langcode = d.langcode');
Chris@0 555 if (count($this->conditions)) {
Chris@0 556 $this->condition($this->conditions);
Chris@0 557 }
Chris@0 558
Chris@0 559 // Add default score (keyword relevance) if there are not any defined.
Chris@0 560 if (empty($this->scores)) {
Chris@0 561 $this->addScore('i.relevance');
Chris@0 562 }
Chris@0 563
Chris@0 564 if (count($this->multiply)) {
Chris@0 565 // Re-normalize scores with multipliers by dividing by the total of all
Chris@0 566 // multipliers. The expressions were altered in addScore(), so here just
Chris@0 567 // add the arguments for the total.
Chris@0 568 $sum = array_sum($this->multiply);
Chris@0 569 for ($i = 0; $i < count($this->multiply); $i++) {
Chris@0 570 $this->scoresArguments[':total_' . $i] = $sum;
Chris@0 571 }
Chris@0 572 }
Chris@0 573
Chris@0 574
Chris@0 575 // Add arguments for the keyword relevance normalization number.
Chris@0 576 $normalization = 1.0 / $this->normalize;
Chris@0 577 for ($i = 0; $i < $this->relevance_count; $i++) {
Chris@0 578 $this->scoresArguments[':normalization_' . $i] = $normalization;
Chris@0 579 }
Chris@0 580
Chris@0 581 // Add all scores together to form a query field.
Chris@0 582 $this->addExpression('SUM(' . implode(' + ', $this->scores) . ')', 'calculated_score', $this->scoresArguments);
Chris@0 583
Chris@0 584 // If an order has not yet been set for this query, add a default order
Chris@0 585 // that sorts by the calculated sum of scores.
Chris@0 586 if (count($this->getOrderBy()) == 0) {
Chris@0 587 $this->orderBy('calculated_score', 'DESC');
Chris@0 588 }
Chris@0 589
Chris@0 590 // Add query metadata.
Chris@0 591 $this
Chris@0 592 ->addMetaData('normalize', $this->normalize)
Chris@0 593 ->fields('i', ['type', 'sid']);
Chris@0 594 return $this->query->execute();
Chris@0 595 }
Chris@0 596
Chris@0 597 /**
Chris@0 598 * Builds the default count query for SearchQuery.
Chris@0 599 *
Chris@0 600 * Since SearchQuery always uses GROUP BY, we can default to a subquery. We
Chris@0 601 * also add the same conditions as execute() because countQuery() is called
Chris@0 602 * first.
Chris@0 603 */
Chris@0 604 public function countQuery() {
Chris@0 605 if (!$this->executedPrepare) {
Chris@0 606 $this->prepareAndNormalize();
Chris@0 607 }
Chris@0 608
Chris@0 609 // Clone the inner query.
Chris@0 610 $inner = clone $this->query;
Chris@0 611
Chris@0 612 // Add conditions to query.
Chris@0 613 $inner->join('search_dataset', 'd', 'i.sid = d.sid AND i.type = d.type');
Chris@0 614 if (count($this->conditions)) {
Chris@0 615 $inner->condition($this->conditions);
Chris@0 616 }
Chris@0 617
Chris@0 618 // Remove existing fields and expressions, they are not needed for a count
Chris@0 619 // query.
Chris@0 620 $fields =& $inner->getFields();
Chris@0 621 $fields = [];
Chris@0 622 $expressions =& $inner->getExpressions();
Chris@0 623 $expressions = [];
Chris@0 624
Chris@0 625 // Add sid as the only field and count them as a subquery.
Chris@0 626 $count = db_select($inner->fields('i', ['sid']), NULL, ['target' => 'replica']);
Chris@0 627
Chris@0 628 // Add the COUNT() expression.
Chris@0 629 $count->addExpression('COUNT(*)');
Chris@0 630
Chris@0 631 return $count;
Chris@0 632 }
Chris@0 633
Chris@0 634 /**
Chris@0 635 * Returns the query status bitmap.
Chris@0 636 *
Chris@0 637 * @return int
Chris@0 638 * A bitmap indicating query status. Zero indicates there were no problems.
Chris@0 639 * A non-zero value is a combination of one or more of the following flags:
Chris@0 640 * - SearchQuery::NO_POSITIVE_KEYWORDS
Chris@0 641 * - SearchQuery::EXPRESSIONS_IGNORED
Chris@0 642 * - SearchQuery::LOWER_CASE_OR
Chris@0 643 * - SearchQuery::NO_KEYWORD_MATCHES
Chris@0 644 */
Chris@0 645 public function getStatus() {
Chris@0 646 return $this->status;
Chris@0 647 }
Chris@0 648
Chris@0 649 }