function hook_update_index

7.x search.api.php hook_update_index()
5.x core.php hook_update_index()
6.x core.php hook_update_index()

Update the search index for this module.

This hook is called every cron run if search.module is enabled, your module has implemented hook_search_info(), and your module has been set as an active search module on the Search settings page (admin/config/search/settings). It allows your module to add items to the built-in search index using search_index(), or to add them to your module's own indexing mechanism.

When implementing this hook, your module should index content items that were modified or added since the last run. PHP has a time limit for cron, though, so it is advisable to limit how many items you index per run using variable_get('search_cron_limit') (see example below). Also, since the cron run could time out and abort in the middle of your run, you should update your module's internal bookkeeping on when items have last been indexed as you go rather than waiting to the end of indexing.

Related topics

5 functions implement hook_update_index()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

comment_node_update_index in drupal/modules/comment/comment.module
Implements hook_node_update_index().
comment_update_index in drupal/modules/comment/comment.module
Implements hook_update_index().
node_update_index in drupal/modules/node/node.module
Implements hook_update_index().
search_node_update_index in drupal/modules/search/search.module
Implements hook_node_update_index().
statistics_update_index in drupal/modules/statistics/statistics.module
Implements hook_update_index().
7 invocations of hook_update_index()
SearchExactTestCase::testExactQuery in drupal/modules/search/search.test
Tests that the correct number of pager links are found for both keywords and phrases.
SearchNodeAccessTest::testPhraseSearchPunctuation in drupal/modules/search/search.test
Tests that search works with punctuation and HTML entities.
SearchNodeTagTest::testNodeSearchQueryTags in drupal/modules/search/search.test
Tests that the correct tags are available and hooks invoked.
SearchRankingTestCase::testDoubleRankings in drupal/modules/search/search.test
Verifies that if we combine two rankings, search still works.
SearchRankingTestCase::testHTMLRankings in drupal/modules/search/search.test
Test rankings of HTML tags.

... See full list


drupal/modules/search/search.api.php, line 312
Hooks provided by the Search module.


function hook_update_index() {
  $limit = (int) variable_get('search_cron_limit', 100);

  $result = db_query_range("SELECT n.nid FROM {node} n LEFT JOIN {search_dataset} d ON d.type = 'node' AND d.sid = n.nid WHERE d.sid IS NULL OR d.reindex <> 0 ORDER BY d.reindex ASC, n.nid ASC", 0, $limit);

  foreach ($result as $node) {
    $node = node_load($node->nid);

    // Save the changed time of the most recent indexed node, for the search
    // results half-life calculation.
    variable_set('node_cron_last', $node->changed);

    // Render the node.
    node_build_content($node, 'search_index');
    $node->rendered = drupal_render($node->content);

    $text = '<h1>' . check_plain($node->title) . '</h1>' . $node->rendered;

    // Fetch extra data normally not visible
    $extra = module_invoke_all('node_update_index', $node);
    foreach ($extra as $t) {
      $text .= $t;

    // Update index
    search_index($node->nid, 'node', $text);