You are here

public function SearchApiElasticsearchElastica::fieldsUpdated in Search API Elasticsearch 7

Overrides fieldsUpdated().

Overrides SearchApiElasticsearchAbstractService::fieldsUpdated

1 call to SearchApiElasticsearchElastica::fieldsUpdated()
SearchApiElasticsearchElastica::addIndex in modules/elastica/includes/SearchApiElasticsearchElastica.inc
Overrides addIndex().

File

modules/elastica/includes/SearchApiElasticsearchElastica.inc, line 191
Provides Elastica client for Search API Elasticsearch.

Class

SearchApiElasticsearchElastica
Search API Elasticsearch Elastica service class.

Code

public function fieldsUpdated(SearchApiIndex $index) {
  parent::fieldsUpdated($index);
  $elastica_index = $this
    ->getElasticaIndex($index);
  if (!empty($elastica_index)) {
    $elastica_type = $elastica_index
      ->getType($index->machine_name);

    // Create a new mapping.
    $mapping = new Elastica\Type\Mapping();
    $mapping
      ->setType($elastica_type);
    $mapping
      ->setParam('_all', array(
      'enabled' => FALSE,
    ));
    try {
      try {

        // First we try a simple merge.
        $mapping
          ->setProperties($this->fieldsUpdatedProperties);
        $mapping
          ->send();
      } catch (Exception $e) {

        // If a merge fails, we must delete the type first.
        $elastica_type
          ->delete();
        $elastica_type = $elastica_index
          ->getType($index->machine_name);
        $mapping
          ->setType($elastica_type);
        $mapping
          ->send();
      }
    } catch (Exception $e) {
      watchdog('Elasticsearch', check_plain($e
        ->getMessage()), array(), WATCHDOG_ERROR);
      drupal_set_message(check_plain($e
        ->getMessage()), 'error');
      drupal_set_message(t('Fields are not re-indexed'), 'error');
      return FALSE;
    }
  }

  // Flag for re-indexing.
  return TRUE;
}