How to Configure Search With Elasticsearch in Yii 2

by Ahmed Khan  August 2, 2016

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

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...

  • Judd Bundy

    I would like to see an example with a compound query if you don’t mind providing one.

  • Alex

    Hi Ahmed, thanks for this article, I think it’s a great starts for beginners. But can you please share how to index database table and create autocomplete with Yii2 and elasticsearch. I’ve searched all over the internet but couldn’t find any article on this.

  • Alex

    Hi Ahmed, thanks for this article. Can you please suggest any article covering how to create autocomplete using Yii2 and elasticsearch.