You are here

public function SearchApiElasticsearchConnector::addIndex in Elasticsearch Connector 7.5

Same name and namespace in other branches
  1. 7 modules/elasticsearch_connector_search_api/service.inc \SearchApiElasticsearchConnector::addIndex()
  2. 7.2 modules/elasticsearch_connector_search_api/service.inc \SearchApiElasticsearchConnector::addIndex()

Overrides addIndex().

Overrides SearchApiAbstractService::addIndex

1 call to SearchApiElasticsearchConnector::addIndex()
SearchApiElasticsearchConnector::deleteItems in modules/elasticsearch_connector_search_api/service.inc
Overrides deleteItems().

File

modules/elasticsearch_connector_search_api/service.inc, line 270
Provides a Elasticsearch-based service class for the Search API using Elasticsearch Connector module.

Class

SearchApiElasticsearchConnector
Search service class.

Code

public function addIndex(SearchApiIndex $index) {
  $index_name = $this
    ->getIndexName($index);
  if (!empty($index_name)) {
    try {
      $client = $this->elasticsearchClient;
      if (!$client
        ->indices()
        ->exists(array(
        'index' => $index_name,
      ))) {

        // TODO: This needs to be handled differently because the >2.0.0
        // needs index recreation!
        if (!empty($index->force_create)) {
          $params = array(
            'index' => $index_name,
            'body' => array(
              'settings' => array(
                'number_of_shards' => $index->force_create['number_of_shards'],
                'number_of_replicas' => $index->force_create['number_of_replicas'],
              ),
            ),
          );
          drupal_alter('elasticsearch_connector_search_api_add_index', $index, $params);
          $response = $client
            ->indices()
            ->create($params);
          if (!elasticsearch_connector_check_response_ack($response)) {
            drupal_set_message(t('The elasticsearch client wasn\'t able to create index'), 'error');
          }
        }
        else {
          throw new SearchApiElasticsearchConnectorException(t('The index you have selected, does not exist.'));
        }
      }

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