You are here

public function SearchApiElasticsearchBackend::addIndex in Elasticsearch Connector 8.2

Same name and namespace in other branches
  1. 8.7 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::addIndex()
  2. 8 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::addIndex()
  3. 8.5 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::addIndex()
  4. 8.6 src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php \Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend::addIndex()

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

2 calls to SearchApiElasticsearchBackend::addIndex()
SearchApiElasticsearchBackend::deleteAllIndexItems in src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php
Deletes all the items from the index.
SearchApiElasticsearchBackend::fieldsUpdated in src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php

File

src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php, line 267
Contains the SearchApiElasticsearchBackend object.

Class

SearchApiElasticsearchBackend
Plugin annotation @SearchApiBackend( id = "elasticsearch", label = @Translation("Elasticsearch"), description = @Translation("Index items using an Elasticsearch server.") )

Namespace

Drupal\elasticsearch_connector\Plugin\search_api\backend

Code

public function addIndex(IndexInterface $index) {
  $index_name = IndexFactory::getIndexName($index);
  if (!empty($index_name)) {
    try {
      if (!$this->client
        ->indices()
        ->exists(IndexFactory::index($index))) {
        $response = $this->client
          ->indices()
          ->create(IndexFactory::create($index));
        if (!$this->client
          ->CheckResponseAck($response)) {
          drupal_set_message($this
            ->t('The elasticsearch client was not able to create index'), 'error');
        }
      }

      // Update mapping.
      $this
        ->fieldsUpdated($index);
    } catch (ElasticsearchException $e) {
      drupal_set_message($e
        ->getMessage(), 'error');
    }
  }
}