How to Use Elasticsearch With Yii 2

by Ahmed Khan  July 19, 2016

In my previous article about elasticsearch, I discussed how readers could use elasticsearch with custom PHP sites. In this article, I will talk about using elasticsearch with Yii and Yii2. Let’s get started.

elasticsearch in yii2

Step 1: Installing Elasticsearch in Yii2

Go to basic folder and open composer.json. Add the following line in the require section.

Add ElasticSearch Yii2

After adding the line, run the following command in the terminal, inside the basic folder.

Add ElasticSearch Yii2

Elasticsearch extension is now installed on the system. In order to verify it, open the main page of Yii2 in the browser. Open the Yii2 developer toolbar and in the configuration scroll down till you find the installed extensions heading.

Add ElasticSearch Yii2

Look for the newly added extension i.e. elasticsearch. if you could see it, you have successfully installed it.

Related: How To Host Yii 2 Framework On Cloudways Using Composer

Step 2: Configuring Elasticsearch

Now that the extension have been installed successfully, let’s configure it. Now head to config folder and open web.php file. In the file, add the following lines inside ‘components’ before ‘db’ and save the file:

Now that elasticsearch has been configured with Yii2 let’s use it.

integrate ElasticSearch Yii2

Step 3: Using Elasticsearch with Yii2

In order to to use elasticsearch, you need to create a Model class for elasticsearch first and extends it from \yii\elasticsearch\ActiveRecord.

Create a new class inside the model folder and name it elastic. Paste the following code in it.

The above code resembles ActiveRecord for accessing a database. Now let’s create a Controller and insert a new record in elasticsearch. Inside Controller folder, create a new folder and name it ElasticController. Paste the following code in it.

Insert() inserts new data in elasticsearch record. Let’s call up this controller in the browser and see if it works. In my previous articles on Yii2, I have teach user how to call different controllers. If you need a refresher on the topic, check out my getting started guide for Yii2..

Now run the controller in your browser. If you see the following message:

Then you have successfully added the data. It also means that elasticsearch is working perfectly with Yii2.

What More?

Elasticsearch don’t ends here. Read the complete  Elasticsearch API guide and implement all the possibilities that elasticsearch can do.  If you need further clarification about the topic or have something to contribute, please leave a comment.

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

About Ahmed Khan

Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works. You can email him at ahmed.khan@cloudways.com

Stay Connected:

You Might Also Like...

  • Thanks for the tutorial. I am missing everywhere the zero downtime full reindex for Yii2. Maybe that is something you would like to tuto one day.

    Also, don’t I miss the index name on the Active Record? Just asking.

    • Hello Antonio,

      Thanks for taking out sometime to go through my article… Yes, Antonio, I missed it intentionally because as far as my knowledge is concerned, if you don’t define index in Elasticsearch, it uses default index which is already pre-defined in the Elasticsearch.yml file.

      However, if you wish to define your own index, navigate to model folder and add the following functions within your elastic model.

      public static function index()
      {
      return “your elasticsearch index”;
      }

      public static function type()
      {
      return “your elasticsearch type”;
      }

      Hopefully, this bit of information helped you out with your query.

  • masih

    Thanks for this tutorial. How I can add mapping and settings to the model? How I can use this setting in query?

  • Gabriele Carbonai

    hello for use elasticsearch I need to install something in my server ?

    • Hi,

      If you are hosting your site on Cloudways, you can follow these steps to install Elasticsearch through 1-click
      To do so, follow these guidlines:
      1. Login to Cloudways Platform
      2. Select the server in which you want to install Elasticsearch.
      3. From the left side menu select Settings & Packages
      4. Now Select the Packages Tab
      5. Now Click the Install button in front of Elasticsearch, and there you go, your server is installed and running.

      If you are hosting your site somewhere else you need to ask your hosting provider to setup elasticsearch for you separately.