hook_update_index
- Versions
- 4.6 – 7
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
Code
modules/search/search.api.php, line 253
<?php
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[FIELD_LANGUAGE_NONE][0]['value']) . '</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);
}
}
?>Login or register to post comments 