
- Configure Elasticsearch in Symfony 5 to build a powerful search engine for your web application.
- Use FosElasticaBundle to integrate Elasticsearch with Symfony, enabling efficient indexing and querying of data.
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.
Prerequisites
Before starting with Elasticsearch in Symfony, the following prerequisites must be fulfilled:
- Sign up for a free account and create a server on Cloudways
- 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:
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)
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:
fos_elastica: clients: default: host: %fos_elastica.host% port: %fos_elastica.port% indexes: search: finder: ~ client: default types: books: mappings: id: type: integer title: ~ author: ~ persistence: 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: fos_elastica.host: localhost // Host for elasticsearch fos_elastica.port: 9200 // Port where elasticsearch runs
Finally, import foselasticabundle.yml file in config.yml:
imports: - { 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:
If you want to see the list of all indexes, run the following command:
$ curl http://localhost:9200/_aliases?pretty=1
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!
Q. What are the prerequisites for using FOSElasticaBundle in Symfony 5.x?
Before using FOSElasticaBundle in Symfony 5.x, you need Elasticsearch installed and running, along with Symfony 5.x and Composer.
Make sure your Symfony project has the Doctrine ORM configured if you’re indexing database entities. If you’re hosting your application on Cloudways, you can set up Elasticsearch on a managed server for better performance and scalability.
Q. How to configure Symfony on Elasticsearch step by step?
Follow the steps below to configure Symfony on Elasticsearch.
1. Install Elasticsearch on your server or use a hosted service.
2. Add FOSElasticaBundle to your Symfony project with Composer:
composer require friendsofsymfony/elastica-bundle
3. Configure the bundle in config/packages/fos_elastica.yaml by defining connections, indexes, and mappings.
4. Set up an Elasticsearch client and connect it to your database entities.
5. Run indexing commands to populate data in Elasticsearch:
bin/console fos:elastica:populate
6. Test the integration by querying data using FOSElasticaBundle services.
Q. Why should I use FOSElasticaBundle in my Symfony application?
FOSElasticaBundle makes it easier to integrate Elasticsearch with Symfony, providing powerful full-text search capabilities. It helps efficiently index and retrieve data, improving search performance for applications that handle large datasets.
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]