Work Laravel 5.3 Scout + Elastic Search

Laravel 5.3 Scout + Elastic Search

24th Oct, 2016 | Work

One of the new features introduced in Laravel 5.3 is the rather brisk Laravel Scout. He is quick, easy, convenient and bloody good. As part of my series of posts covering creating a real application in Laravel 5.3 and VueJs 2.0, I will show you how I got my search working using Scout and Elasticsearch.

Laravel Scout is a package that enables full-text search in Laravel, allowing you to play with a variety of full-text search providers such as Algolia and Elasticsearch. Many people tend to go the Algolia route but as I have been using the free and open-source Elasticsearch, I thought I would continue with the same for this project. It being an optional package, you have to install it as per the instructions in its section of the Laravel documentation. Onto that in a minute.

What the documentation doesn't cover is how to get the various drivers working. For Elasticsearch, it is a little trickier since you have to set up your Elasticsearch server first and then use Scout to connect to it and run the errands. So let us install it on a Windows machine (instructions valid for version 2.4.1):

  1. Head over to the download page of Elasticsearch here
  2. Once you have downloaded and uncompressed the archive, move it to a location of your choice (e.g. C:\Program Files\elasticsearch-2.4.1)
  3. Install (or update to) the latest version of Java Development Kit (JDK - JRE is not good enough). At time of writing, it's version 1.8.0_112 (
  4. Add the new version of JDK to your JAVA_HOME environment variable:

  5. Head back into your Elasticsearch directory and run the command to create Elasticsearch service (see
  6. Once all is set up, verify Elasticsearch is running by visiting http://localhost:9200 - if all is well, you will see such information:

At this point, we are ready for Laravel Scout. Follow the instructions in the documentation to get it installed via composer. You will need laravel/scout and elasticsearch/elasticsearch packages. The rest from here is self explanatory:

Add all subscribers to the index:

php artisan scout:import "App\Subscribers"

And then you can add this line of code in the Subscriber model for searching:

 * Get search results
 * @param $search
 * @param int $paginate
 * @param int $trash
 * @return mixed
public static function getSearchResults($search, $paginate = 1000, $trash = 0)
    return static::search($search)->where('is_deleted', $trash)->paginate($paginate);

That's it! Told ya, easy!