You are here

function search_api_index_recalculate_fields in Search API 7

Recalculates the saved fields of an index.

This is mostly necessary when the multiplicity of the underlying properties change. The method will re-examine the data structure of the entities in each index and, if a discrepancy is spotted, re-save that index with updated fields options (thus, of course, also triggering a re-indexing operation).

Parameters

SearchApiIndex[]|false $indexes: An array of SearchApiIndex objects on which to perform the operation, or FALSE to perform it on all indexes.

2 calls to search_api_index_recalculate_fields()
search_api_admin_index_workflow_submit in ./search_api.admin.inc
Form submission handler for search_api_admin_index_workflow().
search_api_flush_caches in ./search_api.module
Implements hook_flush_caches().
1 string reference to 'search_api_index_recalculate_fields'
search_api_field_update_field in ./search_api.module
Implements hook_field_update_field().

File

./search_api.module, line 1674
Provides a flexible framework for implementing search services.

Code

function search_api_index_recalculate_fields($indexes = FALSE) {
  if (!is_array($indexes)) {
    $indexes = search_api_index_load_multiple(FALSE);
  }
  $stored_keys = drupal_map_assoc(array(
    'type',
    'entity_type',
    'real_type',
    'boost',
  ));
  foreach ($indexes as $index) {
    if (empty($index->options['fields'])) {
      continue;
    }

    // We have to clear the cache, both static and stored, before using
    // getFields(). Otherwise, we'd just use the stale data which the fields
    // options are probably already based on.
    cache_clear_all($index
      ->getCacheId() . '-1-0', 'cache');
    $index
      ->resetCaches();

    // getFields() automatically uses the actual data types to correct possible
    // stale data.
    $fields = $index
      ->getFields();
    foreach ($fields as $key => $field) {
      $fields[$key] = array_intersect_key($field, $stored_keys);
      if (isset($fields[$key]['boost']) && $fields[$key]['boost'] == '1.0') {
        unset($fields[$key]['boost']);
      }
    }

    // Use a more accurate method of determining if the fields settings are
    // equal to avoid needlessly re-indexing the whole index.
    if ($fields != $index->options['fields']) {
      $options = $index->options;
      $options['fields'] = $fields;
      $index
        ->update(array(
        'options' => $options,
      ));
    }
  }
}