Using Elasticsearch with PHP a simple guide

Elasticsearch with PHPElasticsearch is an open-source full-text search engine which allows you to store and search data in real time. You can search for phrases as well and it will give you the results within seconds depending on how large the Elasticsearch database is. This engine outputs results faster than SQL and is one of the most used search around the web. There are two ways you can use Elasticsearch with PHP; one with using curl and the other by using official client of Elasticsearch for PHP. In this tutorial I will show you how to use Elasticsearch using its PHP Client. Before we start, make sure that Elasticsearch is installed on your local machine. Here’s how you can do it:

Installing Elasticsearch for PHP

We will install it using composer. In your localhost root folder, create a new directory and name it elastic_php. In the same directory, create a new file composer.json and paste the following text in it:

Run composer install. Now that we have have installed it, let’s connect it with PHP.

Connecting Elasticsearch with PHP

Now create a new PHP script file and name it index.php inside the elastic_php directory and paste the following code in it:

Save it and run the script. Make sure Elasticsearch is running or you will get a connection error.

Indexing data in Elasticsearch

Now that we are connected to Elasticsearch, let’s index some data in it. Open your index.php file and change the following code in it.

When you run the above code you will get 1 as output.

Getting data from Elasticsearch

Now we have an index, let’s get data from it. Now replace the code with the following in your index.php and run it:

You will get the field that we have added.

Adding My First Field In Elasticsearch

Searching in Elasticsearch

Let’s search for some data in Elasticsearch. Now open your index.php file again replace it with the following code.

Now let’s understand the code a bit. When we search for our query in Elasticsearch it returns with a lot of results including our specific query result. We need to fetch our result from it and for that, first we need to check how many results we have:

Then we fetch our results from it which will be less than its hits because it’s an array.

 After that we print the result in a browser. Now run the code provided above and you will get the following result (again):

Adding My First Field In Elasticsearch 


In this tutorial you learned how to connect Elasticsearch with PHP and how to save and retrieve data from it. You also learned how to perform a search with PHP and Elasticsearch. If there is anything that confuses you, please leave a comment below and we’ll come up with the solution for you!

About the author

Ahmed Khan is the PHP Community Manager at Cloudways and writes about MySQL and PHP. An experienced professional in PHP related topics, Ahmed loves to spend his free time watching The Flash, Game of Thrones and reading DC Comics. Follow him on Twitter and Facebook to keep up with his work and insights on PHP hosting.

5 Responses

  1. Hi Ahmed,
    nice and simple tutorial!

    I’m missing some information in your tutorial, you’re using in your $params array a “type” value. Without checking the Elasticsearch manual, I would say that this parameter has some specific function. Maybe you can explain how to use that parameter?
    Is this a free parameter or are there default type values too? And how/where is this type parameter used?

    Next you’re using two different values for the “index” parameter (indexing and getting function). Is this necessary or just a typo?

  2. Hi Pete,

    Glad you like it. As you see it a simple tutorial for beginners “$param” is just an array type variable which is using to perform Elasticsearch queries.
    Yes! “index” are different because when we are indexing ID is indexing auto, you can define your own id though. And when we are getting we are fetching only 1 data from elasticsearch using ID. That’s why it is different.

  3. Hi Ahmed, thanks for you comment.
    Looks like my question wasn’t clear enough :)
    I’m talking about this type:
    'type' => 'my_type'
    What is the function of this parameter?

    The next one is not about 'index' => 'my_index' but about 'id' => 'my_id2' and 'id' => 'my_id'
    Both are different and I’m wondering why. Maybe you can give an example for each parameter.

  4. Hi Pete,

    Thanks for clearing me out. :) Type in ElasticSearch is used to define types of the index that are being indexed in the specific node. It’s a predefined keyword. As can be seen here.

    Not a function but can be treat as a predefined keyword.

    Yup! they different and that’s not a typo for me as I want to tell that you can get the index from ID. Yes, I could add `’id’ => ‘my_id’` in the second part too. I just did this to feel different from indexing and fetching. :)

    In this tutorial, I am only teaching how to connect Elasticsearch with PHP, but not what is Elasticsearch :) To understand this tutorial I think one should know what is ElasticSearch :)

  5. Pete,
    sure this is a beginners tutorial and that’s the reason why I ask. The additional information you’ve provided in your last comment was just missing. For example, if someone is trying to use your example for his own application, he (or she) needs to know how to set these parameters.

    A beginner tutorial doesn’t mean that you keep your readers stupid :D
    Thanks again!

Leave a Reply

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

Don’t post your code here!
Post your code block or snippet to pastebin and include the pastebin URL in your comment.

I delete all comments with non related links inside the comment text. Keep your comment related to the topic, if your question is off-topic, please use the contact form instead.