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.

CloudwaysCDN — a powerful solution that offers superior performance and satisfied global audience for your business. Read More

How to Use Elasticsearch With Yii 2

Updated on  19th July

3 Min Read
Reading Time: 3 minutes

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.

Share your opinion in the comment section. COMMENT NOW

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

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

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

THERE’S MORE TO READ.