September 202422:0423.7k views

Free, fast, full text search: Laravel + Typesense

About

I set up full-text search for my course website, integrating Laravel Scout with Typesense for fast, efficient searches across video titles and transcripts. I also use Vue.js with Typesense to create a frontend search UI, showcasing the power of full-text search and highlighting features.

Transcript

Everything I said

00:00

Here's the plan for this video. I wanna get full text search going on one of my course websites. Specifically, we're gonna do highperformancesqlite.com. It's a very good course. If you're looking to learn SQLite, please go check it out. What I need to do is give the user the ability to search across all the titles of the videos, but more importantly, across all the transcripts, and the transcripts are many tens of thousands of words.

00:24

I think it's, like, seven, eight, nine hours of content, and so you can imagine that is a lot of words. We're gonna be using Laravel with Typesense here, and Typesense did sponsor this video, so thanks Typesense for keeping my dreams alive. I appreciate that. Um, we're gonna be integrating Typesense with Laravel Scout, and then in the end, with Vue.js.

00:45

The first thing I need to tell you is Typesense is open source. What I'm gonna be showing you here, I didn't pay anything for. I actually didn't even create an account for any of this. I just Brew installed it, and I'm running Typesense locally. So that's kinda nice. The second thing we have to talk about, for goodness sakes, if you load typesense.org, they default to recipes with steam- like steamed mussels.

01:09

Nobody wants that. So, let's go ahead and type in pizza. That's a lot better. Homemade pizza dough, much better than steamed broccoli. So, this is what we're gonna be using. Um, I have already completed this behind the scenes, because, uh, dealing with Homebrew can sometimes take a long time. This actually didn't, but I installed a Brew Typesense server, and then I started the server.

01:32

So it is running here on my Mac. You don't have to do it this way. You could do Docker, Kubernetes. You could, you could do a bunch of different ways. I just did it this way because it was easiest. I will say, if you don't wanna muck around with any of that, they do have Typesense Cloud, which is a hosted version, and that can make your life a lot easier, depending on kinda what you're after.

01:54

Moving on. We have already installed this. We're gonna be using Laravel Scout, and fortunately, Laravel Scout has documentation for Typesense. If you're not familiar with Laravel Scout, it's a first-party package from the Laravel team that adds full text search to your Laravel application, and it is driver-based. So it provides a common interface, and you can use any one of the drivers.

02:17

We're gonna be using the Typesense driver. That is the whole point of this video. So, first thing we're gonna do is we're gonna require Scout and the Typesense, uh, PHP SDK. So we're gonna install both of those things, and then after that, I think what we probably wanna do is we wanna say vendor publish, and we're gonna look for, not Typesense.

02:41

We're actually gonna look for Scout. So we're gonna publish the, uh, scout.php, which is the config file. So, hopping over to PHP Storm, let's look at scout.php, and here we go. There is already a Typesense, um, a Typesense array in here, and so you can see these are the different configurations.

03:03

Algolia, Meilisearch or Meilisearch, I never know. Um, and the default is set to, if we come all the way up here, you'll see that the default is set to Algolia. So, let's hop over into our ENV, and we've already set that to Typesense. So we're good there. And coming back here, and we'll look for Typesense.

03:26

This is the default. So when you, uh, Homebrew, when you Brew install it, it is set up to localhost 8108, and so we should be good to go there. Now, the next question is, how do we get all of our data out of our database into the Typesense indexes? And fortunately, Scout makes this super easy.

03:47

So we're gonna open models video here, and we're gonna say that it is searchable. Eh, that's pretty much all we have to do. We're gonna add a few things here, and so let's say, uh, searchable as, and we're just gonna give this an index name. So we'll say video index. And that, uh, lets us control the name of the index, which we will need on the front end later.

04:11

And this also lets us separate... Let's say we've got, uh, users and documents and videos, and we wanna be able to search them one at a time. That's how we do this. Now, we also need to do toSearchableArray. So what toSearchableArray does is instead of sending the entire video object over, which may con- contain a bunch of columns I don't actually wanna search against, this lets us control what we send to Typesense.

04:39

So here, what we're gonna do is, in fact, let's check the docs, because there's one interesting thing that we have to do here. We have to cast the ID to a string, and we have to cast createdAt to a timestamp. So we're gonna yoink. That's mine now. And whoops, messed that up. And we're gonna put that in...

05:00

