در حال بارگذاری ...

Downloading Sample Dataset0:03

All right, so now that we have type, since fully installed and ready to go, of course the next step is to perform a search. So let's get going. First we need some kind of data set, right? So here's what we'll do. Let's return to the type sense documentation and down to building a search application. And yeah, you'll see they have a huge book database that's gonna be perfect for our needs.

And yeah, you'll see they have a huge book database that's gonna be perfect for our needs. So we will pull that in and then later we're gonna work with something a little more custom. Alright? So notice it goes to your temp directory and it downloads this, uh, books file and unzips it. All right? So let's copy that, paste it in. All right. And yeah, now we have this books.json file. So if I were to catch, that is really big,

And yeah, now we have this books.json file. So if I were to catch, that is really big, but you can see it is a massive collection of books that consist of a title and authors and ratings and everything that we might want to search or filter upon. Alright, so why don't we do this? Let's move it out of my temp directory into my code/supercharge-search directory. Alright, so now if I switch back to PhpStorm and yep, here's our massive books database, uh, so to speak.

Configuring Typesense Client1:04

Alright, so now if I switch back to PHPStorm and yep, here's our massive books database, uh, so to speak. We're all set. Alright, so now let's go into our routes file and this is a good place to play around and connect to our client. All right, let's get going. So the first step is to create a new client and make sure you pull on the right one. We want typeSense clients. All right? And next we need our configuration.

We want type sense clients. All right? And next we need our configuration. For example, the API key, uh, what is the host? And you'll remember in the last episode, we assigned those to environment variables that you see here. So as you may know, in Laravel it's bad form to, um, access the environment variables directly. Instead, we should go through our config. So let's put this within services and then if I scroll down, I can place it right here.

So let's put this within services and then if I scroll down, I can place it right here. So we're gonna have typeSense and yeah, I'm just gonna reference everything we created. For example, API_KEY will be in the environment typeSense API_KEY. All right, next the HOST and the same thing typeSense host. And then finally, uh, port, right, we'll be environment typeSense port. Okay? So now we can safely, uh, reference those values.

we'll be environment type sense port. Okay? So now we can safely, uh, reference those values. All right, back to our routes file. All right, so now we can get going. Let's declare the API_KEY. And yeah, we're gonna look in config and the services file. The key is type sense, and we want API_KEY. And you know what? Already I can tell we have inconsistent naming here, it's fine, but I'm gonna get tripped up. So I'm just gonna normalize that and we will use snake_case.

naming here, it's fine, but I'm gonna get tripped up. So I'm just gonna normalize that and we will use snake_case. That's fine. Okay, so let's switch back, paste that in, and next we can set up our nodes. And this will be, uh, what's our connection, localhost, what's the port, what's the protocol? So let's do this. The host is going to be config('services.typesense.host'). The port will be config('services.typesense.port'). And let's also do the protocol, uh, http or https.

The port will be services type senses port. And let's also do the protocol, uh, HTTP HTTPS. So why don't we make that configurable by returning to our services file and we'll add another one here for protocol. Yeah, so the entire point of all of this is so that we can easily swap out these values in a local environment versus staging or production. So in my .env file, I will paste this in type senses protocol and we'll set that to HTTP.

I will paste this in type sense protocol and we'll set that to HTTP. That looks good. Alright, back to our routes file. This is looking pretty good. Uh, the last thing I wanna do is set up the connection timeout. So this is what you think it is. How long, uh, before we can't connect, let's set it to 2. And of course, uh, that can be configurable if you want, but I think it's okay for now.

And of course, uh, that can be configurable if you want, but I think it's okay for now. And yeah, we have our client. So let's do this. Uh, before we do anything else, let's dd this object and see what we're working with. So I'll dd the client, and then I'm gonna access our projects homepage in the browser. All right? And sure enough, we have an instance of our typesense client.

Defining Documents and Collections4:06

All right? And sure enough, we have an instance of our typesense client and yeah, we can see it consists of a lot of things. We have the config, we have collections, which you can sort of think of as um, tables. We have aliases and keys. Don't worry about this right now. The important thing is we're up and running. Okay? Alright. So I have two definitions that I want you to memorize. First up is documents. So in the context of typesense, a document you can sort

