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 Configure Search With Elasticsearch in Yii 2

Updated on  2nd August

4 Min Read
Reading Time: 4 minutes

In this tutorial, I will discuss how to search data in Elasticsearch in Yii2. In order to properly test the code, Elasticsearch database must have some sample data. To learn how you could add sample data, please refer to my previous article. For this demonstration, the sample data consists of articles on different topics.

yii2 elasticsearch integration

Let’s get started by creating a new model that will be used for searching the data. The name of the model is Search.

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

Step 1: Creating Model for Search

The Search model extends from Elastic model which I created in the previous article. Now paste the following code in it.

Let’s understand the above code.

I started with Elasticsearch Query Methods that come with Yii2 Elasticsearch extension and combined it with QueryBuilder to build the search query that searches for the content inside the body of the article.

Next, I send the query for search with search() method. The method returns the result inside a dataprovider. To know more about the search in Elasticsearch, please go here.

Step 2: Updating the Controller of Elasticsearch

Now let’s update the controller of Elasticsearch. I will use the same controller that was used in the previous article.

First, actionIndex needs to be updated, so that it can render an index page with a form for search. The update will also create a new action actionSearch, and that will search user queries in Elasticsearch database.

Replace the controller code with the following:

Step 3: Creating Views for Search

Now go to your views folder, create a new folder and name it elastic.

First, create an index page for it. Create a new file inside elastic folder and name it index.php. Paste the following code in it:

The active form page will look something like this.

image01

Now, let’s create a page which will show the search results. Create a new file in elastic folder and name it search.php. Paste the following code in it:

The hard work is now over! It is now time to see the code in action!

Related: How To Setup Elasticsearch With MySQL

Step 4: Searching in Elasticsearch

To check the code, first route the Yii site to elastic/index.

Next, search something in it.

image00

The result page would resemble:

image02

Conclusion

In this article, I have discussed how to build and use search query inside Elasticsearch using Yii2. If you need clarifications or would like to contribute to the discussion, please leave a comment below

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.