You are here

public function SearchIndex::updateWordWeights in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/search/src/SearchIndex.php \Drupal\search\SearchIndex::updateWordWeights()

Updates the {search_total} database table.

Parameters

array $words: An array whose keys are words from self::index() whose total weights need to be updated.

Throws

\Drupal\search\Exception\SearchIndexException If there is an error updating the totals.

Overrides SearchIndexInterface::updateWordWeights

1 call to SearchIndex::updateWordWeights()
SearchIndex::index in core/modules/search/src/SearchIndex.php
Updates the full-text search index for a particular item.

File

core/modules/search/src/SearchIndex.php, line 295

Class

SearchIndex
Provides search index management functions.

Namespace

Drupal\search

Code

public function updateWordWeights(array $words) {
  try {

    // Update word IDF (Inverse Document Frequency) counts for new/changed
    // words.
    $words = array_keys($words);
    foreach ($words as $word) {

      // Get total count.
      $total = $this->replica
        ->query("SELECT SUM([score]) FROM {search_index} WHERE [word] = :word", [
        ':word' => $word,
      ])
        ->fetchField();

      // Apply Zipf's law to equalize the probability distribution.
      $total = log10(1 + 1 / max(1, $total));
      $this->connection
        ->merge('search_total')
        ->key('word', $word)
        ->fields([
        'count' => $total,
      ])
        ->execute();
    }

    // Find words that were deleted from search_index, but are still in
    // search_total. We use a LEFT JOIN between the two tables and keep only
    // the rows which fail to join.
    $result = $this->replica
      ->query("SELECT [t].[word] AS [realword], [i].[word] FROM {search_total} [t] LEFT JOIN {search_index} [i] ON [t].[word] = [i].[word] WHERE [i].[word] IS NULL");
    $or = $this->replica
      ->condition('OR');
    foreach ($result as $word) {
      $or
        ->condition('word', $word->realword);
    }
    if (count($or) > 0) {
      $this->connection
        ->delete('search_total')
        ->condition($or)
        ->execute();
    }
  } catch (\Exception $e) {
    throw new SearchIndexException("Failed to update totals for index words.", 0, $e);
  }
}