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

Override Sponsorship Scenario0:00

Alright, welcome back. Now let's move on to overrides. This is pretty cool. So let's invent a little scenario here just for fun. And for the example, notice that this book right here, this is the global phenomenon that Taylor Twell read, but this other one from Aaron Hunter was released just a year after. It was probably a coincidence. Aaron Hunter didn't know that this book right here was gonna become the,

Aaron Hunter didn't know that this book right here was gonna become the, the gigantic one. It's just a coincidence that they share the same title probably. So let's imagine that Erin Hunter is a little annoyed, or her publisher is annoyed and they are willing to pay in order to boost, uh, this book to the top of our result set, right? So they contact us and say, Hey,

to the top of our result set, right? So they contact us and say, Hey, we'll give you X amount of dollars. Just make sure when somebody searches for twilight, our book comes up first. Now, from my angle, it's probably a bad business decision to allow this, but maybe it happens all the time. So what would we do in this case? Well, we're gonna take their money and we're gonna boost this to the top

Creating Playground Route0:52

Well, we're gonna take their money and we're gonna boost this to the top of the list and that'll show you how. So let's open up our routes file. And once again, I'm gonna do this as like a playground, uh, route. In real life you probably wouldn't do that. You might have an artisan command, you might have a little, a little script that you run, or you might have developed a full dashboard ui, um,

a little script that you run, or you might have developed a full dashboard UI, um, to make it easy for non-developers to add these sorts of overrides. Very likely that's what you would do in real life. But nonetheless, a simple route will get us going. Okay, so I'm gonna call this playground and yeah, here I'm basically gonna say, alright, what are the parameters for this override? What am I, what am I targeting?

Finding Target Book ID1:28

what are the parameters for this override? What am I, what am I targeting? Well, I'm gonna target, uh, let's do this. Target a query for twilight. Exactly. Okay, what should we do in that case? Well, we should bump the document with the relevant ID to the top. All right, so we're gonna have to figure out what the ID is. And you know what, why don't we do that right now? Let's go into our view.

And you know what, why don't we do that right now? Let's go into our view. And right down here, here's our hits. Yeah, just very quickly, let's spit out the items id that will be our i, our identifier. Okay, back to the browser. Give it a refresh. And now if I scroll down, okay, this is the one we want. So the version of Twilight with an Idea of 8 3 5 4 is the one we want to boost to the top. Great. Back to our route file.

Defining Override Rule2:17

of 8 3 5 4 is the one we want to boost to the top. Great. Back to our route file. 8 3 5 4. All right, let's get going. So let's generate our override. So we'll have two keys here. We'll have our rule, which is what are you targeting? And then we'll have our includes, which means, well, what are we doing here? Are we, are we moving this particular document to a certain position?

Are we, are we moving this particular document to a certain position? Well, yes we are. So we would add that here. Alright, so our rule is that the query is for twilight. Next. The match is, well, are you looking for any query that contains twilight or does the, the query itself have to be exactly twilight? And this just depends. And you can factor this into the UI you build. Uh, if that's something you do.

into the UI you build. Uh, if that's something you do. In our case we're saying, no, this only, this rule only takes effect if you look, if you search exactly or precisely for twilight. All right, next four includes what are we gonna do here? Well, I'm gonna say on that condition, let's find the record with an ID of 8354. And I'm gonna move that to position number 1. All right. So it, it takes into account your,

And I'm gonna move that to position number one. All right. So it, it takes into account your, your default sorting order and then it reads the overrides and it makes any final adjustments. And in this case, it's gonna basically pluck that, um, less popular version of Twilight and it's gonna move it to position number one. Alright, so now we just have to insert this into our collection. So like we've done before,

Upserting Override in Collection3:42

to insert this into our collection. So like we've done before, let's reference our typeSense clients and then I can say, let's reformat this. There we go. So let's say clientCollections. We are working with the books collection. And I'm gonna add an override and I can use this upsert method. If you're not familiar with upsert, think of it like insert or update.

If you're not familiar with upsert, think of it like insert or update. If it already has been inserted, if there already is a record, then update it. All right? Let's give it a key. Anything you want promote twilight, um, sponsorship, you know, whatever convention you want. And then we reference our override and that's it. So when I hit this route, we declare a override that will promote any search for twilight.

So when I hit this route, we declare a override that will promote any search for twilight and it's gonna grab this less popular book and push it to the top because they paid us for that, for that service. So now we have the override and we will insert that as part of the collection and I'll say, uh, override created If I can type properly. Okay, let's run this in the browser.

Fixing ID String Requirement4:41

If I can type properly. Okay, let's run this in the browser and then refresh our search results. Alright, so before we hit that playground route, I wanna remind you that Aaron Hunter's book is currently in position three and they want it in position one. So let's hit the route playgrounds and oh, it feels inclusion id must be a string. Okay, this is something to remember whenever you're working with type sense,

Okay, this is something to remember whenever you're working with typesense, when you declare your schema or anything like this, ID must always be represented as a string. And in this case, I used a, uh, an integer of course. So it's an easy fix. Reference it as a string and don't forget when you create your schema. And if it's based on an Eloquent model, we'll make sure

and don't forget when you create your schema. And if it's based on an Eloquent model, we'll make sure that eloquentModel id is cast to a string as well before you send it to typesense. Okay, back to the browser. There we go. Override created. So now if I return to search, you're gonna see dun, dun, dun, AaronHunter's version of twilight is promoted to the top result and they paid as well for that privilege. Okay? And that's it. So as you can imagine, you could store things like

Deleting Overrides and Resetting5:45

Okay? And that's it. So as you can imagine, you could store things like this within your database. You could have the overrides as JSON, you could build a little UI around it. And uh, also if you need to delete these things, you could say overrides reference the key, like so, and then you would call delete. And that would delete it. Override delete it. All right, back to the browser. One more time. Playground.

And that would delete it. Override delete it. All right, back to the browser. One more time. Playground. All right, it's gone back to search. And once again, the sponsorship deal has ended, the money is gone. So they return to position number three. Tell me what you think.

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