You are here

public function FuzzySearchService::fieldsUpdated in Fuzzy Search 7

Implements SearchApiServiceInterface::__construct().

The default implementation always returns FALSE.

Overrides SearchApiAbstractService::fieldsUpdated

File

includes/service.inc, line 264

Class

FuzzySearchService
Search service class using the database for storing index information.

Code

public function fieldsUpdated(SearchApiIndex $index) {
  $fields =& $this->options['indexes'][$index->machine_name];
  $new_fields = $index
    ->getFields();
  $reindex = FALSE;
  $cleared = FALSE;
  $set = $this
    ->setDb();
  foreach ($fields as $name => $field) {
    if (!isset($new_fields[$name])) {
      db_drop_table($field['table']);
      unset($fields[$name]);
      continue;
    }
    $old_type = $field['type'];
    $new_type = $new_fields[$name]['type'];
    $fields[$name]['type'] = $new_type;
    $fields[$name]['boost'] = $new_fields[$name]['boost'];
    $old_inner_type = search_api_extract_inner_type($old_type);
    $new_inner_type = search_api_extract_inner_type($new_type);
    if ($old_type != $new_type) {
      if ($old_inner_type == 'text' || $new_inner_type == 'text' || search_api_list_nesting_level($old_type) != search_api_list_nesting_level($new_type)) {

        // A change in fulltext or list status necessitates completely
        // clearing the index.
        $reindex = TRUE;
        if (!$cleared) {
          $cleared = TRUE;
          $this
            ->deleteItems('all', $index);
        }
        db_drop_table($field['table']);
        $this
          ->createFieldTable($index, $new_fields[$name], $field['table']);
      }
      elseif ($this
        ->sqlType($old_inner_type) != $this
        ->sqlType($new_inner_type)) {

        // There is a change in SQL type. We don't have to clear the
        // index, since types can be converted.
        db_change_field($field['table'], 'value', 'value', $this
          ->sqlType($new_type) + array(
          'description' => "The field's value for this item.",
        ));
        $reindex = TRUE;
      }
      elseif ($old_inner_type == 'date' || $new_inner_type == 'date') {

        // Even though the SQL type stays the same, we have to reindex
        // since conversion rules change.
        $reindex = TRUE;
      }
    }
    elseif (!$reindex && $new_inner_type == 'text' && $field['boost'] != $new_fields[$name]['boost']) {
      $multiplier = $new_fields[$name]['boost'] / $field['boost'];
      db_update($field['table'], $this->queryOptions)
        ->expression('score', 'score * :mult', array(
        ':mult' => $multiplier,
      ))
        ->execute();
    }
    unset($new_fields[$name]);
  }
  $prefix = 'fuzzysearch_' . $index->machine_name . '_';

  // These are new fields that were previously not indexed.
  foreach ($new_fields as $name => $field) {
    $reindex = TRUE;
    $table = $this
      ->findFreeTable($prefix, $name);
    $this
      ->createFieldTable($index, $field, $table);
    $fields[$name]['table'] = $table;
    $fields[$name]['type'] = $field['type'];
    $fields[$name]['boost'] = $field['boost'];
  }
  if ($set) {
    $this
      ->resetDb();
  }
  $this->server
    ->save();
  return $reindex;
}