Search is hard, unless you’re using Algolia.
By:
Mark Biek
on 11/17/2016
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!
They’ve got API clients for most major languages, plus some handy JavaScript libraries for instant-search and auto-complete UI.
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 $indices
array.
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.
Where next?
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.)
Related Posts
4 AI Resources To Add To Your UX Design Toolbox
By:Morgan Plappert on 7/18/2023
These AI-powered tools can help you streamline your workflow and empower you to take your designs to the next level.
Read More »Component-Based Web Design
By:Morgan Plappert on 8/9/2024
Designers and developers are always looking for ways to make handoff easier, collaboration more seamless and our processes better aligned. Automation, consistency and efficiency, without compromising creativity. It’s a constant battle and often times, a balancing act.
Read More »