Install Elasticsearch with Symfony in 3 minutes

Share Button

Lire la version française

This article is the second one about Elasticsearch.
Its goal is not to present what is Elasticsearch, you can read more about it on other blogs, like this one (in French). But it will allow you to get to the heart of the matter, and to understand the following articles.


The most important : Elasticsearch is a search engine (that indexes documents), open source and clustered. It’s based on Apache Lucene, also used by Apache SolR and developed in Java.

Its main features we are interested in :

  • Indexing via filter/token (for example synonyms, worddelimiter, …)
  • Search, sort, pagination
  • Facets browsing
  • Results aggregations / Statistiques (since the 1.0.0-beta versions)

We had the nice surprise to find a PHP library, Elastica, that created a link between PHP and the Elasticsearch server (via REST and cURL), nut also a Bundle (FOSElasticaBundle, formerly known as FOQElasticaBundle) that makes starting a project easier.

Install Elasticsearch 

You can install Elasticsearch from a package! Which is easier and more integrated than before : http://www.elasticsearch.org/downloads

Download it and install it. Once the installation is over, Elasticsearch is installed as a service : 

$ service elasticsearch status

You Elasticsearch instance is now working perfectly. You can configure it via the yml file : /etc/elasticsearch/elasticsearch.yml.

Install the plugin head

We advice, as anybody, the installation of the plugin “head”. This plugin allows you to :

  • See your indexes, your instances : the status of your server 
  • Browse your documents
  • Run queries (simple or complex). Which will be very useful when debugging!

We are going to install it as a plugin. To do it, go to your root application folder :

$ cd /usr/share/elasticsearch
$ sudo bin/plugin -install mobz/elasticsearch-head

Here we are, now go to : http://localhost:9200/_plugin/head/ to see the plugin in action.
You must to know how to use 4 tabs :

  • Overview : The status of your cluster (and all the linked nodes). You can see your indexes and the total number of documents. A “Yellow” Cluster is compound of a unique node, and so, not replicated.
  • Browser : Allows to browse your documents, with the help of useful filters. It’s particularly useful to see your indexing in details
  • Structured Query : A wizard that helps you to create queries and view the results
  • Any Request : A raw queries interpreter which will be useful to understand how the queries sent byt the FOSElasticaBundle work.

To know more about the plugin, see the Github repository : https://github.com/mobz/elasticsearch-head

 Install Elastica and FOSElasticaBundle on Symfony

Elasticsearch is now installed and ready to index its first documents. For that, you must install the FOSElasticaBundle and add it to your composer.json file.

{
    "require": {
        "friendsofsymfony/elastica-bundle": "3.0.*@dev"
    }
}

and do not forget to register the Bundle in the AppKernel.php file:

public function registerBundles()
{
    $bundles = array(
        // ...
        new FOS\ElasticaBundle\FOSElasticaBundle(),
    );
}

Here we are, the library (Elastica) and the Bundle (FOSElasticaBundle). You can now read the next post which will explain how to (well) index your first document.

We have also written an article about the Elasticsearch configuration but some elements from this post will be completed in next articles.

Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Protected by WP Anti Spam