Search is hard, unless you’re using Algolia.
Search is hard
Look, sometimes it’s enough to slap Google Custom Search onto a website and call it a day. For simple content sites where you just need the basics, that can be the easiest way to go about it. Or raise your hand if you’ve rolled your own using some variation of
LIKE '%query%'. It’s OK, we’ve all done it.
Or is it?
We recently launched an app that needed more robust search capabilities, so we decided it was time to step up our search game. That’s where Algolia comes in.
The basic idea behind Algolia is that you upload your searchable data to their servers as JSON (in the structure of your choosing). You then use their API to query your data and get back lightning-fast results!
Algolia also has extensive controls for tuning your search results. You can specify which attributes to search in your data, how to rank results (for example, by count), etc. Plus you get typo-correction for free (and the typo tolerance is completely configurable).
Like peanut butter and chocolate
Our backend was written in PHP using Laravel and, hey-look-at-that, they have a Laravel integration!
The Laravel integration is fantastic because it attaches directly to your Models so your indices get updated automatically.
Let’s look at a simple example
That’s it! That’s all you have to do. Now, every time you save your Person Model, it will automatically send all visible attributes of that object to the Algolia index defined in config/algolia.php. You can even have a model store to multiple indices by listing them in the
Let’s say you want more control over the attributes sent to Algolia. You just need to add a
getAlgoliaRecord() method to your Model.
In the above example, we’re sending all visible attributes of the Model (that’s the
$this->toArray() part) and we’re adding some additional fields based on Eloquent relationships (addresses & contacts).
Every Algolia index item needs to have a unique key value. The default behavior of the Laravel integration is to use
$this->id as the key, which is fine if you’re certain everything in the index is going to have a unique id.
In our case, we were putting lots of different items of different types into a single index so there was potential for ids to overlap. You can work around this by implementing the
getKeyAttribute() method in your Model.
We took the approach of assigning a “type” to each index item. For example:
It’s also super easy to filter search results using Faceting when each index item has a type.
There are lots of other Laravel configuration options in the documentation. It’s also worth reading through their other docs to see what else you can do.
Until next time…
That’s a very brief overview of a very powerful tool. Go check out http://algolia.com for more details.
Hope you “found” this useful (yeah, I went there).
(Note: We’re not getting anything from Algolia to write this. We just like them.)
Wireframing vs. Prototyping and Everything In-Between
By:Morgan Plappert on 7/14/2020
Prototyping demonstrates ideas, and baking prototyping into your workflow can begin to shift the way you think and approach design problems as a whole.Read More »
The Internet's Bubble Wrap: Your 404 Page is More Important Than You Think
By:Morgan Plappert on 9/13/2017
Hand over that half-empty glass and let us show you a few examples of how to turn possible frustration into opportunity.Read More »