Intro to Scout & Algolia0:00
Okay, let's take a look at Scout. Scout provides a simple driver-based solution for adding full-text search to your Eloquent models. Scout comes with a driver for Algolia, which we'll take a look at here. We'll take a look at another driver in a future video. The advantage of using Scout with a search index is that you get a better search experience with better performance and features like typo tolerance. So let's go ahead and install Scout on a blank project here. So let's composer require it, as always, and make sure to sign up for Algolia and generate a new API key.
Install and Configure Scout0:31
So let's composer require it, as always, and make sure to sign up for Algolia and generate a new API key. I already have one here, but if you don't, just go here to create application, and it should be done now. Okay, let's go ahead and publish the config here. Let's copy that and paste that in here. And now we want to add the searchable trait to any of our models that we want to make searchable in Scout, or in Algolia in our case. So we'll just work with the default User model here. So let's use searchable in User.
Make User Searchable1:07
So we'll just work with the default User model here. So let's use searchable in User. So let's go to our code, and let's go to our User model, and let's add searchable here. And that should import that. Cool. And let's also seed some data here. So I'm going to go to DatabaseSeeder, and let's uncomment this, and let's put 200 users in the database. Okay. And let's go ahead and php artisan migrate:fresh and add the seeder.
Okay. And let's go ahead and migrate fresh and add the seeder. Oh, yeah. So we have to install the Algolia client. That's the next step in the docs, which I didn't look at here. It's right here. So let's do that. Okay. And if you look at the docs here, you can also use queues, which you should if you're running in production.
Set Algolia Environment Keys1:57
And if you look at the docs here, you can also use queues, which you should if you're running in production. So any of our operations when we're syncing our data to Algolia servers will be put on the queue. But in our case, we'll just stick to not using the queue, so I don't have to set it up. Now, if you look at the config, so config/scout, you'll see some options here. But now we have to set our Algolia app ID and our Algolia secret here. So let's do that in our .env file. So let's add this and the secret as well. So let's grab this from our Algolia account right here.
So let's add this and the secret as well. So let's grab this from our Algolia account right here. So let's go to our API keys, which is right here. And let's grab the application ID and paste it in here. And the secret is this right here. So let's copy that and paste it here and save that. And let's make sure my migration with the seeds worked. So let's go into our database. And that did not work. So let's run php artisan migrate:fresh --seed again.
Customize Indexed Data3:11
And that did not work. So let's run php artisan migrate:fresh --seed again. And now that should have worked. So now I should have 200 users in my database here. Okay, so the next step is to synchronize our data with whatever driver we're using, in this case, Algolia. But before we do that, there are a few options here. So if you want to change the index name, so the index is basically a table, you can override this method in your model. But the default is just the table name.
this method in your model. But the default is just the table name. So in our case, just users, which is fine. You can also configure what data you want to push up. So you can override this method. So let's try that. I'm going to copy this. I am going to go to my User model. And let's paste this down here. And say I don't want to push one of the columns to Algolia.
And let's paste this down here. And say I don't want to push one of the columns to Algolia. So say for example, we don't want to push updated_at. So because we already have this hidden field, so password and remember_token won't be pushed to Algolia, which is what we want. But if you want to configure that even more and not add other columns, say updated_at, we can do that in here. So we can do unset, and we can just say array and the column name. So updated_at. Okay.
Import and Test Syncing4:34
So updated at. Okay. So save that. And now what's next? You can even do the same for the model id. But we'll just leave it to default. So now let's import our users to Algolia's servers. So we can use this command. And this will push it to Algolia servers and we can see it there. So let's try this.
And this will push it to Algolia servers and we can see it there. So let's try this. php artisan scout:import changes to User. And it looks like it worked. If we check Algolia, I'm just going to refresh this. We can check our new indices or our new index, and we can see 200 users right here. So now Scout will observe whatever changes we make to our models and synchronize it with this index here. So I'm going to open up Tinkerwell here and create a new User. So User::create().
So I'm going to open up Tinkerwell here and create a new User. So User::create. Let's put me in there. So name, let's say Andre, and let's close that out. And let's run this. And we see we get ID of 201. Let's see if that's in our database. And we have 201 rows. And if I search or if I refresh the Algolia index, we have 201 and I should be in here when I search.
And if I search or if I refresh the Algolia index, we have 201 and I should be in here when I search. Cool. So let's go ahead and update that record and you'll see it update here as well. So let me just comment this out. Let's say $user is User::find(201), and we can just do $user->name. Let's change it to my full name. And we can do $user->save(). And this should update as well. Let's run that.
And this should update as well. Let's run that. Oops, sorry. And that should have updated in our database and also our Algolia index. So let me just refresh that. And there it's updated. Cool. And the same works for delete as well. So let me just delete myself. So I can just do php artisan user:delete here.
So let me just delete myself. So I can just do user::delete here. And this should delete the User from my database and also the Algolia search index. Okay. And now if I refresh, we're back to 200 and there are no results for Andre. So Scout also provides a search method which you can make use of. So this is if you're doing a basic search, the more interactive search requires a JavaScript library, which we'll take a look at in the next video. But if you just want to do a back end search, you can make use of the search method. So where is it here?
But if you just want to do a back end search, you can make use of the search method. So where is it here? Right here. Let's grab this. And it has all the features that Algolia provides, like typo tolerance, which is nice. So let's just paste this in here. Actually, let me make another User again. So let's say Andre again as 202. Okay. Comment this out again and paste that in.
Okay. Comment this out again and paste that in. Let's say users. And let's just say user search. And we'll search for Andre or let's misspell my name and let's see if we get anything. So before I do that, let's see what we get in Algolia when I search for that. So let me refresh. And if I search for A-N-D-E-R, we get me because it has typo tolerance by default and we get a few other results here. So one, two, three.
And go back here. Now, if I search for 202, it will not have any results. And let's go back to tinker here, and this should be empty now. And it is. Cool. So there you have it, a quick review of using Scout with Algolia and keeping your two data sources synchronized.
