Setting up ElasticSearch with Symfony3 and Platform.sh
Following are instructions for setting up and configuring ElasticSearch 5.6 with Symfony3 on Platform.sh (and locally).
Install ElasticSearch Locally:
For this I used Homebrew. The process is straightforward:
brew install elasticsearch
To run Elasticsearch, you should be able to open a terminal and type:
elasticsearch
Configuration for local/platform environments
Since we need to be able to run elasticsearch both locally and in the platform environments we need to configure for both.
Local Config
These are your local configuration parameters for your ElasticServer instance. Make sure to update parameters.yml.dist also.
#app/config/parameters.yml
parameters:
... elasticsearch_host: 127.0.0.1
elasticsearch_port: 9200
Platform Config
This will override your local config for these parameters when in the Platform.sh environment.
<?php// app/config/parameters_platform.php$relationships = getenv("PLATFORM_RELATIONSHIPS");
if (!$relationships) {
return;
}
$relationships = json_decode(base64_decode($relationships), true);
foreach ($relationships['elasticsearch'] as $endpoint) {
$container->setParameter('elasticsearch_host', $endpoint['host']);
$container->setParameter('elasticsearch_port', $endpoint['port']);
}
Also add the service to .platform/services.yaml
#.platform/services.yamlsitesearch:
type: elasticsearch:5.2
disk: 1024
…and the relationship to .platform.app.yaml
#.platform.app.yamlrelationships:
...
elasticsearch: "sitesearch:elasticsearch"
Install the FOS Elastica Bundle:
Follow the installation and configuration instructions available here:
https://github.com/FriendsOfSymfony/FOSElasticaBundle/blob/master/Resources/doc/setup.md
I went ahead and set up an index on one entity only for testing purposes. My config looks like this:
#app/config/config.yml
fos_elastica:
clients:
default: { host: "%elasticsearch_host%", port: "%elasticsearch_port%" }
indexes:
app:
types:
work:
properties:
title: ~
description: ~
intro: ~
persistence:
driver: orm
model: AppBundle\Entity\Post
provider: ~
finder: ~
Using parameters for the client, you can run your local or platform ElasticSearch instance accordingly.
While ElasticSearch is running, run the following command to populate your index:
php bin/console fos:elastica:populate
You’ll also want to add that command to your deploy hook in your platform config.