DEV Community

Cover image for Searching for users with Laravel Scout and TNTSearch
Nenad Ticaric
Nenad Ticaric

Posted on • Updated on • Originally published at tnt.studio

Searching for users with Laravel Scout and TNTSearch

In almost every web application, a common requirement is a search feature.
Clients are spoiled by Google and other search engines and expect a powerful search experience in their products. They don't realize the complexity behind it and just want it to work. Like they are used to.

This tutorial will cover how to search your users table and sequentially make yourself or your clients happy.

It's most likely that your application has a users table, and the client wants you to search over all the fields it contains. You'll probably come up with a solution that looks like this:

This will certainly work for one keyword, but you'll face problems when you try to search for multiple keywords like Taylor Otwell. So you dig deeper into your toolbox and write something like:

As you can see, this will cover the case Taylor Otwell but will have several downsides. This approach doesn't scale very well. If the wildcard % operator is both on the left and right side of the query %query%, the database's internal index cannot be leveraged, which means that the DB engine needs to go through every single row to see if there's a match. As you may know, that isn't nice... not to mention slow!

If you introduce more fields, you'll have even more permutations in your code. You'll end up with a non performant and non readable query.

The solution to this very problem comes in a package written in pure PHP that deals with this stuff and lets you do some cool things.

Laravel also introduced a driver based solution for full-text search which works nicely with TNTSearch.

Requirements

Lets see what the requirements are.

  • Composer
  • PHP >= 7.1
  • PDO PHP Extension
  • SQLite PHP Extension
  • mbstring PHP Extension
  • Laravel Framework 5.6+
  • TNTSearch
  • Laravecl Scout

This is a pretty standard stack, and most of the hosting providers will come with all of the requirements. If not, you may consider switching to Digital Ocean

Installation

The installation process is easy and can be done in a couple of simple steps

composer require teamtnt/laravel-scout-tntsearch-driver

After that, add the service provider to app/config/app.php:

Add SCOUT_DRIVER=tntsearch to your .env file

Then you should publish the scout.php configuration file to your config directory

In your config/scout.php add:

Creating the index

The first thing is creating the index so that we can search against it in milliseconds. You'll be amazed how fast it is.

We'll create a laravel command that will do the indexing for us:

Once we have this command, we can run php artisan index:users. This will create a file in your
storage folder called users.index. Now we are able to do queries against it.

Another approach, and a more common one to achieve the same thing, is to add a searchable trait to your model:

And then running:

php artisan scout:import App\\Post

That's it. The index has been created.

Performing a search

Performing the search is simple. We'll do this in our UserController:

This controller assumes that you have a simple search form which submits a query in
an input field called q.

Updating the index manually isn't necessary because scout will do it automatically.

What about scaling you may ask? Will it handle more than 100k users? Don't worry, it will scale fine even if you have millions of users. The index itself is a sqlite database and is optimized for millions of records.

Conclusion

As you saw from the tutorial, adding a full-text search to your project is pretty straightforward. We covered only the tip of the iceberg here. TNTSearch is a powerful engine that can do a lot of stuff - even classification if that's something you might need. For more info, you can check out the GitHub documentation.

We also created a cool demo for you so that you see the engine's power in action. It's an airport search that displays the results directly on the map in real-time.
If you like it, leave a mention on twitter, and we might create a tutorial on how to do it.

This article was originally published on tnt.studio

Discussion (0)