You are here

public function Database::addIndex in Search API 8

Adds a new index to this server.

If the index was already added to the server, the object should treat this as if removeIndex() and then addIndex() were called.

Parameters

\Drupal\search_api\IndexInterface $index: The index to add.

Throws

\Drupal\search_api\SearchApiException Thrown if an error occurred while adding the index.

Overrides BackendPluginBase::addIndex

File

modules/search_api_db/src/Plugin/search_api/backend/Database.php, line 642

Class

Database
Indexes and searches items using the database.

Namespace

Drupal\search_api_db\Plugin\search_api\backend

Code

public function addIndex(IndexInterface $index) {
  try {

    // Create the denormalized table now.
    $index_table = $this
      ->findFreeTable('search_api_db_', $index
      ->id());
    $this
      ->createFieldTable(NULL, [
      'table' => $index_table,
    ], 'index');
    $db_info = [];
    $db_info['server'] = $this->server
      ->id();
    $db_info['field_tables'] = [];
    $db_info['index_table'] = $index_table;
    $this
      ->getKeyValueStore()
      ->set($index
      ->id(), $db_info);
  } catch (\Exception $e) {
    throw new SearchApiException($e
      ->getMessage(), $e
      ->getCode(), $e);
  }

  // If dealing with features or stale data or whatever, we might already have
  // settings stored for this index. If we have, we should take care to only
  // change what is needed, so we don't discard indexed data unnecessarily.
  // The easiest way to do this is by just pretending the index was already
  // present, but its fields were updated.
  $this
    ->fieldsUpdated($index);
}