Actually, I just want those parts. I wanna do this all myself. So, we're gonna put that in there. That is correct. So, because of the way that Typesense works, you have to cast it to a string and to this, um, this integer instead of a date time object. The only other things that I wanna include, I wanna say, let's do title, and let's do transcript.

05:25

So for transcript, I'm gonna say this, this-transcript, and then I know that it is a JSON, so we're casting transcript to JSON, and then all the good stuff is inside this content key. So that's all, that's all we're sending over. We're sending over the ID, which is required to link up Laravel's eloquent and Typesense.

05:51

We're sending createdAt as an integer to help sort, and then the two things we actually wanna search against are the title and the transcript. Now, this is our, this is kind of our schema, right? This is our searchable schema. However, we need to be more explicit about that. So coming over to scout.php, back in the Typesense, if you scroll down here, you'll see you've got model settings.

06:15

And so what I'm gonna do here is I'm gonna uncomment these, and I'm gonna say, instead of, uh, user, it's actually video. And fields, we have ID as a string, which we just talked about. We have, uh, name as a string, which in our case it's actually title, and we have createdAt as an Int 64, which was that timestamp conversion we did on the carbon object.

06:40

So we're good to go. The only thing we need to add is transcript type string, default sorting createdAt. Query by, we don't have name anymore. Name doesn't make a lot of sense. I'm gonna put title first and then transcript. You can, um, assign different weights, so you can say that a, a hit on title is worth three times as much as a hit on transcript, for example.

07:07

You can add that parameter right down here. I'm not gonna do that right now because according to the documentation, whichever one you put first has the highest weight if the weights aren't specified. Title first, transcript after. Hey, seems pretty good to me. All right, so we're good to go. We're this close to importing all of our data and being able to search.

07:28

We're gonna start by searching on the command line 'cause it's very easy to set up, and then we're gonna muck around on the front end and set up a little bit of a, a little bit of a nice autocomplete. We love a little autocomplete. So before we do that, we have to... Let's look at AR Scout. AR is just my shorthand for PHP Artisan. Here are the scout, uh, options.

07:50

Delete all? No. Delete, flush, import. That looks like the one we want to do. So import a given model. So let's clear this out, AR Scout import, and let's see what we get. Missing model. Perfect. So the model is app, models, video. Now, if we run that, we see 141 videos were imported into Typesense.

08:14

Everything seems to be working so far. It seems too good to be true. Let us, um, let's do composer require Laravel prompts. We're gonna have a little fun here. And there we go. Almost. There we go. Okay, so we have now, we've imported all of our videos, and we've required Laravel prompts.

08:36

So we're- what we're gonna do is, this is m- this is my command where I just do stuff. Um, so we're gonna take search, and we're gonna import, we're gonna import Laravel prompts search. And let's see what we need to do here. Label, options, placeholder. So we're gonna do it like this. We're gonna say label, um, search for a, search for a video.

09:02

Then options. Options takes a closure. So let's make this a little not insane, and we'll say that the closure accepts a string of value. And if, um, let's do if strlen of value is greater than zero, then we're gonna do some kind of search.

09:26

Otherwise, we're gonna return an empty array. So the search that we're gonna do, let's break out here and here. The search that we're gonna do is using Laravel Scout, which under the hood is going to use Typesense. So we're gonna say video search for value. That's the interface. That's it. That's all you have to do. And then we're gonna say get all of those, and then we're gonna pluck.

09:50

I don't remember the order that we're supposed to pluck them in. I think it's title, ID, and then we're gonna turn that into an array. So let's just see if that worked. That would be pretty awesome if that actually worked. Um, so if we run and we do... Actually, I honestly, I'm not gonna lie, I did not expect that to work first time.

10:15

I thought I was gonna have to cut and retake that. Ah. I love it when it works first time. Okay, so, eh, it worked. I'm not super impressed because I'm searching basically by title, and so what if... I, I, I guess I am impressed, but I'm not proving that it's searching by transcript also. So if I type favorite, I remember when I was recording this course talking about how my favorite data transport format is JSON.

10:45

So this makes sense to me that JSON is coming up first even though favorite is not in the title. So we have now, we can go back to JSON, or we could go back to indexes is probably everywhere. And so now if we do that, then there you go. We've got, we've got nonsense because I didn't do anything. So if we did ID equals that and dump out the ID and run this again and do indexes, now that's the ID of the video that we just selected.

11:15