First up is documents. So in the context of typesense, a document you can sort of think of as a table, row or adjacent object, right? So for a book, that object might consist of the, the author of the book, the title of the book, the release year, things like that. So that collection there, that object in typesense you can think of as a document. Alright? And now your next definition is collection. So once again, in the context of typesense,

Alright? And now your next definition is collection. So once again, in the context of type sense, a collection is sort of like a table. It's synonymous with a database table. A table consists of rows, a collection consists of documents. If you want, it's sort of a one-to-one link there and that will really help you understand it. Okay? Collection table document, table row. Does that make sense? Okay, so let's create our schema,

Creating Books Schema5:14

Okay? Collection table document, table row. Does that make sense? Okay, so let's create our schema, let's create a collection. We are working with books, so I'm gonna call it bookSchema. Okay? So this is standard type sense syntax. Here, we need to give it a name. What is the name of your collection or what is the name of your table? We'll just call it books. That's fine. Next, what are the fields?

We'll just call it books. That's fine. Next, what are the fields? Again, think of it like your date and use all of your database knowledge here. What are the fields in this table or in this collection? Well, what are the things that I want type sense to know about here? Maybe the title, maybe the author, maybe the publication_year. That would be fine. When did it come out?

maybe the publication year. That would be fine. When did it come out? Maybe the ratings count, maybe even things, uh, custom, like the average rating. Yeah, we can, we can dynamically, uh, determine these values and sync them up. And I'll show you a number of ways you can do that in your own projects. Uh, but yeah, things like this are probably what you would want.

Uh, but yeah, things like this are probably what you would want. And if we switch back to our books database here, we can quickly see we have title, we have author, we have publication year, we have average rating. Yeah, we're only going to include things that would make sense to sync up with type sense. All right, things that we might wanna search or filter upon. Okay? So let's get going. Our first field has a name of title

Okay? So let's get going. Our first field has a name of title and we need to give it the type, well, a title is of type string. There we go. Done Next author. Well, if you think about it, a book can be written by more than one author. So in this case, the type is string, but it's really sort of like an array of strings. That's one way to think of it. So here's what we can do.

but it's really sort of like an array of strings. That's one way to think of it. So here's what we can do. Use this common syntax here. Alright, so now it can be one or many strings. Cool. Next publicationYear. So here, it's not gonna be a string, it's going to be effectively a number, right? So we're gonna set the type to int, uh, and 32 bit. All right? Next, the ratingsCount. This also will be int 32.

All right? Next, the ratings count. This also will be int 32. And by the way, there's also int 64, which you might wanna use for timestamps and things like that. There's not many types, so don't worry about this. Okay, and then finally we're gonna have average rating. So let's update this one. And yeah, if you want, you could use um, int 32. You can even use a float, which might make sense. And yeah, this is basically our book, uh, table schema.

You can even use a float, which might make sense. And yeah, this is basically our book, uh, table schema. That's a way to think of it. Okay, so now I want to decide what is our default sorting order. And here's what I mean. If I search these books and we have multiple books that match your search query. For example, if I search for har, well I'm immediately thinking Harry Potter there, right? But maybe there's another book that also begins with HAR, maybe the Harry Truman biography.

But maybe there's another book that also begins with HAR, maybe the Harry Truman biography or something like that, right? So in those situations, like how do we determine the correct sorting order? What is the importance level? What is the logic that determines, uh, when one might come before the other? And yeah, this just depends on your application. So in our case, we might wanna say, well, the number

And yeah, this just depends on your application. So in our case, we might wanna say, well, the number of ratings is most important, or even the average rating. So, uh, the book that has a much higher rating should naturally come before one, that has a horrible rating, right? It's probably not as good and not as likely to be the thing that you're searching for. So yeah, it just depends on, uh, your collection and how you wanna organize things.

