This episode is for members only

Sign up to access "Build a Forum with Inertia and Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
48. Indexing discussions for search

Episodes

0%
Your progress
  • Total: 6h 54m
  • Played: 0m
  • Remaining: 6h 54m
Join or sign in to track your progress
01. Introduction and demo
6m 59s
0%
02. Getting set up
10m 36s
0%
03. Modifying registration for usernames
7m 15s
0%
04. Figuring out the forum layout
5m 57s
0%
05. Creating and listing topics
9m 15s
0%
06. Basic discussion listing
13m 33s
0%
07. Pinning discussions
4m 1s
0%
08. Tackling pagination in Inertia
8m 23s
0%
09. Customising pagination text in Laravel
52s
0%
10. Showing a discussion
6m 4s
0%
11. Setting up discussion posts
5m 53s
0%
12. Listing through discussion posts
5m 28s
0%
13. Adding more data to posts
8m 24s
0%
14. Adding pagination to posts
1m 35s
0%
15. Adding a post preview to discussions
4m 52s
0%
16. Adding the last reply to discussions
5m 54s
0%
17. Outputting discussion participants
8m 4s
0%
18. Limiting participants in the UI
5m 56s
0%
19. Ordering discussions by last post
4m 30s
0%
20. Handling deleted users
2m 31s
0%
21. Counting replies
8m 13s
0%
22. Building our first filter
8m 31s
0%
23. Highlighting current filters, and merging with pagination
5m 37s
0%
24. Adding auth specific filters
6m 40s
0%
25. Adding the topic filter
8m 18s
0%
26. Scaffolding the new discussion form
13m 29s
0%
27. Toggling the create discussion form
9m 2s
0%
28. Keeping form state
4m 59s
0%
29. Storing a new discussion
11m 29s
0%
30. Discussion validation and authorization
5m 1s
0%
31. Generating markdown for posts
8m 37s
0%
32. Toggling the markdown preview
7m 43s
0%
33. Fetching and displaying markdown
8m 6s
0%
34. Adding a markdown shortcut toolbar
5m 53s
0%
35. Dealing with SVG icons
7m 46s
0%
36. Creating the reply form
7m 48s
0%
37. Basic Inertia permission checking
6m 26s
0%
38. Creating replies to discussions
5m 37s
0%
39. Jumping to posts
11m 40s
0%
40. Automatically scrolling to posts
6m 18s
0%
41. Toggling post editing
7m 32s
0%
42. Editing posts
3m 36s
0%
43. Deleting posts
4m 21s
0%
44. Deleting discussions
6m 7s
0%
45. Setting up for best answers
7m 29s
0%
46. Toggling the best discussion answer
12m 14s
0%
47. Solved and unsolved filters
2m 23s
0%
48. Indexing discussions for search
8m 6s
0%
49. Searching discussions
12m 35s
0%
50. Debouncing search
2m 47s
0%
51. Adding mentionable functionality to forms
6m 32s
0%
52. Indexing users for mentioning
9m 9s
0%
53. Hooking up users for mentions
10m 10s
0%
54. Detecting and storing mentioned users
9m 54s
0%
55. Adding the mentioned filter
2m 26s
0%
56. Adding mentions to the markdown toolbar
1m 1s
0%
57. Adding mentions to the reply form
7m 21s
0%
58. Fixing up some unauthenticated state
1m 1s
0%
59. Fixing up post scrolling
1m 48s
0%
60. Reviewing SSR (Server-side rendering)
8m 20s
0%
61. Preventing parent posts from being deleted
2m 31s
0%
62. Improving solution marking
4m 9s
0%

Transcript