It's very cool so far, right? In a matter of just a few minutes, we've set up Typesense, we've got Laravel Scout running, we imported all of our data, we ran a search, we used Laravel prompts, which is awesome. It's very cool. I think the real use case is not running it on the command line. As much as I just wanna say, "Hey, look, it's cool.

11:35

Awesome. Thanks. See ya," s- come on. Hey, we- people wanna see this on the front end, right? So we have to carry on. And the good news is we're, we're back here on the Typesense documentation right where we left off with the Homebrew installation. But come down here to search UI components. Using InstantSearch JS, the good folks over at Algolia have built an open source set of UI components, React, View, and Angular.

12:01

So there's this interesting thing happening amongst all these search competitors where they're like, "Ooh, let's all do the same front end. We can customize it, whatever, and we'll build our own back ends." Kind of awesome. Makes everyone's lives much easier, so thank you for doing that. If we look, we've got View, or I'm sorry, Algolia Instant Search here. It is a monorepo, so it's got the React, View, and Angular all in here.

12:25

And then we have the Typesense adapter that is Typesense Instant Search adapter. So we're gonna be using both of those things together, and I'll leave links to this down in the description. And so let's hop over here, and we are gonna NPM install the CSS And then TypeScript, that's the adapter, and then vue-instantsearch, because we're using Vue.js locally.

12:47

And if we run that, uh, dah, that worked a lot faster than I expected, and with a lot, a lot fewer, uh, warnings. So now we've got all of that, let's take a look at... We're using, uh, Vue.js, so with vue-instantsearch. I am just gonna yoink, take that, and here is what we've got going so far.

13:12

It's not my best work. Um, I just have the word search written. It's, it's a start. So if we come over here, that is where I wrote search, and I'm just gonna paste all of that in. We'll take a look at, uh, we'll take a look at this in a second. It's not quite right, but first, we need to come over to app.js. So we need to import this instantsearch plugin and then use that in our Vue.js application.

13:37

So I am going to import instantsearch from Vue 3, and then down here, I have createApp, use. I'm using a, uh, inertia plugin, and I'm gonna just chain this on and say use the instantsearch plugin as well. Now we can come back to search, and we can start to clean this up just a little bit. The index name, remember, we set up in video, we set up an index name as searchableAs, and so we're just gonna say searchableAs videoIndex.

14:08

And then there are a few other things I wanna clean up. This is, I'm gonna use xyz, which is just the default key. I think that is a writable key, so if you were doing this in production, you'd want a read-only key. I'm just gonna do that for now. And we have discussed it's localhost:8108. That is fine. And so we're just cleaning a little bit of this up.

14:29

Remember, we want to query by, um, we wanna query by title and then transcript. And remember, we're not, we're not going through Laravel Scout anymore. This is actually gonna hit Typesense directly, and so if you feel like, "Wait, we're duplicating. We already said that on the back end," we did already say that on the back end, but this is skipping Scout altogether and going straight to the Typesense server, which is a lot faster and a lot more flexible, because Scout just

15:00

kind of searches and then returns the model. But here, we wanna highlight the hits. We wanna show people, um, the keywords that they searched for showing up in the transcript. All right. Let's take a look at how we can build up this UI. So if we scroll back up here, you'll see we've got AisInstantSearch, and this is a component provided by that Algolia instant search package, and because we installed it with the Vue.js plugin, these components are just available to us everywhere.

15:26

So you can see, yeah, the IDE is complaining, but these are available to us in all places, and you can then mix and match the components within. So the search box is what actually provides the place where you type, the search box, hmm, and then the hits is, that's where you see all of the results. So I've deleted everything that's in the hits, because I wanna build it up by ourselves together.

15:48

And so what I'm gonna do is I'm just gonna put a template in there, and then we're going to receive the item, which is the hit, and then I'm just gonna show you that we have access to a bunch of stuff, and then we'll do some cool things. The final thing I need to import is import InstantSearchCSSThemesAlgoliaMin. We'll do that one. There are several themes that you can use that are just kinda straight out of the box.

16:12

We're gonna just start with that one. And so now, if we look, we have, we have a search. Um, it's not great, but we're getting closer. So if we say JSON, we see a bunch of stuff's changed. If we say favorite, whoa, we're really lagging, because look at what we're putting into the browser.

16:32

So not a great start. Well, in fact, a great start. Let's keep going, though. Let us put something here. Let's do, um, item.title. So this is the result out of Typesense. So if you're expecting this to match your eloquent model, it doesn't. This is the two searchable array part. So now coming back here, hey, hey, hey, hey, not bad.

