Implementing ElasticSearch in a Laravel app/site
I will assume that the reader of this tutorial is very conversant with laravel framework.
I am using Laravel 5.4, windows 8.1, MYSQL database, bootstrap3 and Elasticquent package, those are the important things for now.
First, get an installation of ElasticSearch with respect to your operating system. There is a MSI installer for windows, download it and install (follow the installation instructions here.
To ensure that the installation is successful. Run the elasticsearch.exe file from the installation folder: C:\Program Files\Elastic\Elasticsearch\bin.
You should see an output like these(run with administrative rights please) in the console and type this in your browser window: http://localhost:9200/?pretty:
Set up your Laravel app as usual, please refer to laravel docs on how to do that. Set up the database (MYSQL was used for this tutorial).
Open the composer.json file and add this required object and run composer update command:
You can check out the github page of this package, it also contains nice guide on setting it up.
open your config/app.php. Under providers add and under alias, add:
Run the command: php artisan vendor:publish — provider=”Elasticquent\ElasticquentServiceProvider”
This should create elasticquent.php file inside the config folder
Open elasticquent.php, edit these:
Set up the database table that we need using laravel migrate command: php artisan migrate:make create_workers_table
Locate the migration file (you know where to find that), and inside the up function, add the lines of codes meant for the database table columns. Run the migrate command: php artisan migrate
Create a model if you have not, this command should do that: php artisan make:model Worker. Inside the Worker.php file, you should find something like this, if you don’t edit yours to suit the context of this tutorial.
You can now add dummy data if you need so. I already have some data in my database.
To set up Elasticquent, edit our model (Worker.php) and add the following:
Eac mapping has a type and an analyzer. It can include strings, numbers, dates etc. Analyzer determines how elastic search stores your data for searching. I am using standard, it will remove HTML and grammar then index each word seperately. We have configured how we want our search to operate.
To index our database into elastic search, we need to create an Index first:
We use Laravel REPL to generate our elastic search data. From your CLI, type:
Worker::putMapping($ignoreConflicts = true);
putMapping() takes the mappin properties we set in the model so that Elastic search knows how to index all of our data.
addAllToIndex() takes all the data from database table and putts it into elastic search.
To view and verify that we are on the right path, type this in your browser or postman app: localhost:9200/main_index/workers/_mapping?pretty
You can now test a search using: localhost:9200/main_index/workers/_search?q=resident:fct&pretty
We now have to create the routes for all these things: edit your web.php file and add:
Make a controller: php artisan make:controller “WorkerAuth\WorkerOps”
and the function:
Now, create the view. I already have a blade file (searchworker.blade.php), you can go on and create yours. Add the search form first:
Finally, add a panel and the usual @foreach
To test out our work: check out the images below
Next tutorial, we will try to incorporate vuejs into it and see how it goes.
Thanks to this guy, his article provided a foundation for this tutorial.