This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

How to Use Elasticsearch With Yii 2

Updated on December 8, 2021

3 Min Read

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.

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

Add ElasticSearch Yii2

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

composer update

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:

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

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.


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.

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 = '[email protected]';
        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.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Customer Review at

“Cloudways hosting has one of the best customer service and hosting speed”

Sanjit C [Website Developer]

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 [email protected]


Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!


Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Unleash The Cloud.
Not Budgets.

For 4 Months +
Up To 30 Free Migrations

Cyber Week

Time Left In Offer
  • 0


  • 0


  • 0


  • 0


40% OFF

On All Plans