Fatal error when working with indices with custom cluster prefix/suffix

Created on 13 January 2020, almost 5 years ago
Updated 13 June 2023, over 1 year ago

My Elasticsearch Connector has custom Cluster prefix and Search APU Elasticsearch Attachments module results in a fatal error when updating the search index mapping:

Error: Call to a member function getProcessors() on null in Drupal\search_api_elasticsearch_attachments\EventSubscriber\PrepareIndexMapping->indexMapping() (line 32 of /app/docroot/modules/contrib/search_api_elasticsearch_attachments/src/EventSubscriber/PrepareIndexMapping.php) 
#0 [internal function]: Drupal\search_api_elasticsearch_attachments\EventSubscriber\PrepareIndexMapping->indexMapping(Object(Drupal\elasticsearch_connector\Event\PrepareIndexMappingEvent), 'elasticsearch_c...', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#1 /app/docroot/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(111): call_user_func(Array, Object(Drupal\elasticsearch_connector\Event\PrepareIndexMappingEvent), 'elasticsearch_c...', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher))
#2 /app/docroot/modules/contrib/elasticsearch_connector/src/ElasticSearch/Parameters/Factory/IndexFactory.php(222): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('elasticsearch_c...', Object(Drupal\elasticsearch_connector\Event\PrepareIndexMappingEvent))
#3 /app/docroot/modules/contrib/elasticsearch_connector/src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php(439): Drupal\elasticsearch_connector\ElasticSearch\Parameters\Factory\IndexFactory::mapping(Object(Drupal\search_api\Entity\Index))
#4 /app/docroot/modules/contrib/elasticsearch_connector/src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php(400): Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend->fieldsUpdated(Object(Drupal\search_api\Entity\Index))#5 /app/docroot/modules/contrib/elasticsearch_connector/src/Plugin/search_api/backend/SearchApiElasticsearchBackend.php(382): Drupal\elasticsearch_connector\Plugin\search_api\backend\SearchApiElasticsearchBackend->updateIndex(Object(Drupal\search_api\Entity\Index))
...

It turns out that the \Drupal\search_api_elasticsearch_attachments\Helpers::getIndexName() doesn't work properly when the ES Cluster has "Alter the Elasticsearch index name" enabled. The helper method only replaces the default prefix 'elasticsearch_index_' . $site_database . '_' from the index name, hence it returns the full index name on the ES server and the next line of code returns a fatal error:

$indexName = Helpers::getIndexName($event->getIndexName());
$processors = Index::load($indexName)->getProcessors();

In Elasticsearch Connector 8.x-6.x, the IndexFactory service always passes the ID of the search index entity along with the index parameters:

  public static function index(IndexInterface $index, $with_type = FALSE) {
    $params = [];
    $params['index'] = static::getIndexName($index);

    if ($with_type) {
      $params['type'] = $index->id();
    }

    return $params;
  }

...

  public static function mapping(IndexInterface $index) {
    $params = static::index($index, TRUE);
    ...
  }
Drupal\elasticsearch_connector\Event\PrepareIndexMappingEvent Object
(
    [indexMappingParams:protected] => Array
        (
            [index] => elasticsearch_index_nonprod_node_local
            [type] => node
            ...
        )

    [indexName:protected] => elasticsearch_index_nonprod_node_local
    [propagationStopped:Symfony\Component\EventDispatcher\Event:private] => 
)

IMO it's safe to use the type key from index params instead of solely relying on the helper method.

🐛 Bug report
Status

Needs review

Component

Code

Created by

🇦🇺Australia sonnykt Melbourne, Australia

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇷🇴Romania ciprian.stavovei

    Had some issues with existing patches. They don't work in all scenarios, for example when a the prefix doesn't have the "_", it is automatically added in the elasticsearch_connector but not considered in the patch. Added a new and improved one.

Production build 0.71.5 2024