Using Elasticsearch with Drupal 9.x


Add an Elasticsearch service 

1. Configure the service 

Use the elasticsearch type to define the service:

    type: elasticsearch:<VERSION>
    disk: 256

Note that if you later change the name, it’s treated as an entirely new service. This removes all data from your service. Always backup your data before changing the service.

2. Add the relationship 

Use the elasticsearch endpoint to define the relationship:
    <RELATIONSHIP_NAME>: "<SERVICE_NAME>:elasticsearch"

You can define <SERVICE_NAME> and <RELATIONSHIP_NAME> as you like, but it’s best if they’re distinct.

Example Configuration 

Service definition

    type: elasticsearch:7.10
    disk: 256

App configuration
    essearch: "searchelastic:elasticsearch"

Add the Drupal modules 

You need to add the Search API and Elasticsearch Connector modules to your project. If you are using composer, the easiest way to add them is to run:

$ composer require drupal/search_api drupal/elasticsearch_connector

And then commit the changes to composer.json and composer.lock.


Because Drupal defines connection information via the Configuration Management system, you need to first define an Elasticsearch “Cluster” at admin/config/search/elasticsearch-connector. Note the “machine name” the server is given.

Then, paste the following code snippet into your settings.platformsh.php file.

  • Edit the value of $relationship_name if you are using a different relationship.

  • Edit the value of $es_server_name to match the machine name of your cluster in Drupal.


// Update these values to the relationship name (from
// and the machine name of the server from your Drupal configuration.
$relationship_name = 'essearch';
$es_cluster_name = 'YOUR_CLUSTER_HERE';
if ($platformsh->hasRelationship($relationship_name)) {
  $platformsh->registerFormatter('drupal-elastic', function($creds) {
    return sprintf('http://%s:%s', $creds['host'], $creds['port']);

  // Set the connector configuration to the appropriate value, as defined by the formatter above.
  $config['elasticsearch_connector.cluster.' . $es_cluster_name]['url'] = $platformsh->formattedCredentials($relationship_name, 'drupal-elastic');

Commit that code and push. The specified cluster now always points to the Elasticsearch service. Then configure Search API as normal.