This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

✨ Vultr and Linode are back on Cloudways! VIEW PLANS→

Configure Elasticsearch in Symfony 5.x Applications Using FOSElasticaBundle

Updated on September 30, 2021

5 Min Read
symfony elasticsearch

Popular web projects often consist of a complex database schema and its implementation by applying different search algorithms and logics. If a database contains millions of records, you surely need a smart system which can search queries in milliseconds.

Using Symfony Elasticsearch is the one such smart search engine based on Lucene. It provides a full text search engine with HTTP web interface and schema-free JSON documents. Elasticsearch allows you to search and store data in realtime. The search time is much faster than SQL.

Elasticsearch has become well known for creating advanced database search systems which properly match the queries and display the best-matched results. In this article, I will explain Elasticsearch configuration in Symfony 5.x and use it to create a search system on a Cloudways hosting for PHP website.


Before starting with Elasticsearch in Symfony, the following prerequisites must be fulfilled:

  1. Sign up for a free account and create a server on Cloudways
  2. Enable Elasticsearch from the Platform

Let’s get started by first signing up on Cloudways and launching a server with PHP stack application. Check out the following GIF for more details on the topic:

cloudways application

Fortunately, Cloudways has Elasticsearch pre-installed on all servers. You need to enable it by going to server settings, and then Setting & Packages tab and enabled your required version.


By default, Elasticsearch is disabled on the server. To enable it, just click the Start button.

Now to check if the Elasticsearch is running on your server, open SSH terminal and run the following command:

$ Curl localhost:9200

It will return the Elasticsearch version and additional information:

You can see it’s working on the server. Cloudways provides the easiest way to using Elasticsearch in just a single click. The next step is to install Symfony on the server.

Nothing as Easy as Deploying PHP Apps on Cloud

With Cloudways, you can have your PHP apps up and running on managed cloud servers in just a few minutes.

Install Symfony 5.x in the Public Directory

Now, as I have configured Elasticsearch at the server, I can now configure it in the Symfony application. However, first I need to install the Symfony framework.

Go to the application folder from the SSH terminal:

$ cd application/{your_app_/public_html

Run the following command to install a Symfony standard project:

$ composer create-project symfony/framework-standard-edition your_project_name "5.x.*"

You can install any version of Symfony 5.x (from 3.0 to 5.X). Replace the “5.x” with the chosen version in the above command. The installer will ask for the database credentials(available in Application Access Details tab)

mysql access

Install and Register FosElasticaBundle

Symfony provides Foselasticabundle bundle which provides integration with Elasticsearch and Elastica. This bundle automatically generates mapping using Serializer and contains listeners for Doctrine events for automatic indexing. The Foselasticabundle is now supported by all Symfony versions.

To install this bundle, go to the Symfony application folder and run the following command:

$ composer require friendsofsymfony/elastica-bundle

This command will install the bundle in Symfony. Now, go to the app folder and open appKernel.php

File. Register the bundle in the bundle’s array:


Create an Entity: Books

For mapping and indexing purposes, I will create an entity in Symfony, which will contain Id, name and author columns. This entity will be defined in fostelasticabundle.yml. I will create this file after generating an entity Books. Run the following command:

$ php bin/console doctrine:generate:entity

Note: Since the id column is autogenerated by Doctrine, I will only define name and author


Finally, to update fields in the database, run the following command:

$ php bin/console doctrine:schema:update --force

Create Foselasticabundle.yml in Config

I will create a separate file for defining indexing in Elasticsearch. Create foselasticabundle.yml in the config folder and add the following minimal configuration:

           port: %fos_elastica.port%
           finder: ~
           client: default
                           type: integer
                       title: ~
                       author: ~
                       driver: orm
                       model: AppBundle\Entity\Books
                       finder: ~
                       provider: ~
                       listener: ~

This is pretty much self-explanatory. In addition to host and port values, the other keys are:

Indexes: This contains the name of the index in Elasticsearch. In this case, “search” is the index. Indexes are similar to the database name in MySQL.

Types: This defines the type of document which will be indexed. Types are similar to the table names in MySQL.

Mappings: This contains the list of all documents. Mappings are similar to the table columns in MySql. In this case I have three columns in table id,name and author.

Persistence: This defines how the documents will be persisted according to the entity defined in the model

Define Parameters & Import Foselasticabundle.yml

To get the host and port values in foselasticabundle.yml, I will define the values in the parameters.yml file. Open the file and add two more values in the list:

parameters: localhost  // Host for elasticsearch
   fos_elastica.port: 9200          // Port where elasticsearch runs

Finally, import foselasticabundle.yml file in config.yml:

   - { resource: foselasticabundle.yml }

Update Indexes In Elasticsearch

At this point, all necessary components are ready and I have set up the necessary configuration for Elasticsearch in Symfony. It’s time to update the indexes. You can also update indexes by performing CRUD operations. However, If you have imported SQL files in the database, you need to update indexes in Elasticsearch. For this, run the following command:

$ php app/console fos:elastica:populate

The output of this command is:

Indexes In Elasticsearch

If you want to see the list of all indexes, run the following command:

$ curl http://localhost:9200/_aliases?pretty=1

Update Indexes In Elasticsearch

Additionally, you can also fetch all the indexes with their respective mapping and type fields with this command:

$ curl -XGET 'http://localhost:9200/_mapping?pretty=true'


Elasticsearch is now properly configured in Symfony application, and you can now create custom controllers and methods to perform CRUD operations on the entity.

Final Words

Symfony Elasticsearch is the most competitive candidate for Big Data search. It provides a simple web interface that returns the best-matched results.

In this article, I configured Elasticsearch with Symfony 5.x versions. Using this article as a guide, you can create custom controllers to work with the data in Elasticsearch.

If you have any questions or queries, leave a comment below and I will get back to you!

Share your opinion in the comment section. COMMENT NOW

Share This Article

Shahzeb Ahmed

Shahzeb is a Digital Marketer with a Software Engineering background, works as a Community Manager — PHP Community at Cloudways. He is growth ambitious and aims to learn & share information about PHP & Laravel Development through practice and experimentation. He loves to travel and explore new ideas whenever he finds time. Get in touch with him at [email protected]


Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!


Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Want to Experience the Cloudways Platform in Its Full Glory?

Take a FREE guided tour of Cloudways and see for yourself how easily you can manage your server & apps on the leading cloud-hosting platform.

Start my tour


  • 0


  • 0


  • 0


  • 0



For 4 Months &
40 Free Migrations

For 4 Months &
40 Free Migrations

Upgrade Now