YouTubevideo· Apr 25, 2025· 30:19

What Full-Text Search Looks Like in Laravel

Watch on YouTube ↗

transcript

68 moments
  1. 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 users to search across my whole entire application. So what do you do? 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.

  2. 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.

  3. and here's the reasons why you should just use a Postgres or a SQLite or similar search functionalities within your application. This video is mostly just meant to show you, hey, here's 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

  4. 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 in 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 the database and now you get to figure out what to do with that and it's great to

  5. have that kind of large data information i actually found this one uh that's this mcdonald's store reviews and uh 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

  6. 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 How do we set up TypeSense to take us the rest of the way?

  7. 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 of these McDonald's store reviews. I think there's over 30,000, 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.

  8. But in my code editor, I basically created this review seeder to take that CSV that I downloaded from Kaggle.com. Again, fantastic place if you want to test out some large data sets. But I grabbed 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 say, hey, every time for each one of those row, I'm just going to create a new review model.

  9. 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 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.

  10. 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 a 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.

  11. and I bumped that up. We're just, all we're doing is just searching and we'll, 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 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

  12. I have not seen pizza at McDonald's, but this has got me a little bit interested. And of course, stuff like this works great. Let's say 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

  13. 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, we're grabbing all the reviews and then we're searching for the review and the store name. And

  14. I probably could have done without this. This was just kind of before I only was going to use the McDonald's data set. 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

  15. 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

  16. and I could make sure in my review model or my review migration, I should say, I don't actually have that review 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 still need more, usually, for most search capabilities,

  17. especially if this is a command case 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 database am I thinking through or what kind of information do I want to be sent up to my users in order to search across multiple columns, maybe even multiple tables?

  18. Well, that's where Scout and TypeSense 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?

  19. I love this. They've had multiple iterations, but this is one of my favorites. TypeSense, it's 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

  20. 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

  21. 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 and building your own. If you can take out some of the live demos where they have like basically like recipe search, XKCD comic search, all of these things kind of show you the functionality and capability

  22. 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

  23. 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. We have all of those variables in a McDonald's capability. So we would be able to hook that up as well, saying I want to

  24. 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

  25. see like 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 home page 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

  26. the way in that they, you know, they store all 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 bunch of different options. You You can use brew within macOS, for example.

  27. You can also use Docker. 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.

  28. 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 that 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.

  29. So in my terminal, I'm just going to, I don't need my environment variables yet. 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 review search application. And then we're going to add our environment variables to our .env file. So I'm going to grab those right here.

  30. 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 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

  31. what we need to set up. So when we go back to the Laravel docs, you can see here preparing a data for storage in type sense. Your searchable models must define a two searchable array method that casts your model's primary key to 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

  32. install the typesense php sdk but we have to make sure we install the scout underlying framework for this so composer require laravel scout and 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

  33. 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 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 Q driver, but this is helpful when you are kind of saying, let's say you have a new model, a new,

  34. let's say 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 rows in your database. And so with that, you probably want them to be queued to be sent to your TypeSense driver because you don't want to kind of hold up your application

  35. waiting for all that information to be in sync with type sense 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 doing a review search in 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

  36. helpful to have okay we installed the type sense 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 jumped the gun you know searchable models must define a two searchable array method that casts your model's primary key in this case our id and creation data to a unix timestamp and this

  37. 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 to 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 define our type sense collection schemas in the

  38. scout.php config file. Why don't we go there? So the scout.php config file, you can see here where 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

  39. 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. TypeSense, again, is an open source search engine using minimal configuration. Here's all of the information

  40. that we might need this is specifically kind of like our env settings that we've already set up but what we're really looking for is our model setting 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

  41. 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 category of the review. I don't think the category and store name matter too much because I think

  42. that's more generic. I think it's just fast food and McDonald's. So probably store address latitude, longitude. Rating count's probably not as important right now. Maybe it would be. That's more just kind of like for categorizing by, but no, 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

  43. 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 typesense documentation i want the store address and 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

  44. and then we have the location which we're going to set as a geopoint and that location i believe we can set it to be latitude and longitude and so yeah i believe we can set the location in the to 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

  45. that out together so we have the review and then what i want to do here is go ahead and say i also want 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 time stamp and then we should also need i don't believe we need the

  46. rating or the store address, but it looks like, yeah, it looks like it just timestamps and then IDs here. Query by any kind of ID string. 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 to searchable array, or if we have any specific dates. But I also believe that we could.

  47. I also think 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 type sense 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 within our model now that we have type sense configured.

  48. So in our index, instead of having this weird review query, when we're raw or we're raw, latest, paginating, 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 TypeSense.

  49. And so what we do is just pass some fields into the search parameter. We can see that. 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. So whether you're using something like inertia or even just straight blade, you would just make a post request with this information.

  50. And before we even do that, let's go ahead and actually import all this information using the scout import. 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 slash models slash review. I believe that's what it's kind of requesting. Yep. And we get an error. Let's go ahead and see

  51. parameter fields is required. Maybe we didn't save that information in our scout.php. We have fields. Okay. It looks like I might've accidentally deleted the model settings parameter. and I think that's what kind of messed everything up. So now if I go into my warp and I go into

  52. 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 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,

  53. how am I importing this data? Not necessarily just what's readable by my database, but what's readable by type sense. And so it was really, um, some of my locations didn't have both the latitude and longitude in my database. And so it was thrown in error because the location needed both a latitude and longitude. Also, I had some UTF-8 interesting characters in my review and store

  54. 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.

  55. We said, yeah, we're just going to search review using this search string and then paginate. And 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. So if we go pizza and yeah, that's looking all good.

  56. You can see here maybe pizza Orlando because we're searching for the review and the location as well. So yeah, we have like near instant search results 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

  57. go ahead and say okay like maybe um like 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 if i go mcchicken orlando um or mcchicken new york so let's see if we can also do in this review

  58. search if we can say we want to do the fields for location as well as review see if we can get that up and running so i believe within type sense we have the ability to do you know we can say query by and we can 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,

  59. 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? So we have review and then also store address.

  60. Maybe that's why we're also calling it wrong here. No, probably not. Did we call that here store address? We did. Let's go ahead and see. So we have view and store address. Perfect. So we have that query by and now let's go ahead and see what happens. So we have McChicken and we can say Florida. Okay, same five results, but nothing is found. Two results.

  61. 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 Type Sense's filters. So like Type Sense, for example, has a ton of filters and abilities to basically pass in options. And like we kind of showed within our code, we just kind of passed 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.

  62. So if I was to search for Miami, let's say Miami Beach, that 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 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

  63. columns reasonably without fuzzy search with fuzzy search as well without having to do much else now of course we don't have i was kind of testing like i'm doing like around or near me we don't have that ability but using simple browser geolocation like a request we can make that pretty simple for example uh if we were to just do something like this what we could do is say okay

  64. i'm going to set the latitude and longitude of 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 kilometers i wonder if this does miles as well maybe we'll say like 100 miles um see if that works yep 100 miles uh and then we're going to sort it by the review time so you can see here

  65. the review time date this was one hour ago that was that was changed and this is all miami uh florida one so we don't have any results from any other location 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 client's, at least your user's client,

  66. 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. Type Sense just does it for you. There's probably so much more that I'm missing just because there's so much more that Type Sense 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,

  67. where I didn't have to search the actual latitude and longitude of Miami. But because we have the latitude and longitude of all of these particular 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

  68. wrap it up on one 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.

just you here