00:00
Over the next couple of episodes, we're going to look at searching topics. And in this episode specifically, we're going to look at indexing the discussions that we have
00:09
and any future discussions so they can actually be searched. And we'll focus on the search functionality in the next episode. So to actually drive this search, we can use anything that Laravel Scout supports, which we'll talk about in a second.
00:23
But we're going to go ahead and use MeliSearch. It's just a really nice easy way to get this set up. We can do this completely for free on our local machine. So we don't need to really invest in anything to get this working.
00:35
And then later on, if you want to switch this out, Laravel Scout will let you do that. So you could switch over to a third party paid solution if you wanted to. OK, so what is Laravel Scout?
00:47
If you've not come across this before, it's a really convenient way in Laravel to index your models specifically with any of these third party services. So there are lots of different drivers available that you can go ahead and use,
01:01
index that data, and then you can directly use Laravel Scout's search functionality to pull back models that are already fetched out of your database or directly as raw data. So we're going to use this in a very basic way.
01:14
But then, of course, later, you can add this by reading the docs if you have something specific that you need to do. OK, so we're going to head up with the installation section, of course. And first of all, go ahead and pull Laravel Scout into here.
01:26
So let's just pull this into here and wait for that to finish. We can go ahead and publish the Scout configuration file as well. We're not going to be touching that, but you can change things around, like whether you want search indexing to be queued,
01:39
which is recommended, but we're not covering queues specifically in this course. And to use this functionality, it's very easy. We just go ahead and use a searchable trait on any of the models we want to be searchable. So we're going to cover this a little bit more later.
01:55
But for now, we just want our discussion model to be searchable. So we're going to go ahead and pull in the searchable trait. We just re-index things here and let's pull this in from Laravel Scout. So now Laravel Scout is aware that we want to index our discussion model
02:15
and we can search on this as well. So this trait contains all that searchable functionality as well. Now we're going to head down to the driver prerequisite section because we are using MeliSearch.
02:25
Of course, you can use something like Algolia later if you want to. But let's go ahead and just pull in the dependencies that we're going to need for MeliSearch. And then we'll talk about running MeliSearch on our local machine as well. OK, so that's pulled in.
02:39
And the last thing that we need to do is just go ahead and add in our MeliSearch details into our EMV file. So let's go ahead and do that now. And then we'll just talk very quickly about running this on our local machine.
02:51
So the Scout driver is set to MeliSearch. So that's just going to work. This is the MeliSearch host, which is when you're running this locally, pretty much always going to be the same for everyone.
03:00
Local IP and port 7700. And the MeliSearch key, we can just set to master key because we're running this locally. But when this gets to production, you want to generate a master key and have this in your environment variables depending on your environment.
03:17
So what about running MeliSearch locally on our machine, which is what we're actually going to need to do? And we're going to need to keep this running. Well, the documentation will guide you through installing MeliSearch
03:28
depending on your operating system. It's going to vary. I've gone ahead and pulled mine in using Homebrew, which is just a installation manager for Mac.
03:39
But if you're using another operating system or you do things differently, all of the instructions for that will be in the documentation. What you basically want to get to the point of doing is being able to globally run MeliSearch from anywhere.
03:53
And in this case, I'm going to go ahead and run it directly from the project root here. Now, what this will do is it will generate out a data MS, which MS stands for MeliSearch file inside of your root project. So what I would recommend you do if you are doing this is add specifically to your,
04:14
if we just close this off for now, git ignore file, we want to go ahead and ignore data.ms. So I'm going to go ahead and add that in there now so it doesn't get pushed up to source control.
04:25
And I'm going to go ahead and run MeliSearch here. So now that's running, we have a MeliSearch instance running. So if we go up here, let's just have a look at what we've got here. We've got a few notes here.
04:35
We've also got this warning because we have no master key set. That's just because we're working on a local machine. And if we go ahead and open up the server here on port 7700, you'll actually see this little mini dashboard,
04:50
which is really helpful because then you can see what's been indexed within your Laravel application. So once we run a scout command to take all of our discussions that are searchable now and push them to MeliSearch,
05:02
we're actually going to see a discussions index in here, which will allow us to just view what data we're indexing and see which records we're indexing as well. OK, so before we go ahead and index stuff,
05:15
in fact, we can go ahead and just index this now. We'll just take a look at what this does by default. So Laravel Scout gives us a scout import command. And then into this, we just pass in the model that is searchable.
05:27
So in our case, that's app models and discussion. So if we go ahead and run this, that's going to go ahead and import all of the records that we have in that table. And you can see here it's done up to ID 6.
05:39
So now if we head over to our mini dashboard, you can see, sure enough, we've got a discussions index in here. And all of that data has been indexed. That means that now, either from the client side
05:49
or from the back end, we can make a request directly to MeliSearch really, really quickly as well, search for things based on what we're indexing. Now, this is actually including a lot of data.
06:02
And at the moment, at least, really all we want to be searchable is the title of the discussion. And we'll also want to include the ID in there as well. So we have something unique with our index.
06:15
So to customize what gets indexed within MeliSearch because we really don't need everything, we're going to head over to the discussion model here. And let's just do this up here.
06:24
We can create a two searchable array method, which returns the columns or the data more like that we want to index. So we can say ID, this ID. And we can say title, this title.
06:41
So now only these two things are going to get indexed into MeliSearch. We can see that by going ahead and re-importing. Or if we want to go ahead and flush everything out, we can actually use the flush command.
06:52
So app models and discussion. And you'll see that that's now empty. We can go ahead and re-import them. And sure enough, we just now have the data that we need.
07:04
Now for this, we can actually make this a little bit cleaner. We can say this only ID and title. So just on one line now. OK, so we've indexed all past discussions.
07:17
But what about new discussions that we create? Well, they're automatically indexed within MeliSearch because we have that searchable trait over on our discussion. So I'm going to go ahead and create a, this is a new discussion discussion.
07:31
And let's go ahead and post this. What you'll see is this will be bumped from five to six. And it's already in there. Now, likewise, when discussions get deleted,
07:44
they're also going to be automatically removed from our index as well. So all we've done is just add a trait basically and just specify the data that we want. Everything is going to be kept up to date now.
07:55
We don't really need to do much manually here at all. And there we have a list of our discussions indexed, ready to be searched. And that's what we're going to be doing in the next episode.
62 episodes6 hrs 54 mins

Overview

Ready to build a forum with Inertia and Laravel?

Why a forum? A forum touches a whole load of concepts that you'll use throughout your development career – particularly on the client-side, where we'll be doing most of the heavy lifting.

So, let's build a clean, modern forum with features like markdown support, code highlighting, advanced filtering, user mentions, full-text search, the ability to mark best answers, and more.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!