You are here

public function SearchApiElasticsearchBackend::addIndex in Elasticsearch Connector 8

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.2 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()

Overrides addIndex().

Overrides BackendPluginBase::addIndex

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

File

src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php, line 344
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) {
  $this
    ->connect();
  $index_name = $this
    ->getIndexName($index);
  if (!empty($index_name)) {
    try {
      $client = $this->elasticsearchClient;
      if (!$client
        ->indices()
        ->exists(array(
        'index' => $index_name,
      ))) {
        $params = array(
          // TODO: Add the timeout option.
          'index' => $index_name,
          'body' => array(
            'settings' => array(
              'number_of_shards' => isset($index->options['number_of_shards']) ? $index->options['number_of_shards'] : 5,
              'number_of_replicas' => isset($index->options['number_of_replicas']) ? $index->options['number_of_replicas'] : 1,
            ),
          ),
        );
        $response = $client
          ->indices()
          ->create($params);
        if (!Cluster::elasticsearchCheckResponseAck($response)) {
          drupal_set_message(t('The elasticsearch client wasn\'t able to create index'), 'error');
        }
      }

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