Advanced TypeSense Configuration0:00
Alright, so now let's quickly touch upon how we can drop down a level, so to speak, when we need to apply, um, advanced configuration options directly to the type sense engine. I'll show you how. So here you can see we are searching courses and let's look for this keyword. We fetch the results and then we compile the results into an eloquent collection. If I switch to the browser, this is what we get, right? Okay, but now let's imagine that, well I wanna search
If I switch to the browser, this is what we get, right? Okay, but now let's imagine that, well I wanna search directly for the category. In this case, we're gonna allow you to search for a category rather than the name or the description. Alright? In that case, let's set the category to languages. But yeah, if I were to come back and give this a refresh, we're not gonna get any results because we're not looking in the category. That's not one of our default fields.
Querying Non-Default Fields0:46
because we're not looking in the category. That's not one of our default fields. So in these situations you have two choices. First up with types sense, there is an options method we could call. So in this case I could say query by, and then we provide, uh, the fields just like before. So in this case, we're gonna query by the category in this case. Alright? And now we get all matching results just as long
by the category in this case. Alright? And now we get all matching results just as long as each course is under the languages category, which is great. Alright, so this is good, but real quick, just as an aside, if you're just trying to do a simple where clause, don't forget there's a where method that Scout provides. So what we could do here is change this out with where. I could say search for whatever it is we're looking for,
So what we could do here is change this out with wear. I could say search for whatever it is we're looking for, but we could say, oh, limit it to only the languages category. Those are the only ones that I care about. So now we effectively get the exact same thing. And yeah, that's definitely something to be aware of and it's probably how I would do it in this case. But yeah, in our case, I wanna talk a little bit more about this options method.
Using Options Search Parameters1:44
But yeah, in our case, I wanna talk a little bit more about this options method. Alright? So you'll remember at the beginning of the course we reviewed search parameters, right? It was an array where we specified things like, uh, query by, we learned about filter by, right? We learned about highlighting, um, lots of different things, right? That search parameters array that we learned about, that's exactly what you have here.
That search parameters array that we learned about, that's exactly what you have here. It's the same thing. So in situations where you need to kind of drop down and really configure how these things are constructed, you can do so. So for example, maybe in this case you want to query by the description only, and that's not the default. Then you could do it. Uh, like, so then if I come back and give it a refresh, um, well, if we were to search for,
Then you could do it. Uh, like, so then if I come back and give it a refresh, um, well, if we were to search for, how about this word here? Now I'm gonna get results only where the description, uh, includes that word, which is here and here, all right? If we wanna do multiple fields, like maybe, uh, we're gonna search through the description as well as the category, then you could do that as well. So that's gonna give you some results.
category, then you could do that as well. So that's gonna give you some results. But also if we look for a dedicated category name, that would show up in the search results as well. Languages, languages, languages. So yeah, you can make this as flexible as you need to. Cool. Uh, let's say you need to filter the results. And again, you need to really drop down a level to have more fine grained control. You could say filter by.
to have more fine grained control. You could say filter by. And then once again, if we were to reproduce what we had before, if we wanna say, well, gimme the ones where the category is equal to. How about tooling? In this case, you've already learned the syntax for this, right? It's the field name followed by a colon, followed by the operator, followed by the value, right? We've learned this many times. So once again,
by the operator, followed by the value, right? We've learned this many times. So once again, this is available to you. Now I'm gonna get only the results, uh, in this case that, uh, are under the tooling category. Or again, if we wanna look for results that were created in the last two months. Again, you could specify all of this here. Okay? And again, if you need a little refresher, go back to the lesson where we discussed filters.
Search Callback for Control3:51
And again, if you need a little refresher, go back to the lesson where we discussed filters. Cool. Now in addition to the options method, which I think most of the time is probably going to satisfy what you need, you can also pass a second argument to the search method. So this really allows you to dig down. It's gonna give you, in this case, access to the typeSense, uh, documents instance. So I'll just call this typeSense.
uh, documents instance. So I'll just call this type sense. It's gonna give you the query you're searching for, and then it's also going to once again give you that options array. So if I were to just dye and dump all of these for you to review, let's have a look anyways, we have our documents instance, then we have our query itself, and then finally the search parameters, array or options.
then we have our query itself, and then finally the search parameters, array or options. And yeah, this is a great place to see what are the default parameters that are being included as part of my query, right? So we can see q that's everything, and that's being, um, pulled dynamically through that search method. What are we querying by? Well, the name by default,
What are we querying by? Well, the name by default, and that is being of course, specified within your config/scout.php file. If I go down to typesense into our model settings, there we go. What are we querying by, by default, just the name. But if I were to change this to description, now it's gonna use the description field to build up, uh, that parameters list.
now it's gonna use the description field to build up, uh, that parameters list. And you can see that updated here. Next, do we have any filter buys? No per page 250. How do I configure that? Well, again, you can treat it just like, uh, an Eloquent query. So imagine you are performing a query against your database and you only want take 20 results. Same thing. So it's, it's a different method.
and you only want take 20 results. Same thing. So it's, it's a different method. It's a different underlying logic, but the API is the same, which is really cool. You just interacted with the way you're already comfortable, uh, interacting with Eloquent. So now if I come back and give it a refresh, now we've updated this and we're only getting 20 per page. Uh, next, what if we wanna configure things like this on the fly?
Uh, next, what if we wanna configure things like this on the fly? Uh, once again, maybe instead of mark tags, we want a span with a class of highlights. Well, you can do that here. So I'll show you. So let's say options or search parameters, um, highlight start tag. And you know what, I bet AI can figure this out for us. There it is, span with a class of highlight and I bet it'll figure out the next part.
There it is, span with a class of highlight and I bet it'll figure out the next part. Amazing, right? Uh, so now we have tweaked the options. The final step is to perform the search. And keep in mind whenever you use this second, uh, argument approach, make sure you return the search results. So you would say return, gimme the TypeSense collection instance. Perform the search and send through the search parameters. Okay, let's have a look. Yep.
Perform the search and send through the search parameters. Okay, let's have a look. Yep. And we get our results like usual. Uh, let's make it a little more clear. Let's look for this word. Come back, refresh. And now we're only going to find, um, where this word is included within the description. But yet now how about, like, what about the highlighting? I want highlighting, but I just have a model instance now, right?
Getting Raw Results Highlights6:49
I want highlighting, but I just have a model instance now, right? I don't have a result from type sense. So how do you deal with situations like that? Well, sometimes you might need to tweak it in two ways. You might need to say, well, give me the results, but then also give me the raw results. So in these situations you could say, not get but raw. Give me the raw response from type sense. Come back, give it a refresh.
Give me the raw response from type Sense. Come back, give it a refresh. And notice now it's not gonna wrap it up in, into Eloquent model instances within a collection, you get the raw results. So yeah, once again, you would have access to your highlights and you would interact with it in the same way that you did before. Or what I would probably do is map over these and maybe I would build up a new searchResults, uh,
Or what I would probably do is map over these and maybe I would build up a new SearchResults class where I just send through whatever I want. Uh, yeah, be creative. Do do what feels natural to you. In this case though, do notice that we have a span tag with a class of highlight rather than the default mark tags. If I were to comment this out, they will return to mark tags as we see right here. Cool. But yeah, I would imagine that in most situations, well, you probably don't need this second argument.
Cool. But yeah, I would imagine that in most situations, well, you probably don't need this second argument unless you really need to drop down and calculate some stuff. Otherwise, most of the time you're just setting search parameters to be honest. And in those cases, you have a dedicated method for that. Provide your search parameters here and you're all set to go.
Provide your search parameters here and you're all set to go. Even for things like, um, maybe a vector query and hint, hint, I'll show you how to do that in an episode or two. You could provide that here. You could say, this is my vector query, and then you provide your embedding. Um, let's format and yeah, that would be how you would handle this.
Finding Search Parameters Docs8:27
Um, let's format and yeah, that would be how you would handle this. So I'm gonna wrap up by showing you where all the search parameters are listed. Have a look of course, if we visit the typesense documentation, you wanna go to API/search and you'll see a section for all of the search parameters and yeah, queryByPrefix. Um, again, most of these you won't use,
and yeah, query by prefix. Um, again, most of these you won't use, but there's VictorQuery that you could reach for. You can even do a voice query. Uh, there, there's so much fun stuff to play around with. Okay, so I think that does it in the next episode. Let's move on.
