Build search with Laravel Scout and Meilisearch
Today we're launching the first version of company search on Employbl. Not trying to sound sales-y. It's relevant to Laravel Scout and Meilisearch. We've got a list of companies in a database with tables for supplemental information. We want a fast search experience for a non-trivial database of companies. We have ~1,200 records of tech companies mostly centered around the SF Bay Area but others around the United States. In the last post I shared how we connected Employbl to Diffbot. This could allow us to pull in a lot more data about tech companies and startups in the United States and around the world. We want search to be fast and we want to allow users to slice and dice the data by various fields. Enter Meilisearch Facets. There's a lot of docs on getting up and running with Laravel Scout but not a lot that can really due justice to the power of Laravel Scout with Meilisearch in production. We host using Docker containers on Render. They've got a supportive slack community. So far we've got a Laravel server, Postgres database, Redis service and Meilisearch server all running on their platform!
Host and Deploy Meilisearch in production
We used the meilisearch-laravel-scout driver to get Meilisearch and Laravel running together. We launched a "service" on Render, one of their products $7/month to be run in conjunction with our server "service" that we have running $7/month. Getting the Meilisearch service deployed to Render required this Dockerfile that's 2 lines long:
FROM getmeili/meilisearch:latest
EXPOSE 770
It wasn't too bad. These services are new. There are lots of alternatives to the stack we're using. Especially Elastic.co and Algolia. Laravel Forge with Digital Ocean, Fortrabbit and a few others are ways to host your site. I think Render runs on AWS but I'm not sure. They have a Render v.s. Heroku comparison here in case you're interested in evaluating :)
Add Companies to a Meilisearch Index
Our companies are stored in our Postgres database. We have to get them into Meilisearch. Meilisearch has these things called Indexes that are comparable to tables in the relational database SQL world. To add them from the postgres table to a meilisearch index is one command with Laravel Scout:
php artisan scout:import "App\Company"
Our Companies model has a special method called to searchable array like this:
public function toSearchableArray() : array
{
$company = $this->toArray();
$company['_geoloc'] = [
'lat' => $company['latitude'],
'lng' => $company['longitude'],
];
unset($company['created_at'], $company['updated_at']);
unset($company['latitude'], $company['longitude']);
return $company;
}