You are here

public function SearchApiElasticsearchConnector::fieldsUpdated in Elasticsearch Connector 7

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

Overrides fieldsUpdated().

Overrides SearchApiAbstractService::fieldsUpdated

2 calls to SearchApiElasticsearchConnector::fieldsUpdated()
SearchApiElasticsearchConnector::addIndex in modules/elasticsearch_connector_search_api/service.inc
Overrides addIndex().
SearchApiElasticsearchConnector::indexItems in modules/elasticsearch_connector_search_api/service.inc
Overrides indexItems().

File

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

Class

SearchApiElasticsearchConnector
Search service class.

Code

public function fieldsUpdated(SearchApiIndex $index) {
  $params = $this
    ->getIndexParam($index, TRUE);
  $properties = array(
    'id' => array(
      'type' => 'string',
      'include_in_all' => FALSE,
    ),
  );

  // Map index fields.
  foreach ($index
    ->getFields() as $field_id => $field_data) {
    $properties[$field_id] = $this
      ->getFieldMapping($field_data);
  }
  drupal_alter('elasticsearch_connector_search_api_field_update', $index, $properties);

  // Not remapping for read only indexes as it removes all data
  if (!isset($index->read_only) || !$index->read_only) {
    try {
      if ($this->elasticsearchClient
        ->indices()
        ->existsType($params)) {
        $current_mapping = $this->elasticsearchClient
          ->indices()
          ->getMapping($params);
        if (!empty($current_mapping)) {

          // If the mapping exits, delete it to be able to re-create it.
          $this->elasticsearchClient
            ->indices()
            ->deleteMapping($params);
        }
      }
      $params['body'][$params['type']]['properties'] = $properties;
      drupal_alter('elasticsearch_connector_search_api_mapping_update', $index, $params['body'][$params['type']]);
      $results = $this->elasticsearchClient
        ->indices()
        ->putMapping($params);
      if (!elasticsearch_connector_check_response_ack($results)) {
        drupal_set_message(t('Cannot create the mapping of the fields!'), 'error');
      }
    } catch (Exception $e) {
      drupal_set_message($e
        ->getMessage(), 'error');
      return FALSE;
    }
  }
  return TRUE;
}