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 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:

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):

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:

$ 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:

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:

$ 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

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!

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