Is it worth it to learn Elasticsearch

Full text search using Elasticsearch & PHP

September 11, 2020
  1. Home
  2. »
  3. Blog
  4. »
  5. Full text search using Elasticsearch & PHP

Full text search using Elasticsearch & PHP

The full-text search is part of every modern website, whether blog, forum or online shop. Unfortunately, the functions that MySQL offers with regard to the full-text index are rather limited and you quickly reach your limit. The functionality of the MySQL full text index unfortunately has certain peculiarities that certain words cannot be searched for, the sorting of the results is usually poor and it quickly happens that search queries become very slow.

If you want a really good search for your website, I can recommend Elasticsearch.


Elasticsearch is a free search engine server. Similar to MySQL, you start the server and then add documents to it. It doesn't matter what kind of documents these are: They can be blog posts, entries in a forum, products in an online shop.

Elasticsearch then offers extensive possibilities to search through these documents. The search options are far beyond the scope of this article; I can recommend the very good documentation from Elasticsearch there.

The big advantage of Elasticsearch over MySQL is:

  • Elasticsearch is very fast. Even millions of documents can be searched in a few milliseconds
  • Extensive search options: Searching for words, word combinations, partial words and the beginnings of words is no problem. You can also set extensive filters, e.g. search only the posts by a specific user, only search products in the 'Technology' category, etc.
  • Sorting the results: Elasticsearch already sorts the results according to relevance, i.e. it tries to display the best hits at the top of the list. To do this, it analyzes all documents in the index as well as the search input.

For me, Elasticsearch is the first choice when it comes to offering a comprehensive, good and fast search in an application. I'm not alone with this assessment: As listed in this blog article, various large companies use Elasticsearch, including Xing and Github. ES can also be used for an online casino bonus immediately without a deposit.

Start the Elasticsearch server

With shared web hosting, it is unfortunately difficult to operate an Elasticsearch server, almost no provider offers this. But if you have your own server or vServer, you can easily start and operate an Elasticsearch server there. This is accessed in PHP using the REST API, i.e. it is not a problem if the Elasticsearch server is operated on a server other than your web server / MySQL server.

How to install and start Elasticsearch can be found in the documentation. I also assume that you have an ES server running.

Personally, I like to use Docker to start Elasticsearch. You can find out how to do this here.

You can determine whether the Elasticsearch server has started correctly by calling http: // localhost: 9200 /. The result should look like this:

Control Elasticsearch using PHP

The Elasticsearch server is controlled using the REST API, i.e. normal HTTP calls are used to add documents to the server or to search for them.

I can recommend elastic / elasticsearch-php as a useful tool. Installation is correspondingly simple.

Index documents using PHP

The first step is to index documents using PHP. This means that we add our text documents to the ES server so that it can search through them.

A minimal example of indexing documents looks like this:

useElasticsearch \ Client Builder;
require'vendor / autoload.php ';
$ client = ClientBuilder :: create () -> build ();
$ docs = ["These are various sample documents, all of which are indexed in ES.", "The documents can be short", "Or they can also be very long and contain thousands of words.", "This does not constitute anything Problem for ES. "];
for ($ i = 0; $ i
'index' => 'test_index',
'id' => $ i,
'body' => ['text' => $ docs [$ i]]
$ response = $ client-> index ($ params);
echo "



We can determine whether the indexing worked by calling this URL: http: // localhost: 9200 / test_index / _stats

Here we can see that there are 4 documents in the index.

Search Elasticsearch We can check the ES search quite simply by calling up the following URL: http: // localhost: 9200 / test_index / _search? Q = documentsHere we search the test_index with the search query Documents. In response, we get two results, in which the word


was found.
In PHP we can search Elasticsearch as follows:
useElasticsearch \ ClientBuilder;
require'vendor / autoload.php ';
$ client = ClientBuilder :: create () -> build ();
'index' => 'test_index',
'body' => [
'query' => [
'match' => [



'text' => 'Documents'

$ response = $ client-> search ($ params);

additional Information

As mentioned, Elasticsearch is very powerful and unfortunately not always easy to use at the beginning. The effort can be worthwhile, however, because with ES you get a very good, extensive and, above all, fast search. If the search function is a crucial point in your application, e.g. in a large online shop, the effort is definitely worth it.
As a comprehensive tutorial on Elasticsearch, I can recommend the documentation: Getting started with Elasticsearch