Ranking and Tiebreakers0:03
All right, so let's take some time to talk about ranking just a little bit more, just to be sure that you fully grasp everything that's going on behind the scenes. Okay? So let's do this once again, I'm gonna search for Taylor Atwell's favorite book, Twilight. And you'll see that the first four results are all called Twilight. But of course, different authors, different release states, different stories, but the same name.
But of course, different authors, different release states, different stories, but the same name. Alright, so in these situations we, we basically need a tiebreaker, right? Or in other words, how do we determine among these four what the sort order should be, right? How do we know that this one by Stephanie Meyer should come up first? Well, we can guess just as humans, we know. Well that's the popular one, right?
Well, we can guess just as humans, we know. Well that's the popular one, right? That's the one that, that Taylor read. So that one should come first on the list because it's the most popular and as it turns out, that's what's going on here, right? Alright, so here's what's going on behind the scenes. First up, it's going to take into account your text match. So if I search for fubar, of course twilight is not gonna come up at all.
So if I search for fubar, of course twilight is not gonna come up at all because there's no match. So of course that is first and foremost, the most, uh, the most important thing. Alright? Next up, if you declared a default sorting order when you defined your schema and we did hint, hint, then that will be taken into account. So our default sorting order, I believe, was writings,
that will be taken into account. So our default sorting order, I believe, was writings, counts or maybe average rate. I can't remember which one we did. Maybe writings counts. So basically what that means is, okay, well if you have a tie, and in this case we have a four-way tie for text match, they're all perfectly named twilight. It's not twilight at the end of a sentence, it is the single word.
It's not twilight at the end of a sentence, it is the single word. So it gets the highest text match score. So in these cases, we need a tiebreaker. How do, how do we determine which of these four comes first? Well, we declared ratings count, so it's gonna say, okay, the book that has the most ratings or maybe the highest average rating, if you declared that, well that one should come first. And though it makes sense, right?
well that one should come first. And though it makes sense, right? For books, if I'm looking for twilight, I'm probably looking for the popular one rather than, and I don't know these other ones, but maybe if nobody read these, that's probably not the one you're looking for. So let's make sure we, we take popularity into account Now, otherwise, if there is no default sorting order, it will just use like the document I deem.
otherwise, if there is no default sorting order, it will just use like the document I deem. So recency the one that was inserted, the document that was inserted into your index most recently would come up first by default. That's the way it works. But of course you have full control here, right? You can override these to your heart's content and I'll show you how. Alright, so let's open up our view
Overriding Sort in View2:26
to your heart's content and I'll show you how. Alright, so let's open up our view and yeah, right at the top where we declare our search parameters, I'm gonna add sortBy, and again, notice it's the same as what we were doing within our Laravel controller. On the server side, it's the same thing. So if I wanna override how we sort, then I use sortBy, but here's what I wanna do. I want you to see this side by side so
by, but here's what I wanna do. I want you to see this side by side so that you immediately see the difference. So let's do this, let's pull up our browser and instantly we see it's not super responsive. So let's do this really, really quick. Uh, we'll say maybe for extra large devices we will use a two column grid. Otherwise, uh, let's just do col-span-12. And then right here, uh, same thing, col-span-12
Otherwise, uh, let's just do call-span-12. And then right here, uh, same thing, call-span-12 or call-span-full. Okay? So now if I scroll down, still not great, but we have a little more real estate to work with. Great. Okay, next up, let's scroll up. And once again, I wanna simulate searching for twilight, but I don't wanna have to manually type that in over and over each time we change our code. So here's what I'm gonna do.
and over each time we, we change our code. So here's what I'm gonna do. I will scroll down to our search box and I'm gonna use vm on this and we'll call it searchQuery. And then if I scroll up right here, we will define searchQuery equal to a ref. So now notice if I were to just set a default searchQuery of twilight, it instantly shows up even when we refresh the page.
of twilight, it instantly shows up even when we refresh the page. So yeah, as you can imagine, if you wanna set the initial value of the input equal to, for example, what's in the query string, you could do that using v-model, okay? This is just a little time saver for us to play around. Alright, so once again, I see that the default sort and order is what you would naturally, uh, expect. Stephanie Meyer's book comes up first,
Sorting by TextMatch4:05
and order is what you would naturally, uh, expect. StephanieMeyer's book comes up first, but now we're gonna tweak this. Let's say sortBy is going to be, and I'm gonna use this special string textMatch. Just think of that not as an actual field, but instead, um, sort of like a magic symbol that represents the score for the textMatch, right? Well, what, what was the score? We're gonna sort that so
Well, what, what was the score? We're gonna sort that so that the highest matching score comes up first. So we'll say in descending order. Okay, so yeah, now we see it's a little bit different. This Aaron Hunter book, uh, is coming up first because we basically deleted the default sorting order. We said no, we're not dealing with ratings count right now. I just want you to sort exclusively by text match. So now, okay, well still though, why did this one come up
I just want you to sort exclusively by text match. So now, okay, well still though, why did this one come up before Stephanie Myers? Well, uh, likely because then it falls back to the document insertion order. So in this case, this one probably was inserted later than this one, and we can see that represented by the, uh, the release here. Great. Okay, now let's take it a little further. What if instead we deleted this
Sorting by Publication Year5:07
Great. Okay, now let's take it a little further. What if instead we deleted this and said, well, let's sort by the publication year in descending order. Now if I save it and I scroll down, it's gonna be a little bit different, right? So you'll notice now the fact that twilight as a single word got the highest score. We're not taking that into account. It's still factored in as you can see here,
We're not taking that into account. It's still factored in as you can see here, but we are sorting according to the release year. So that is taking priority, that's, that's a good way to think of it. We are giving priority to the publication year. So this one from 2011, this graphic novel is coming up before the actual book that Taylor Twell read. Okay? So what you could do is combine these. You could say, well, let's start with the text match score.
Chaining Multiple Sort Rules5:49
Okay? So what you could do is combine these. You could say, well, let's start with the text match score. So let's do that one first, but then if there is a tie, I also wanna specify what the tiebreaker should be. So let's save this, scroll down. All right, here's what we have. Notice AaronHunter is first, but then if there's a tie, and in this case there was a four-way tie, these likely have the exact highest score.
and in this case there was a four-way tie, these likely have the exact highest score. So if the, if the score is the same, we move on to the tiebreaker round. Right now, we're gonna say, okay, give precedence to the publication year. And if I save this, now you can see the most, uh, recently published one is coming up first. But if I were to reverse that and say, no, we're gonna do the old ones first.
But if I were to reverse that and say, no, we're gonna do the old ones first. If I scroll down, in that case, we, we start with a textMatch, and then for a tiebreaker, we switch to the oldest book comes first, which likely doesn't make any sense, but just as an example, okay? So as it turns out, we can have a third tiebreaker, and this will often be the case. You start with a textMatch,
and this will often be the case. You start with a text match, and then maybe you sort by something else. And then if there happens to be a tie again, what is the final tiebreaker around? Okay? Maybe what you might do in real life is say, all right, well let's start with a text match, but then we're gonna switch over to ratingsCount. That's, that's really important. If a book has 30,000 ratings, then it's gonna be
That's, that's really important. If a book has 30,000 ratings, then it's gonna be by default more popular than the book that has 12 ratings, right? So if I save this now, if we scroll down, yes, so we have our matches here, but then we switch over to ratings count and yeah, like I said, very possibly ratings count was the default sorting order, which is why we get the same result. But once again, if we wanted to see like, okay, let's,
default sorting order, which is why we get the same result. But once again, if we wanted to see like, okay, let's, let's take the, the least reviewed book, and that one's gonna come up first for some reason, then we can see among the books called twilight, this one seems to be the least popular. Okay, cool. So yeah, here's maybe what you might wanna do in real life is say, okay, let's go buy a text match and then I'm gonna consider ratings count.
let's go buy a text match and then I'm gonna consider ratingsCount. And very likely you will, and this is important by the way, very likely there won't be a tiebreaker because ratingsCount could be 12 or 25,324, right? The likelihood that two books are called twilight and have that ratingsCount, uh, that same rating count is very, very low, which means you would never go to the third round.
that same rating count is very, very low, which means you would never go to the third round. But in situations where you do well, maybe what should be the final sorting order, maybe at that point we give a little weight to the publication year, that might make sense. If the match is the same and they had the exact same ratings count, which is not very likely, then maybe let's get precedence to the publication year.
which is not very likely, then maybe let's get precedence to the publication year. You're probably, if you're searching for it in 2025, you're more likely to wanna find the more recent book than the older book, um, that might be reasonable to consider. All right? You get the idea. So here's the thing for you to remember. If you add something like this and you notice like, Hey, I add the third tiebreaker, but there's no difference, it's always the exact same
and you notice like, Hey, I add the third tiebreaker, but there's no difference, it's always the exact same results, this is a really common thing to run into. And the answer very likely is because once again, for your tiebreaker, there was no second tie. So you never move on to the third rounds. So I'm gonna give you an example using the laracasts website. Let's switch over now. So if I go to the search page and we search for inertia, why don't you hit pause
Real-World Ranking Strategy9:11
Let's switch over now. So if I go to the search page and we search for Inertia, why don't you hit pause and just imagine if you were building this, how would you determine what the sort order should be for programming courses at ricca? And your first thought might be, well just keep it simple. Whatever is the best match for Inertia should come up first, right? But actually that's probably not what you want. If you really think about it, what if the best match
But actually that's probably not what you want. If you really think about it, what if the best match for Inertia is a course that is literally titled Inertia, but the only problem is it was released in 2015 and it focused on the beta version of Inertia. Well, in that case, even though the text match score is the highest, it's not the one you want, right? If there, in this case, this focuses on Inertia 2, this was released more recently, the score maybe isn't as high, but it's, it's the better result for you.
this was released more recently, the score maybe isn't as high, but it's, it's the better result for you. So these are things you have to take into account for your own website. Don't just assume that the best text match is going to be the best result for the user. Other things need to be taken into account. So I'll tell you specifically for LayerCast, here's exactly what we do. We consider the text match score,
here's exactly what we do. We consider the text match score, but then we move on not to like ratings or views, because once again, a, an Inertia course that was released in 2016, that that got so many views back in the day still is probably not the one you're actually interested in. If we refresh that course in 2024 and it doesn't have as many views, that refresh course is still the better
and it doesn't have as many views, that refresh course is still the better one to provide to you. Alright? So once again, we take the text match score into account, but then next we give a lot of weight to the release here. Alright, so here's something I want you to remember for your own projects, I don't just set the created_at timestamp as the time breaker because that timestamp is going to be unique
timestamp as the time breaker because that timestamp is going to be unique and there can be no third tiebreaker at that point. No, no two courses are going to share the same timestamp. So if that's okay for you, then fine, but just keep in mind, there's never gonna be a third tiebreaker. You're gonna start by text match score, and then you're gonna give, um, the highest weight to the most recently, uh, created course.
and then you're gonna give, um, the highest weight to the most recently, uh, created course. And maybe that's okay for you, but it wasn't for us. So what I decided is, okay, I wanna consider the match, I wanna consider the year the course was published in, and then if we have two Inertia courses that were published in the same year, well, for the tiebreaker at that point, yeah, let's give, let's favor the one that has more views. All right, so those, that's our type breaker text match
let's favor the one that has more views. All right, so those, that's our type breaker text match followed by the release year. And in that case, we could have many courses that were released in 2024 or 2023. Now at that point, there are many courses that could tie when it comes to the release year. So for the third tiebreaker, at that point we give weight to views. Whichever one is more popular at
that point we give weight to views. Whichever one is more popular at that point should come up first. Alright, so now I'm gonna leave you with a little cliffhanger for the next episode. If I instead search for Laravel, notice that 30 days to learn Laravel comes up first. However, based upon the short order I just told you about, which was text match followed by release year, followed by, uh, popularity, well based on that algorithm
which was text match followed by release year, followed by, uh, popularity, well based on that algorithm or that short order, this one would not come up first. However, this is a really important course at Ricca and I want it to come up first. If you are new to Laravel, this is your entry point to the framework and lca and I want it to come up first. Okay? So now we can see there are situations where you basically want to override your sort order. There might be situations where you wanna boost
where you basically want to override your sort order. There might be situations where you wanna boost or promote a particular course or a particular book, maybe even if it's just paid by the publisher or the author. That's something you could offer, um, to your clients. And I'm gonna show you how to do that in the next episode.
