You are here

function apachesolr_rebuild_index_table in Apache Solr Search 5.2

Same name and namespace in other branches
  1. 6 apachesolr.module \apachesolr_rebuild_index_table()
  2. 6.2 apachesolr.module \apachesolr_rebuild_index_table()

Truncate and rebuild the apachesolr_search_node table, reset the apachesolr_index_last variable. This is the most complete way to force reindexing, or to build the indexing table for the first time.

Parameters

$type: A single content type to be reindexed, leaving the others unaltered.

4 calls to apachesolr_rebuild_index_table()
apachesolr_clear_index_confirm_submit in ./apachesolr.admin.inc
Submit function for the "Re-index all content" confirmation form.
apachesolr_delete_index in ./apachesolr.admin.inc
Utility function to delete the index and reset all index counters.
apachesolr_drush_solr_reindex in ./apachesolr.drush.inc
apachesolr_enable in ./apachesolr.install
Implementation of hook_enable().

File

./apachesolr.module, line 291
Integration with the Apache Solr search application.

Code

function apachesolr_rebuild_index_table($type = NULL) {
  if (isset($type)) {
    switch ($GLOBALS['db_type']) {
      case 'mysql':
      case 'mysqli':
        db_query("DELETE FROM {apachesolr_search_node} USING {apachesolr_search_node} asn INNER JOIN {node} n ON asn.nid = n.nid WHERE n.type = '%s'", $type);
        break;
      default:
        db_query("DELETE FROM {apachesolr_search_node} WHERE nid IN (SELECT nid FROM {node} WHERE type = '%s')", $type);
        break;
    }

    // Populate table
    db_query("INSERT INTO {apachesolr_search_node} (nid, status, changed)\n              SELECT n.nid, n.status, %d AS changed\n              FROM {node} n WHERE n.type = '%s'", time(), $type);
  }
  else {
    db_query("DELETE FROM {apachesolr_search_node}");

    // Populate table.
    if (module_exists('comment')) {

      // If comment module is enabled, use last_comment_timestamp as well.
      db_query("INSERT INTO {apachesolr_search_node} (nid, status, changed)\n              SELECT n.nid, n.status, GREATEST(n.created, n.changed, COALESCE(c.last_comment_timestamp, 0)) AS changed\n              FROM {node} n\n              LEFT JOIN {node_comment_statistics} c ON n.nid = c.nid");
    }
    else {
      db_query("INSERT INTO {apachesolr_search_node} (nid, status, changed)\n              SELECT n.nid, n.status, GREATEST(n.created, n.changed) AS changed\n              FROM {node} n");
    }

    // Make sure no nodes end up with a timestamp that's in the future.
    $time = time();
    db_query("UPDATE {apachesolr_search_node} SET changed = %d WHERE changed > %d", $time, $time);
    apachesolr_clear_last_index();
  }
}