Configure Elasticsearch in Symfony 3.x Applications Using FOSElasticaBundle

by Shahroze Nawaz  May 9, 2017

Popular web projects often comprise 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.

Elasticsearch is the one such smart search engine based on Lucene. It provides 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 in symfony

Elasticsearch has become well known for creating advanced database search systems which properly matches the queries and displays the best-matched results. In this article, I will explain Elasticsearch configuration in Symfony 3.x and use it to create a search system.

Prerequisites

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

You can use promo code: PHP15 to get FREE Cloudways hosting credit of $15 on signup.

Let’s get start 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 installed by default on all servers. You need to enable it by going to server settings, and then Manage Services tab:

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:

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.

Install Symfony 3.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:

Run the following command to install a Symfony standard project:

You can install any version of Symfony 3.x (3.0, 3.1, 3.2). Replace the “3.x” with the chosen version in the above command. The installer will ask for the database credentials  (available in Application Access Details tab):

Related: How To Upgrade From Symfony 2.X To 3.X

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 3.x versions.

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

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:

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:

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:

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:

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

Update Indexes In Elasticsearch

At this point, all necessary components are ready and I have setup 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 import SQL file in the database, you need to update indexes in Elasticsearch. For this, run the following command:

The output of this command is:

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

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

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

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 3.x versions. Using this article as a guide, you can create custom controllers to work with the data in Elasticsearch.

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

Start Creating Web Apps on Managed Cloud Servers Now!

Easy Web App Deployment for Agencies, Developers and E-Commerce Industry

About Shahroze Nawaz

Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. He’s always in search of new frameworks and methods to implement them. Besides his coding life, he loves movies and playing soccer with friends. You can email him at shahroze.nawaz@cloudways.com

Stay Connected:

You Might Also Like...