Run Elasticsearch With Laravel on MacOS

Arie Visser • February 21, 2020

laravel php elasticsearch

Elasticsearch is a full-text search engine that is used by organizations such as Cisco, Adobe, eBay, Microsoft and thousands of others. With Elasticsearch you can search large amounts of documents extremely fast.

Laravel Scout is a driver based solution that provides full-text search to your Eloquent models.

In this guide I will show you how you can run Laravel Scout with the Elasticsearch driver on MacOS.

Install Elasticsearch

You probably already have Homebrew installed, if not, do this first:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Tap the Elastic Homebrew repository:

brew tap elastic/tap

Run the following command to install the most recently released distribution of Elasticsearch:

brew install elastic/tap/elasticsearch-full

If you want to start the Elasticsearch service at boot, you can use Homebrew Services. To install, run:

brew tap homebrew/services

Now you can start the Elasticsearch service, by running:

brew services start elasticsearch-full

Install And Configure Laravel Scout

Install Laravel Scout in your project, by using Composer:

composer require laravel/scout

Next, publish the scout.php configuration file:

php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"

Integrate Laravel Scout with Elasticsearch

Laravel Scout does not integrate with Elasticsearch by default. In order for this to work, you can install laravel-scout-elasticsearch, a really nice package that is developed to resolve exactly this.

composer require matchish/laravel-scout-elasticsearch

Add the ElasticSearchServiceProvider to the package service providers in config/app.php:

/*
 * Package Service Providers...
 */
Matchish\ScoutElasticSearch\ElasticSearchServiceProvider::class,

Publish the elasticsearch.php configuration file:

php artisan vendor:publish --provider="Matchish\ScoutElasticSearch\ElasticSearchServiceProvider"

You also have to set two environment variables.

The Scout Driver:

SCOUT_DRIVER=Matchish\ScoutElasticSearch\Engines\ElasticSearchEngine

The Elasticsearch host:

ELASTICSEARCH_HOST=localhost:9200

Create some data

Add the Laravel\Scout\Searchable trait to the model you want to search.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class User extends Model
{
    use Searchable;
}

To make sure the model has some records in the database, we will implement the UsersTableSeeder.

<?php

use Illuminate\Database\Seeder;
use App\User;

class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(User::class, 1000)->create();
    }
}

To create these 1,000 users, run the following command:

php artisan db:seed --class=UsersTableSeeder

This took 22.9 seconds (on my machine), since the records are immediately added to the Elasticsearch index.

Since you've added the Laravel\Scout\Searchable trait to the User model, all new records will be added to the index automatically.

If you would like to import already existing records into the search index, you can run:

php artisan scout:import "App\User"

Search

Searching is made easy with the search method, that is provided by the Laravel\Scout\Searchable trait:

$users = App\User::search('Mrs.')->get();

I've created a repository with a very small Laravel project where you can see everything put together.

This guide is very concise. To read more about Elasticsearch, read the official reference. Information about Laravel Scout can be found in the Laravel documentation,
and you can find more details about the Laravel Elasticsearch package on GitHub.