Platform.sh User Documentation

Using Elasticsearch with Drupal

Try for 30 days
Flexible, version-controlled infrastructure provisioning and development-to-production workflows
Activate your 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:

.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 following configuration:

.platform/applications.yaml
# Relationships enable access from this app to a given service.
# The example below shows simplified configuration leveraging a default service
# (identified from the relationship name) and a default endpoint.
# See the Application reference for all options for defining relationships and endpoints.
relationships:
    <SERVICE_NAME>: 

You can define <SERVICE_NAME> as you like, so long as it’s unique between all defined services and matches in both the application and services configuration.

The example above leverages default endpoint configuration for relationships. That is, it uses default endpoints behind-the-scenes, providing a relationship (the network address a service is accessible from) that is identical to the name of that service.

Depending on your needs, instead of default endpoint configuration, you can use explicit endpoint configuration.

With the above definition, the application container now has access to the service via the relationship <RELATIONSHIP_NAME> and its corresponding PLATFORM_RELATIONSHIPS environment variable.

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.
elasticsearch:
    type: elasticsearch:8.5
    disk: 256

App configuration Anchor to this heading

.platform/applications.yaml
# Relationships enable access from this app to a given service.
# The example below shows simplified configuration leveraging a default service
# (identified from the relationship name) and a default endpoint.
# See the Application reference for all options for defining relationships and endpoints.
relationships:
    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?