So yeah, it just depends on, uh, your collection and how you wanna organize things. I'll give you an example for RAAs, when we have courses that you search on, well, how would you want the default sorting order to be for a programming course? Well, it could be the number of comments in the course. It could be the release year, it could be, um, the number of views the course has received, right? And these are all things that factor into the equation.

of views the course has received, right? And these are all things that factor into the equation. For example, what if a really old Laravel course has so many more views than a new one? Well, it's still not the one that I want to show up first, right? Because it's so old and outdated. So often you will want to take multiple things into account. You wanna consider the release here, but maybe also the number of videos, uh, the number

You wanna consider the release here, but maybe also the number of videos, uh, the number of comments, the number of views, and there are ways to assign weights to each of these components. And I'll show you that later in the course. Okay? So anyways, for now, I'm gonna set the default sorting order. And by the way, it's default because you can override it if you need to, uh,

And by the way, it's default because you can override it if you need to, uh, as part of your search query. But by default, we're gonna sort these by the ratings count. And I think that's fine. And whoops, I'm sorry, not default sorting order. Default sorting field. What is the field that is our default for sorting? Okay, this is looking good. So now that we've effectively defined the schema

Creating the Collection10:05

Okay, this is looking good. So now that we've effectively defined the schema for a collection, let's go ahead and create it. And here's how we already have our client object up here. So we'll say client collections and we're gonna create a new one, nice and readable. Let's pass through our book schema. Finally, I will return done. And yeah, let's give this a run by opening the browser. So we give this a run and we get done.

And yeah, let's give this a run by opening the browser. So we give this a run and we get done. I think we're all set to go here. So let's inspect this. Let's do a quick curl request to ensure that we do have a books collection up and running. And yeah, I'm just gonna paste this in because it's a little bit wordy, but very quickly we're gonna perform a curl request. I'm gonna set the request type to GET, we're gonna once again hit

I'm gonna set the request type to get, we're gonna once again hit that localhost:8108 that we bind to. And then specifically I want the collection/books endpoint. Again, books corresponds to our collection name. So we're going to make that request. But of course, to authenticate or authorize that request, we need to send through our API key.

or authorize that request, we need to send through our API key. So we do that here as a header. So if we give that a run, sure enough we can see all of the schema that we defined. But yeah, if we were to try something else like courses, well that doesn't exist. So we get not found. We're up and running here. And yeah, if you want, you could even save this to a little alias if you want a quick sanity check.

Importing Books Documents11:26

And yeah, if you want, you could even save this to a little alias if you want a quick sanity check to make sure uh, everything was configured properly. Alright, so now that we've defined the schema for our first collection and we've created it, the final step for this video is of course to import our books and then we'll be all set to go. So here's what I'll do. I'm gonna comment this out and I'm gonna keep everything you see here just

So here's what I'll do. I'm gonna comment this out and I'm gonna keep everything you see here just so you can review it in the source code. But yeah, of course if I, I were to hit this endpoint again, we would see an error because we've already created the books collection and we don't have to do that again. Of course, you would only do this once. Alright, so now right down here, here's what I wanna do. I basically want to fetch this file and then import it into type sense.

I basically want to fetch this file and then import it into Typesense. So here's how, let's say file_get_contents. I'm gonna look in our project base path and it's called books.json. Alright, so that's our data. We'll save that to books. If we dd $books, you'll see a massive collection here. That's what we want. All right. Now I can say in our client, go into your collections.

That's what we want. All right. Now I can say in our client, go into your collections. And specifically we want the one for books, right? The one we just created. So grab that collection. All right? And now I effectively want to import a bunch of documents, right? And remember, a document is a synonym for a table. Real, it's just types and speak for a table row. So let's say for the documents, let's import all of these books and then we'll say books imported.

So let's say for the documents, let's import all of these books and then we'll say books imported. Alright? So back to the browser refresh. And sure enough, we get books imported. And that's a wrap for episode three. Okay, so now we've made a little more progress, right? You've learned about collections, you've learned about documents. You've learned how to import a mass of data. In the next episode, we're going to query against that data.

You've learned how to import a mass of data. In the next episode, we're going to query against that data.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