X
    Categories: Learn PHP Tutorials, Tips And GuidesYii Tutorials, Tips And Guides for beginners

How to Use Elasticsearch With Yii 2

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.

Step 1: Installing Elasticsearch in Yii2

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

"yiisoft/yii2-elasticsearch": "~2.0.0"

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

composer update

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.

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:

'elasticsearch' => [
    'class' => 'yii\elasticsearch\Connection',
    'nodes' => [
	['http_address' => '127.0.0.1:9200'],
// configure more hosts if you have a cluster
    ],
],

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

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.

<?php

namespace app\models;

use Yii;

class Elastic extends \yii\elasticsearch\ActiveRecord
{

    public function attributes()
    {

        return['name', 'email'];

    }

}

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.

<?php
namespace app\controllers;

use app\models\elastic;
use yii\web\Controller;

class ElasticController extends Controller
{

    public function actionIndex()
    {

        $elastic        = new elastic();
        $elastic->name  = 'ahmed';
        $elastic->email = 'ahmedkhan_847@hotmail.com';
        if ($elastic->insert()) {
            echo "Added Successfully";
        } else {
            echo "Error";
        }

    }

}

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.

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