Platform.sh User Documentation

Using Elasticsearch with Drupal

Upsun Beta

Access our newest offering - Upsun!

Get your free trial by clicking the link below.

Get your Upsun free trial

Requirements Anchor to this heading

Add an Elasticsearch service Anchor to this heading

1. Configure the service Anchor to this heading

To define the service, use the elasticsearch type:

.platform/services.yaml
# The name of the service container. Must be unique within a project.
<SERVICE_NAME>:

    type: elasticsearch:<VERSION>
    disk: 256

If you’re using a premium version, use the elasticsearch-enterprise type instead.

Note that changing the name of the service replaces it with a brand new service and all existing data is lost. Back up your data before changing the service.

2. Add the relationship Anchor to this heading

To define the relationship, use the elasticsearch endpoint :

.platform.app.yaml
# Relationships enable access from this app to a given service.
relationships:
    <RELATIONSHIP_NAME>: "<SERVICE_NAME>:elasticsearch"

You can define <SERVICE_NAME> and <RELATIONSHIP_NAME> as you like, but it’s best if they’re distinct. With this definition, the application container now has access to the service via the relationship <RELATIONSHIP_NAME>.

Example Configuration Anchor to this heading

Service definition Anchor to this heading

.platform/services.yaml
# The name of the service container. Must be unique within a project.
searchelastic:

    type: elasticsearch:8.5
    disk: 256

App configuration Anchor to this heading

.platform.app.yaml
# Relationships enable access from this app to a given service.
relationships:
    essearch: "searchelastic:elasticsearch"

If you’re using a premium version, use the elasticsearch-enterprise type in the service definition.

Add the Drupal modules Anchor to this heading

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.

Configuration Anchor to this heading

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.

<?php

// Update these values to the relationship name (from .platform.app.yaml)
// 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.

Is this page helpful?