YouTubevideo· Apr 25, 2025· 30:19· 1,900 views on YouTube
React at a moment you like. The best moments light up here.
Search is usually that one feature that you usually always need for most applications, but you usually put it off until the very end when you're like, "Hey, I actually need a way to search through this list of items or search through this content or description or a user to search across my whole entire application." So, what do you do? You usually just say, "Hey, let me just do a full text search in, you know, Postgres or SQLite or whatever database that you're using."
But, what happens when you actually need a little bit more capability, a little bit more features, and want to make it incredibly easy for you to set up within your application? Well, you've probably heard of Laravel Scout, which is a way to have full text search within your eloquent models without doing really much of anything.
And it ships with a bunch of different drivers out of the box. And so, this video isn't really meant to say, "Okay, hey, here's all the reasons you should use something that, you know, coexists with Laravel Scout and here's the reasons why you should just use a Postgres or a SQLite or some similar search functionalities within your application." This video is mostly just meant to show you, "Hey, here's the when that ease of use of setting up something within Laravel Scout makes complete sense." So, let me paint you a picture.
And this is what you would usually have the need for some kind of full text search capability when you have large data sets. And so, I kind of built out a demo application to show off some of these functionalities. But, I would look at uh some of these open source data sets that you can use on kaggle.com, which is fantastic.
It's basically just like, "Hey, give me a gigabyte or less or more, you know, hundreds of thousands, sometimes even a million different rows in a database, and now you get to figure out what to do with that." And it's great to have that kind of large data information.
I actually found this one uh that's this McDonald's store reviews and this is not sponsored by McDonald's, but this video is sponsored by TypeSense, which is one of the out-of-the-box drivers that is supported within Laravel Scout and of course thank you for TypeSense for sponsoring this video.
It's an open-source Algolia alternative and we'll dive into a lot of the features that TypeSense has to offer of course not all of them, but maybe why you might choose it over just having a simple search query in your eloquent model. I'll kind of just show how I got all this information in, what the application actually looks like right now and then we'll dive into okay, so how do we set up Laravel Scout?
How do we set up TypeSense to take us the rest of the way because yes, we have this application and it works okay, but it's not as good as it could be. So yeah, I have all these McDonald's store reviews. I think there's over 30,000 yeah, about 33,000 of them. It's just a bunch of text, a bunch of information that I got to put into the database and so if I pull up my code editor right here and I apologize for the light mode.
I'm trying something, but in my code editor I basically created this review seeder. I take that CSV that I downloaded from kaggle.com. Again, fantastic place if you want to test out some large data sets, but I crammed all the information, put it into this reviews.csv.
That's actually why I just downloaded from the site and then here's what we get to do. We just to say, "Hey, every time for each one of those row, I'm just going to create a new review model and that's what we're doing right here and right now." So with having 30,000 rows in my model, now I can actually uh demo and test out search functionality. It's a lot easier when you have those large data sets that you're actually testing that would be more akin to real-world scenarios.
So maybe you have an application, maybe unlike myself at least at this time, that you have a lot of those rows available and you're like, "Hey, I just want to push this into my production application." Well, yes, go for that. But if you just want to try this out, having the capability to do it with a whole bunch of data is always important.
So this is what that currently looks like. We just have this list of information. You can see there's 33,396 results down here. And if I bump that up, we're we're just all we're doing is just searching. And well, I'll show you the code right now in a little bit. This is outside of Laravel Scout. This is just saying, "Okay, I have all of these reviews. If I want to search for pizza, which I am surprised to know that there is a McDonald's, looks like, in Orlando that has pizza.
Um I'm a Taco Bell boy at heart and I have not seen pizza at McDonald's, but this has got me a little bit interested. Um and of course, stuff like this works great. Let's say I I only wanted to search for the review.
But if I wanted to search for pizza and ice cream, you know, it's not going to kind of split those apart. You have to deal with that in its own separate format. Or if I wanted to search for, I don't know, pizza Orlando, I'm not searching across multiple columns. I'm only searching in the review.
If I take a look at the code and we go into the index page, we're searching for uh we're grabbing all the reviews and then we're searching for the review and the store name. And I probably could have done without this. This was just kind of before I only was going to use the McDonald's data set. Uh but this is you know, you have to do this across multiple different columns.
And it's not going to be as fuzzy as you expect it to be. And when I mean fuzzy, I mean what we expect from fuzzy search, where I can type pizzas and I get pizza and any kind of maybe typo that someone might have put in that review.
And so, there's a couple of things, of course, that you could do just right now to still make this decently nice. You could add all of the columns, and I could make sure in my review model or my review migration, I should say. I don't actually have that that review as a as an index, which would help out with any type of speed if I was searching across 33,000 reviews like I am.
But we're still we still need more usually for most search capabilities, especially if this is a, you know, a command K kind of search across your whole application.
Or if this is more even AI or similarity search, then you have to think about, okay, what kind of vector data base am I thinking through or what kind of what kind of information do I want to be sent up to my users in order to search across multiple data multiple columns, maybe even multiple tables. Well, that's where Scout and Type Sense can come in.
So, why don't we get this set up and see what it has to offer outside of the normal capabilities that we might have within our demo application in its current form. So, like I said, Laravel Scout provides a simple driver-based solution for adding full-text search to your eloquent models. And so, what we have here and what we're going to be taking a look at today is the TypeSense driver. Why don't we take a look at the homepage? I love this.
They've had multiple iterations, but this is one of my favorites. TypeSense is a lightning-fast open-source search.
You can think of it as like an open-source alternative to Algolia. And the cool thing is, yes, they are open-source. You could just install this on like your Laravel Forge server or even locally. And I'm going to be using the Laravel Herd service, which is paid, so you can definitely follow the instructions if you don't have the paid version of Laravel Herd, but there's also the TypeSense Cloud, which if like you're hosting this maybe on Laravel Cloud, for example, you can just set up your API keys and you have connectivity
into TypeSense Cloud without having to worry about the actual install yourself. So, why don't we get this set up locally and then we can talk through more about what this might look like in production.
Now, we're already going through kind of that demo application. And the cool thing is TypeSense gives you a lot of demo application capabilities and even a sample data set that would be helpful for you if you didn't want to use the McDonald's one or something similar in building your own. Uh if you you can take out some of the live demos where they have like basically like recipe search, um XKCD comic search.
All of these things kind of show you the functionality and capability of TypeSense as well as what you might use it for and what might not. I love this page right here. Key use cases. Basically, okay, here's all the good use cases for using TypeSense. And here's all the use cases where you shouldn't use Typesense, which is awesome to see.
It's basically not supposed to be your primary data store. That's the biggest one. It's not good for application logs. But really what it's good for and what I think of it as is one, multi-tenant search. You have different records across multiple things, as well as any kind of like geo search or semantic search where you need something similar, as well as maybe latitude, longitude.
all of those variables in a McDonald's capability, so we would be able to hook that up as well, saying I want to search for something near me would be an interesting search query. But then we also have probably the easiest one to understand most of the time and the one that most people are going to be searching for is typo tolerant fuzzy search for autocomplete and search results.
When you're searching for maybe a bunch of different reviews about pizza, but you want to see like a pizza good or pizza bad and how many people are also searching for pizza across McDonald's reviews? Well, maybe a lot. I don't know. And you can see here even their homepage has a great example of just how Look how fast this is. When I'm searching for recipes of maybe a griddle, griddle recipes or maybe barbecue. It's just so fast.
It feels like it's all done locally and it's the way in that they, you know, they store all of this in memory so that you can search it fast and quickly. And we'll see how we can set this up within Laravel Scout. It's incredibly simple.
It's going to be awesome and thankfully not going to be too long of a video. Okay, so you can start off with installing Typesense. Of course, you can use Typesense Cloud. I probably would use this for production. And then for local machine, you kind of have a different bunch of different options. You can use Brew within Mac OS, for example. You can also use Docker. Uh, you can use a binary for Mac as well, and then there's some options for Linux as well.
I would say choose your pick. I'll have the link for all of this in the description as well. Choose your pick of what works best for you. I'm going to be using Laravel Herd, which has a services feature to kickstart TypeSense for me.
So, if I pull up Laravel Herd, and I go into services, I already have TypeSense running, and then I can just grab these variables and place them in my application so that I can start using TypeSense in the application. I'm just going to copy that to be ready, and then we can follow the Laravel docs for how to install TypeSense in our Laravel application.
It's as simple as requiring this TypeSense PHP, and then entering our variables, and I think we should be good to go. Then we're just going to start preparing the data. So, in my terminal, I'm just going to I don't need my environment variables yet. Uh, first I need to go ahead and copy this. So, we have composer require TypeSense TypeSense PHP. We're going to install that into our uh review search application, and then we're going to add our uh environment variables to our .env file.
So, I'm going to grab those right here.
Perfect. And then we should be good to go. So, now we can start doing what Scout kind of requests and what most of these full-text search drivers are going to be looking for. It's It needs to know what kind of models does it need to ingest. You're not just going to give it all your information to be able to search through everything, but you do need to tell it, "Okay, I want you to take care of my reviews file, and these are the specific columns I want to be able to search through." And that's what we need to set up. So, why don't we go back to the Laravel docs.
You can see here, preparing a data for storage in TypeSense. Your searchable models must define a two searchable array method that casts your model's primary key to a a string and creation date to a Unix timestamp. And I should say before we even do that, we need to make sure we install Laravel Scout as well because we installed the TypeSense PHP SDK, but we have to make sure we install the Scout underlying framework for this.
So, composer require Laravel Scout. Then there's going to be the ability to publish that config directory. And then here's where we add that searchable trait to our model. A lot of this is kind of service agnostic, which is great. But the good thing is TypeSense doesn't make this hard at all. There's only a couple of little things on top of this that we have to do.
We're going to publish those assets. And then we just need to add the searchable trait to the model that you would like to make searchable. It's as simple as that. And we're just going to add this to our uh to our review model. So, we have a review model and then we want to make this searchable. So, we're going to bring that in. Should be good to go there. And this is important. It's not required to use Scout with a queue driver, but this is helpful when you are kind of saying, let's say you have a new model, a new um let's say uh I I store a whole bunch of different reviews that I'm pulling in every night.
Well, maybe it happens more frequently for your specific searchability, you know, columns, your searchability uh rows in your database.
And so, with that, you probably want them to be queued to be sent to your TypeSense driver cuz you don't want to kind of hold up your application waiting for all that information to be in sync with TypeSense. And so, we can set that to that queue to true. Just for this demo application, I don't necessarily need it. And even if I was was a review search and production, I probably would, you know, add new reviews maybe once a day and not necessarily right when they're created.
So, I wouldn't necessarily need it, but it's always helpful to have. Okay, we installed the Typesense PHP SDK, we added our .env files, our variables to our .env file, and now we have to just prepare the data for storage. And so, like I said before, before I jump to the gun, you know, all models must define a two searchable array method that casts your model's primary key, in this case our ID, and the creation data to a Unix timestamp.
And this is really all we're going to need because we are using an ID, and then we have the created at as well. So, why don't we go ahead and jump into here, and that's what we're going to need. We have two searchable array where we have our ID for this model's ID, as well as the created at timestamp. And now we should be good to go. So, now we can conf- define our Typesense collection schemas in the scout.php config file. Why don't we go there?
So, the scout.php config file, you can see here we need to set our scout driver, and we've already done that within our .env. Scout driver is Typesense, so perfect, that's good to go. And then here's kind of some configuration options within Scout. We have an index prefix that we might want to say, "Okay, we need to search index names used by Scout." This is great if you have multiple tenants.
We already said that our queue data syncing, we're going to set that to false for right now. And then, what we really want is down here. There's a bunch of different options that you can dive into, especially if you're thinking, "Okay, I need to have a little bit more customization on how these are being consumed by Typesense." But really this Typesense configuration block is what we're looking for. Type sense again is an open source search engine using minimal configuration. Here's all of the information that we might need.
This is specifically I'm kind of like our ENV settings that we've already set up. But what we're really looking for is our model settings. So basically being able to set the classes that we need as well as the schema that we would like for our application. If I take a look at our review table, we basically have There's a few things that I think I would want to have searchable.
One would probably be the kind of store address latitude longitude. That's nice to know of like where this exists. The review of course the actual text of the review, but then it'd be nice to have the rating, the review time, and I don't know, maybe even the the category of the review. I don't I don't think the category and store name matter too much because I think that's more generic. I think it's just fast food in McDonald's. So probably store address latitude longitude.
Rating counts probably not as important right now. Maybe it would be. That's more just kind of like for categorizing by. But no, we'll we'll just do review, rating, and I think all of the demographics areas as well. All right, so here's what I kind of put together and we'll kind of walk through to make sure that this is what I intend for. So first off, we don't necessarily need the ID fields at least from what I'm understanding from within the type sense documentation. I want the store address.
I can make that optional as well. We need the rating, the review time, which is going to be a field of when that review was left so we know how long ago it was. And then we have the location which we're going to set as a geo point. And that location, um, I believe we can set it to be latitude and longitude. And so yeah, I believe we can set the location in the two searchable array field in our model.
I can believe we can set our latitude and longitude to basically point to location if I'm understanding everything correctly. But we'll figure that out together. So we have the review and then what I want to do here is go ahead and say, um, I also want to the location which will be the latitude and longitude. And then I'm going to also need, I believe, the review time.
We'll do that timestamp. And then we should also need, I don't believe we need the rating or the store address. But looks like, yeah, it looks like it just timestamps and then IDs here. We are using by any kind of ID string. That's That's my understanding at least.
To be able to say, okay, we want to query by any kind of ID, it has to be made a string in that two searchable array. Or if we have any specific dates.
But I also believe that we could I also think, and this is where we kind of are able to set the location to say, I want to use the latitude and longitude. But the neat thing is if you mess up, you can just flush out your TypeSense data and then import it again using these Scout flush and Scout import PHP Artisan commands. Let's take a look and see if we can just change this to the search method with and our model now that we have TypeSense configured.
So on our index, instead of having this weird review query when where raw or where raw latest paginate all that mess. So what we're just going to do is we're just going to search. So, instead of review query, I'm just going to say review search, and we're just going to say this search, and I think we should be good to go. And then we can just paginate that there.
And then we do get an error saying parameter fields is required. I believe this is just with type sense. And so, what we do is just pass some fields into the search parameter. We can see that.
Um let's take a look. Yeah, we can see that here. I think this query by is what we need to kind of modify that. And so, while I'm using Livewire to demonstrate this, a lot of this is functional across stacks, whether you're using something like Inertia or even just straight blade, you just make a post request with this information. And before we even do that, let's go ahead and actually import all this information using the using the Scout import.
Uh yeah, so we get a fields is required error, but if we go into Scout, we should be able to import our particular model. Let's see. So, PHP artisan Scout import, and then we're going to do app/models/ uh review. I believe that's what it's kind of requesting. Yep. And we get an error. Let's go ahead and see. Parameter fields is required.
Maybe we didn't save that information in our scout.php. We do have fields. Okay, it looks like I might have accidentally deleted the model settings parameter, and I think that's what kind of messed everything. Uh so, now if I go into my warp, and I go into import, we're still getting an error.
Let's see. Malformed UTF-8 characters, possibly incorrectly encoded. All right, I did have a couple more things I needed to fix before I could import those, mostly just be because of how my data was imported from that data set, but this is something that you have to be aware of. All of your data might not be perfect. And so, this is a process to basically say, "Okay, how am I importing this data?" Not necessarily just what's readable by my database, but what's readable by TypeSense.
And so, it was really some of my locations didn't have both the latitude and longitude in my database, and so it was throwing an error because the location needed both a latitude and longitude.
Also, I had some UTF-8 interesting characters in my review and store address. So, I just cleaned those out as I'm importing them. And basically, now within the terminal, we can just flush, basically clear out everything from TypeSense, and then import everything back again. And you can see we're importing all those models up into TypeSense. So, now if we kind of configured everything correctly, we should be good to go to start searching.
We kind of already I believe we already did that within our index.blade.php. We said, "We're just going to search review using this search string, and then paginate." Laravel makes this easy with Scout to say, "Okay, I've already done the setup within TypeSense in the background. Now I just want to actually search." Okay, let's give it a go. Um so, if we go pizza, and yeah, that's looking all good.
Um maybe pizza Orlando because we're searching for the just the review and the location as well. So, yeah, we have like near instant search results and that are more fuzzy than we had before when we were just having to call this, "Hey, is this string like what is in the database?" Now, just TypeSense handles that all for us.
Let me go ahead and say, okay, like maybe um like a McChicken. And I probably misspelled that, but yeah, if we do like McChicken and okay, we have spicy McChicken, McChicken missing chicken. Uh and yeah, we have all this but if I go Mc- McChicken Orlando um or McChicken New York.
So, let's see if we can also do in this review search if we can say we want to do the fields for location as well as reviews. See if we can get that up and running.
So, believe within uh type sense, we have the ability to do Yeah, we can say query by and we can uh dynamically perform as well. I think we had set I can't remember if we in our scout we said, okay, yeah, we're querying by the review and store address. And I don't know if that sets it every time we do the search parameter, but we can also try calling it here as well. So, we have query by within the options parameter.
So, review search and we're just going to say options and in here we're going to say query by {underscore} query by and we're going to go let's do review and what did we call it for our table? Well, so we have review and then also store address. Maybe that's why we're also calling it wrong here. No, probably not. Uh did we call that here store address? We did. Um let's go ahead and see.
So, we have review and store address. Perfect. So, we have that query by and now let's go ahead and see what happens. So, we have uh McChicken and we can say Florida. Okay. Saying five results, but nothing is found. Two results. Okay, McChicken FL.
McChicken Miami. All right, so it's not technically fuzzy search across both fields. There probably is some way to do that within TypeSense's filters. So, like TypeSense for example has a ton of filters and abilities to basically pass in options. And like we kind of showed within our or code, we just kind of pass those in as options within that review search. But you can see here we already set our review to search for the store location as well as the review. So, if I was to search for Miami.
Let's say Miami Beach. That's this is not in like Miami Beach is not Let's see.
Like right here we see this Miami Beach here, but if I was to go in and it's not There's nothing about Miami Beach in the review. It was just for that particular location. And so you can see we have search across both of those columns reasonably without fuzzy with fuzzy search as well without having to do much else. Now, of course we don't have I was kind of testing like and doing like around or near me.
We don't have that ability, but as using simple browser geolocation like a request, we can make that pretty simple. For example, if we were to just do something like this what we could do is say, "Okay, I'm going to set the latitude and longitude Miami just hard code it in and then we have we're just saying, "Okay, we're going to search for all the reviews that are next to location that's 250 km."
I wonder if this does miles as well. Maybe we'll say like 100 miles. Um See that works. Yep. 100 miles and then we're going to sort of buy the review time. So, you can see here the review time date this was 1 hour ago.
That was That was changed and this is all Miami Florida ones. So, we don't have any results from any other locations. So, this is something that would be great for you if you wanted to you know, kind of have the this location search without having to do all the hard work on your end. You automatically know your clients at least your your users client, their client side location if they choose to give it to you or if you do any kind of like Google Maps integration.
Well, then you don't have to figure out all that stuff on the back end. TypeSense just does it for you. There's probably so much more that I'm missing just because there's so much more that TypeSense gives when it comes to even things like AI embedding and kind of similarity searches.
And of course, we see that a little bit with location like geolocation where I didn't have to search the you know, the the actual latitude and longitude of Miami, but because we have the latitude and longitude of all of these particular um places within our database, TypeSense does the hard work of actually figuring out okay, which ones are actually close.
And of course, there probably is some PHP plugins that you can do to make this a little easier, but if you can just wrap it up all in one with specifically within Laravel Scout and just say, "Hey, I just want to search and pass in these options." Well, then TypeSense is perfect for that. Like they say, lightning fast open source search gives you a way to just keep building and keep creating.