00:08
ready to index that data inside of our Milysearch database so that we can search against it as documents. Now, we've been talking about indexes and documents thus far, but we haven't really
00:18
explained the difference between the two. Indexes are like collections of documents. It's our tables, essentially.
00:25
And then the documents itself are the rows that we'll search against. So the documents are the actual data that we'll get back as our search results.
00:34
Indexes are the buckets we're going to be searching within. And after we're done with our command here, we'll actually get to see that within Milysearch's UI dashboard to drive home the difference there a little bit further.
00:43
So without further ado, let's go ahead and do node ace make command, and we'll call this command Milysearch underscore index. Hit enter to create that.
00:53
And now if we do node ace, this will list out all of our ace commands. We should see this Milysearch section with a Milysearch colon index command.
01:02
Right now, if we were to run that, so node ace Milysearch index, all it's going to do is print out a hello world message. So we need to fill out what we want that command to do yet.
01:12
Before we do that, however, let's go ahead and also make a service. So node ace make service, and we'll just call this search.
01:20
We'll set up this simple service to be able to perform search and administrative search operations against Milysearch. To aid ourselves with that, there's also a Milysearch JavaScript package that kind of
01:30
gives us an SDK to integrate with the Milysearch API that we have running inside of our terminal.
01:36
So we can do npm i and install the Milysearch package there inside of our project. Perfect. I'm going to go ahead and clear that out. We don't want to run our server quite yet.
01:45
Let's next jump over into our text editor, and we should see two new folders. We have a services folder within our app directory, and we now have a commands folder within the root of our project.
01:54
If we take a look real quick at that command, we'll see that it just simply logs out that message that we saw whenever we ran it. Before we complete this command, though, let's first fill out our service so that we actually
02:04
have something to integrate with that Milysearch package that we've installed. So we're going to treat this as a singleton here. So when we're done with it, what we're going to do is go ahead and create a new instance of it inside of this file.
02:14
So do const search equals new search service, and then we'll go ahead and export default search so that we have a name given so that we can easily import this elsewhere just by
02:23
using and referencing search. This search service is going to house instances of the Milysearch package that we've installed.
02:30
So let's go ahead and import Milysearch from Milysearch, and let's define two different types. We'll type these out as our search key.
02:37
We're going to have one as a literal search and another as admin. These will be the two different instances that we're going to house inside of our search service.
02:46
Admin will be for administrative operations like managing our indexed data and documents, and then search will be for actually performing search operations against that data.
02:55
As you might have assumed, our admin instance is going to connect using our administrative key, and then our search instance is going to connect using our search key that we've set up here inside of our project.
03:05
So we'll just set up a private instances map inside of our search service instance, and the key here is going to be our search key, and the value is going to be an instance of
03:15
Milysearch, and we'll set this up as a new map. And we'll make this similar to other AdonisJS packages.
03:21
So we'll have a use method that accepts in a key that is one of our two search keys.
03:27
We'll have this default to the lesser privileged search key if it is omitted. When we call this use method, what we want to do is check to see whether or not our instances,
03:36
so we'll reach through our instances there, has the key that we're after. So if it has an instance already created, then we just want to go ahead and return that
03:44
instance by getting it out of our map. If our instances map does not have that key yet, then what we want to do is initialize it.
03:52
So we'll create a new private method called init that accepts in the key that we want
03:57
to create as our search key, and then we need to grab the Milysearch API key out of our environment variables. So first, we'll go ahead and check to see whether or not the key that we're after is
04:07
our admin key. If it is, then we'll reach through our environment variables and import that from our startenv to get the Milysearch admin key.
04:15
Otherwise, we'll go ahead and reach through to get our search key. Great. Once we have our API key, what we then want to do is create a new instance of Milysearch.
04:25
So we'll do const, just call this instance equals new, and reach through to Milysearch there. And we need to provide this, our host URL that our Milysearch server there is running
04:34
at, and that's that localhost 7700 that we've seen in previous lessons that we've set up inside of our environment variables as Milysearch host.
04:42
And then we also need to pass through our API key to authorize any requests that we make with this instance.
04:48
Once we have that instance, we can go ahead and add it into our instances.
04:53
So we'll set the key as instance, and then we can return the instance back so that we can use it directly.
05:00
Down inside of our use method now, all that we need to do is return this init and pass that key in to finish this service out.
05:09
So we'll be able to use this service as such. So we've created our new search instance right there that will serve as a singleton by exporting an instance from this file directly.
05:18
We can reach through to search, and then we have our one public use method that then allows us to specify whether we want to perform an administrative operation or a search operation.
05:28
If we do admin, then we have access to an authorized instance to perform any administrative tasks.
05:34
If we use search, we have the same list of methods available, but we're only going to be authorized to perform search-based operations.
05:43
So now that we have our service ready, we can jump into our command to start indexing our data.