You are here

function _similar_add_index in Similar Entries 7

Same name and namespace in other branches
  1. 6.2 similar.module \_similar_add_index()

Adds FULLTEXT indexes to field value database columns.

We want to make sure that we index as many columns as are available with a single index to improve results. So we remove the old index from the table before then reindexing the new fields. Indexed fields are then stored in a Drupal variable which is an array of sub-arrays, with the array key the table name and the sub-array being the fields that are indexed. Before calling this function the $fields argument should be compared against the existing index as stored in the similar_indices variable.

Parameters

$table: A string representing the table to index.

$fields: An array of column names to add to the index. Even though most, if not all of our tables will only have one indexed column, we still may need to add multiple fields some time.

1 call to _similar_add_index()
similar_cron in ./similar.module
Implements hook_cron().

File

./similar.module, line 84
Module that shows a block listing similar entries. NOTE: Uses MySQL's FULLTEXT indexing for MyISAM tables.

Code

function _similar_add_index($table, $fields) {
  $index = similar_get_indices();
  if (db_table_exists($table)) {

    // Drop the existing index on the table if it exists.
    if (isset($index[$table]) && !empty($index[$table]) && db_index_exists($table, 'similar')) {
      db_drop_index($table, 'similar');
      unset($index[$table]);
    }
    if (!empty($fields)) {
      $add_fields = implode(', ', $fields);
      db_query("ALTER TABLE {$table} ENGINE = MYISAM");
      db_query("ALTER TABLE {$table} ADD FULLTEXT `similar` ({$add_fields})");
      $index[$table] = $fields;
    }
  }
  elseif (isset($index[$table])) {
    unset($index[$table]);
  }
  variable_set('similar_indices', $index);
}