Same name and namespace in other branches
  1. 4.6.x developer/hooks/core.php \hook_update_index()
  2. 4.7.x developer/hooks/core.php \hook_update_index()
  3. 5.x developer/hooks/core.php \hook_update_index()
  4. 7.x modules/search/search.api.php \hook_update_index()

Update Drupal's full-text index for this module.

Modules can implement this hook if they want to use the full-text indexing mechanism in Drupal.

This hook is called every cron run if search.module is enabled. A module should check which of its items were modified or added since the last run. It is advised that you implement a throttling mechanism which indexes at most 'search_cron_limit' items per run (see example below).

You should also be aware that indexing may take too long and be aborted if there is a PHP time limit. That's why you should update your internal bookkeeping multiple times per run, preferably after every item that is indexed.

Per item that needs to be indexed, you should call search_index() with its content as a single HTML string. The search indexer will analyse the HTML and use it to assign higher weights to important words (such as titles). It will also check for links that point to nodes, and use them to boost the ranking of the target nodes.

Related topics

2 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.

node_update_index in modules/node/node.module
Implementation of hook_update_index().
taxonomy_node_update_index in modules/taxonomy/taxonomy.module
Implementation of hook_nodeapi('update_index').

File

developer/hooks/core.php, line 2330
These are the hooks that are invoked by the Drupal core.

Code

function hook_update_index() {
  $last = variable_get('node_cron_last', 0);
  $limit = (int) variable_get('search_cron_limit', 100);
  $result = db_query_range('SELECT n.nid, c.last_comment_timestamp FROM {node} n LEFT JOIN {node_comment_statistics} c ON n.nid = c.nid WHERE n.status = 1 AND n.moderate = 0 AND (n.created > %d OR n.changed > %d OR c.last_comment_timestamp > %d) ORDER BY GREATEST(n.created, n.changed, c.last_comment_timestamp) ASC', $last, $last, $last, 0, $limit);
  while ($node = db_fetch_object($result)) {
    $last_comment = $node->last_comment_timestamp;
    $node = node_load(array(
      'nid' => $node->nid,
    ));

    // We update this variable per node in case cron times out, or if the node
    // cannot be indexed (PHP nodes which call drupal_goto, for example).
    // In rare cases this can mean a node is only partially indexed, but the
    // chances of this happening are very small.
    variable_set('node_cron_last', max($last_comment, $node->changed, $node->created));

    // Get node output (filtered and with module-specific fields).
    if (node_hook($node, 'view')) {
      node_invoke($node, 'view', false, false);
    }
    else {
      $node = node_prepare($node, false);
    }

    // Allow modules to change $node->body before viewing.
    node_invoke_nodeapi($node, 'view', false, false);
    $text = '<h1>' . drupal_specialchars($node->title) . '</h1>' . $node->body;

    // Fetch extra data normally not visible
    $extra = node_invoke_nodeapi($node, 'update index');
    foreach ($extra as $t) {
      $text .= $t;
    }

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