You are here

function search_update_totals in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/search/search.module \search_update_totals()

Updates the {search_total} database table.

This function is called on shutdown to ensure that {search_total} is always up to date (even if cron times out or otherwise fails).

19 calls to search_update_totals()
SearchAdvancedSearchFormTest::setUp in core/modules/search/src/Tests/SearchAdvancedSearchFormTest.php
Sets up a Drupal site for running functional and integration tests.
SearchCommentCountToggleTest::setUp in core/modules/search/src/Tests/SearchCommentCountToggleTest.php
Sets up a Drupal site for running functional and integration tests.
SearchConfigSettingsFormTest::setUp in core/modules/search/src/Tests/SearchConfigSettingsFormTest.php
Sets up a Drupal site for running functional and integration tests.
SearchEmbedFormTest::setUp in core/modules/search/src/Tests/SearchEmbedFormTest.php
Sets up a Drupal site for running functional and integration tests.
SearchExactTest::testExactQuery in core/modules/search/src/Tests/SearchExactTest.php
Tests that the correct number of pager links are found for both keywords and phrases.

... See full list

1 string reference to 'search_update_totals'
search_cron in core/modules/search/search.module
Implements hook_cron().

File

core/modules/search/search.module, line 205
Enables site-wide keyword searching.

Code

function search_update_totals() {

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

    // Get total count
    $total = db_query("SELECT SUM(score) FROM {search_index} WHERE word = :word", array(
      ':word' => $word,
    ), array(
      'target' => 'replica',
    ))
      ->fetchField();

    // Apply Zipf's law to equalize the probability distribution.
    $total = log10(1 + 1 / max(1, $total));
    db_merge('search_total')
      ->key('word', $word)
      ->fields(array(
      '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 = db_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", array(), array(
    'target' => 'replica',
  ));
  $or = db_or();
  foreach ($result as $word) {
    $or
      ->condition('word', $word->realword);
  }
  if (count($or) > 0) {
    db_delete('search_total')
      ->condition($or)
      ->execute();
  }
}