16:57

So much better, in fact. So you see we're, we've got 10 results here, um, and if we were to change to, um, let's see, date. Is, did I do anything about dates? There. There's dates down there. So you can see, yeah, we're doing some searching. Now, it's not quite good enough, in my opinion. I think what I wanna do next is I want to, as the user is typing, I wanna highlight the, the words that they're typing in the results.

17:23

So if they type JSON, not only do they see relevant videos, but they see the word JSON highlighted. That's what we've come to know and love. The first thing, before we muck around up here, we need to come down here and send something additional to the server. So we're gonna say highlightFields, and we're just gonna say title and transcript again. So now we're s- we're telling the Typesense server, "Hey, as you're doing this searching, why don't you go ahead and highlight the fields title and transcript against the user's search intent?" We

17:54

can control the cl- opening and closing tag, but it's fine. We're just gonna keep on going. So if we were to do... What do we need to do now? We're gonna use a different component. Instead of just dumping out item.title, we're gonna use this AisHighlight component. And the attribute is title, and the hit, which is the search hit, is this item that we have, um, v-slotted into scope here.

18:22

So now if we come back here and we refresh, we can say JSON, and it worked. That's it. It totally worked. So you can see that the server is sending us back, and I think if we were to inspect this, I think it's marks maybe. Um, yeah, mark. So you can change that. It doesn't have to be mark. You can actually s- change it to be mark class and then put a Tailwind class in there.

18:45

That's totally fine. This, this works well enough. Um, let's go ahead and get the, let's go ahead and get the transcript in there as well. The first thing I wanna do, um, before we do that, is add a little bit of nice stuff here. So we're gonna say font bold, and then we're gonna come down here and we're gonna take that, and we'll say highlight, uh, attribute, ooh, uh, transcript.

19:12

Transcript, and this is, this is gonna be wrong, by the way, so just hang on a second. So now if we look at that, it's, it's, it's right. Um, you know, if I were to say favorite, you see favorite. But over here, it always... Key- keyboard cl- keys clacking. That is how all the videos start. Um, this is not that great, 'cause favorite shows up way down there.

19:35

So instead of highlight for the long guy, we're gonna use snippet. And now that looks a lot better, except this part, but, uh, we'll fix that in a second. So if we say favorite, or if we say JSON, so now we're seeing just the snippet, just the relevant part that was highlighted. Now, we did notice here in the beginning, they all start with keyboard keys, keyboard keys, typing clicks, and then it's the entire transcript.

20:04

There might be a way around this. I know one way around this. We're gonna use, uh, we're gonna use our old friend CSS. So we're just gonna say line clamp two, and this is good also because a snippet might be super long, and I, I just really wanna show them two lines. So look, we got a nice line clamp. We got a nice dot dot dot. If we say favorite, then we have that.

20:25

If we say JSON, lots of JSONs. If we say index, if we say Boolean. It's kind of awesome. I gotta be honest. It's kind of awesome. Um, this is not what the final UI would look like, um, but I didn't wanna s- you know, spend forever f- messing around with CSS, but this is very close. I just need to adjust the CSS now, and I have full text search across all of my transcripts, all of my videos, and of course, in here, in this template, I could change

20:55

these, um, I could change these to be links to link right out to the video. So hopefully that gives you a sense of the power of Laravel Scout plus TypeSense together. We've barely scratched the surface of TypeSense. You know, when you're, when you're on the back end using Laravel Scout, all you're doing is you're getting back your models. So Laravel Scout goes to TypeSense, it does the search, it pulls back the primary keys, and then it pulls the models out of the database, and then you just get your models.

21:21

Like we looked at on the front end, you have a lot more control when you hit directly, when you hit the TypeSense server directly, 'cause then you can do stuff like highlighting matches and all of that. On both sides, you can have, um, fuzzy matches, you can have stop words, you can have minimum words, you can have all kinds of stuff, and I have barely scratched the surface of it all.

21:42

But hopefully that gives you a sense of how quickly you can get full text search up and running on your site. Thank you to TypeSense for letting me do this. Y'all please go tell them thank you. Thank you for supporting me. I'll leave all the links in the description down below. Please go watch some other video that I've made. They're all good. They're all very good. Until the next time, see ya.

Laravel & PHP

More videos on Laravel & PHP

All videos

Get the next one by email

I’ll send you some more. Not too many.

Join 7,500+ other readers.

Read past issues