00:07
might be to just do the exact same thing that we're doing for our books to also search against our authors and genres. And while that would work, we would get back the results that we're after,
00:16
MeliSearch does actually support multi-search itself. So we can search multiple indexes with a single term and a single request.
00:24
So let's dig back into our text editor here and go back up to our search controller where we're actually performing our search for our books.
00:32
So as I said, you could do the exact same thing here for our authors and genres, just searching those indexes individually.
00:40
That would kick off three separate requests to our MeliSearch API, which what would be better to do is a single request utilizing a multi-search.
00:50
So we get rid of the index specification and go back to just our search authorized instance of MeliSearch and we'll add an object in.
00:58
This object accepts an array of queries. These queries are where we will then specify which indexes we want to search using this term.
01:07
And we actually pass the term into each one of these queries. So if you need to chunk the term up in any other way on a per query basis, you can do that as well.
01:16
So within this array, we wanna provide an object and within this object, we specify the index UID that we want to search against.
01:24
And that is just the index name essentially. So that's where our books, genres, and authors would go. Then we wanna specify the query term. So we just do that via a queue
01:33
and pass the term into there. And previously we were getting back and MeliSearch defaults to 20 results per page for a search. If we're gonna do a multi-search,
01:43
it would be better to limit that to just the items that we're gonna be able to fit within our autocomplete list. So it might make more sense to just limit each one of these to five,
01:52
to get back five books, five genres, and five authors. All right, great. So that would search our books with how we have it currently, but we also wanna search our genres
02:02
and our authors as well. So we'll copy and paste that twice and just switch the index UID to genres for that one and then authors for that one.
02:10
So now we're gonna search all three of our indexes with this single term and a single search. What we'll get back then, and we still have this variable named books,
02:19
is a results array. And if we just poke in real quick to take a look at the first one, you'll see that we still get our paginated information like the estimated total hits for the search term,
02:29
the hits array containing our actual results, how many were included per page, and most importantly, which index UID this particular result set is for. That's how we're going to discern
02:39
whether or not it's for our books, genres, or authors, because now we're just getting all three back as a single array from our results. So we can simplify our variable here a little bit
02:49
by just extracting results out of that object. And then we'll just want to pass the results into our view. Give that a save real quick
02:57
and let's jump back into our autocomplete results. And I'll tell you what, let's nuke our if real quick and just inspect our results inside of our browser just to see exactly what we're getting back
03:07
in a nice simple fashion. So search for hello again. So we can now clearly see that we're getting back an array from our results variable with an object consisting of our index UID of books,
03:17
the hits for those books results. We should get back five there. And each book still consists of the exact same information that we were getting previously, which is all of the indexed information
03:26
that we have for that document. So that's its genres, user, blurb, title, and user ID and ID. And then we also get back the information for the page itself.
03:35
So how many books match that result, the limit that we applied to it, about how long it took, and the query that it was searched against. Then we go into our genres, which we got back zero results for.
03:45
And surprisingly enough, we did get back an author result. I guess Heller is close enough to hello for it to come back. So there we go. The main thing to note about this though
03:54
is that we are still getting back separate hits for our search term per index that we're searching against. If instead you would rather have this all mashed
04:03
into a single array of results, you could utilize a federated search. So if we jump back into our search controller and on our multi-search, if we were to just add federation with an empty object,
04:13
there are additional options that you can do to discern how this should be federated as well as give weights to specific indexes. But real quick, if we just take a look
04:22
at the results that we get back here, we no longer get back a results array, but rather a federated search result. So we'll just give that a save real quick.
04:30
Jump back into our browser, search for hello yet again. And oh yeah, whoops, we have to remove our limit as well to see the results for this because it's no longer searching per index.
04:40
So let's just get rid of the limit for each one of those as well. All right, give that a save one more time and let's try that again. So hello, there we go.
04:48
So now we get back just a single hits array with our federated results consisting of, and here you can see we're actually getting back our author first.
04:57
And then the second result is a book and then the rest are probably gonna be books because we did not get any genres with that search term. But this is just now a single array of results.
05:05
So if you would rather have everything mashed together, you'd wanna use federation options to do that. But this style approach doesn't make a whole lot of sense
05:14
with the three different indexes that we have because a book is very different from an author and the author and book are very different from genres. And we would want to display more information
05:23
for our book versus the authors and genres as well. So we will go ahead and keep these as separate non-federated results for our use case.
05:33
Great, so with this, we do need to switch how we had our auto-complete a little bit. I'm gonna go ahead and go back to get our if with our book hits back, but we do need to switch this up
05:43
because we're no longer getting back books, but results and results is no longer a direct list of hits, but rather an array itself.
05:52
So we need to loop over our results to get an individual result. So we'll wrap our if and an each and we'll loop over results in our results
06:01
and then jump down to our end if and add an end each. In addition to that, we do want our books and our authors and genres to display a little bit differently
06:11
because we also want to display the books, genres, and author as well as its blurb, whereas we don't have any of that for our authors or genres itself. We just have that full name and name.
06:21
So we can do an additional if, our result.indexUID equals books, and then do an else and end the if
06:30
for our authors and genres. So we'll paste our UL back in for our books there within our if, and our variable here is a little bit different.
06:38
So it's no longer books itself, it's now result, and then it's no longer books.hit, but rather result.hit, okay? And then inside of our else,
06:47
we'll go ahead and add in some HTML for our author and genre list. So we'll do a class, maxH300px, overflowY, auto,
06:56
and then we will move on to our li to serve as the title. So we'll do an li class, and I'll tell you what, this is gonna be the exact same as our book title.
07:06
So we can go ahead and just copy the book title, paste it in here, and then rather than labeling this as books, we can just describe a quick let variable as title,
07:15
check to see if the result.indexUID equals authors. If it does, then we'll set this to authors, otherwise we'll set it to genres,
07:24
or you could just use the result.indexUID directly and capitalize the title there itself. Either one works just fine. And then let's go ahead and just copy our each loop
07:34
from our book as well, and paste that just below our li. Replace book with, I'm just gonna name this one hit since it applies for both our authors and genres.
07:43
And rather than any of the li's contents, let's just do a simple div, and then inside of the div, we'll have either the hit.name or the hit.fullName.
07:53
It'll be name if it's a genre and full name if it is an author. You could, of course, also do something similar to what we did up here with our title,
08:00
or do a completely separate if statement for this as well. All right, so that should now have our multi-search results. We have one result listing for our books
08:10
and another for our authors and genres. All right, let's see if I got that right. So let's jump back into our browser, just do a simple search for MA,
08:18
and okay, we get back our books with 237 hits, our authors with five hits, and it looks like we don't have any genres with MA.
08:27
So maybe if we do WE, I know we saw Western. There's Western right there. So now we get back books, genres, and authors. Our genres and authors don't need this much spacing
08:37
between the items because it's just a simple div. So we can alter that a little bit. We'll keep the padding on the X-axis the same, but switch the Y to just two.
08:46
So that should condense it up a good bit right there. So we'll jump back into our browser, just do a search for WE again, and yeah, there we go. Now we're getting all of our results back here on a single page.
08:56
We do still have a little bit of a scroll with our books, could easily do books on the left and genres and authors on the right. Within this autocomplete, it's big enough, but you get the gist of where we're at here
09:06
with our autocomplete and it works nice and well. Furthermore, we have the IDs available for our book, genre, and authors. If we wanted to add a click functionality
09:15
to be able to go into an author dashboard or a genre dashboard that lists all the books within that genre or all the books of that author or the book itself.
09:23
And that ID again, serves as a way to map from our Millie search results and its database back to our database. So the last two things that we have left to cover here
09:33
are how to dynamically add something into our document index, as well as take something out. So if something's deleted or created.