Symfony 2.8 Jobeet Day 16: Search

Two days ago, we added some feeds to keep Jobeet users up-to-date with new job posts. Today, we will continue to improve the user experience by implementing the latest main feature of the Jobeet website: the search engine.

The Technology

For this feature we will use ElasticSearch.

Elasticsearch is a highly scalable open-source full-text search and analytics engine. It allows you to store, search, and analyze big volumes of data quickly and in near real time. It is generally used as the underlying engine/technology that powers applications that have complex search features and requirements.

The setup we are going to do will require a lot more memory than the default 1GB vagrant has allocated for our virtual machine. To change this open the Vagrantfile config file and uncomment and change the amount of available memory:

  config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
vb.memory = "4096"
end

Now shutdown and restart your vagrant machine using the vagrant halt and vagrant up commands.

Back to our ElasticSearch installation…

It requires JDK 8 so let’s start with this:

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer

Next, to get ElasticSearch running, download the latest release from https://www.elastic.co/downloads/elasticsearch and install it:

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.2.deb
sudo dpkg -i elasticsearch-5.5.2.deb
sudo systemctl enable elasticsearch.service

Now start it:

sudo service elasticsearch start

To check that it works you can use:

curl -X GET 'http://localhost:9200'

This should return something like this:

{
"name" : "IiQtZOX",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "uodbxuW0SXC6biMEg8KamQ",
"version" : {
"number" : "5.5.2",
"build_hash" : "b2f0c09",
"build_date" : "2017-08-14T12:33:14.154Z",
"build_snapshot" : false,
"lucene_version" : "6.6.0"
},
"tagline" : "You Know, for Search"
}

Integrating ElasticSearch in Symfony

To help us use ElasticSearch we will install another bundle, the FOSElasticaBundle.

composer require friendsofsymfony/elastica-bundle

Enable the bundle by adding the following line in the app/AppKernel.php file of your project:

Now, to configure the FOSElasticaBundle, add the following to the app/config/config.yml file:

To populate the search index run:

php app/console fos:elastica:populate

This should add all your existing jobs into the ElasticSearch index. You can check this by running the following command to list some of the indexed jobs (10 is the default limit):

curl -X GET 'http://localhost:9200/app/job/_search?pretty'

FOSElasticaBundle provides listeners for Doctrine events for automatic indexing so our job is only to configure it, then let it do it’s job behind the curtains.

Searching

To implement the search we will add a new action in the JobController.php file:

We also need a new search.html.twig template:

Last thing you need to do is to update the search form found in the base.html.twig template:

That’s it! You now have a functional search engine on your site… with a small problem: you only get max 10 results and there is no pagination.

Paginating the Search Results

To paginate the search results we will need to install two more bundles in our project: the WhiteOctoberPagerfantaBundle and the KNPPaginatorBundle:

composer require white-october/pagerfanta-bundle
composer require knplabs/knp-paginator-bundle

As always, enable the bundles in the AppKernel.php file:

Now change the search action to use the new paginator service:

We also need to add the page navigation to the search template:

Now we’re really done!

Tests

To test our new search action add the following to the existing JobControllerTest.php file:

Run the tests with the following command:

phpunit -c app/ src/AppBundle/Tests/Controller/JobControllerTest.php

Everything should be fine.

That’s all for today. Find the code for this day here: https://github.com/dragosholban/jobeet-sf2.8/tree/day16


About the Author

Passionate about web & mobile development. Doing this at IntelligentBee for the last 5 years. If you are looking for custom web and mobile app development, please contact us here and let’s have a talk.