Elasticsearch provides a full featured client for nodejs available via npm. In this lesson, you will learn to how install the client and use it to retrieve data from your Elasticsearch server. We will walk through retrieving data through callbacks as well as using promises.
[00:00] While using curl is great for poking around the cluster to see what data is available or checking on the health and status of the cluster, when it's time to consume Elasticsearch data in your application though, you're going to need a different approach.
[00:14] Elastic, the company behind Elasticsearch, publishes a low level Elasticsearch client for Node.js and the browser.
[00:22] It has a one-to-one mapping with the REST API and the other official Elasticsearch clients as well as some awareness of core Elasticsearch features like automatic discovery and load balancing. It's built by Elastic. It's been updated recently and frequently and it appears to be well utilized. These are all things I check before deciding to include someone else's code in my application.
[00:46] To get started, I'll install it using NPM. Be sure to include the --save flags, so it's saved to my package.json as a dependency. Once it's installed, I can require it in my client.js file and then I can create a new client using it.
[01:05] Inside the client configuration, I'll provide a couple of options, the location of the host, and I'm going to lock the API version down to 5.0That's because, by default, the Elasticsearch client grabs the latest API version available. I want to prevent future breaking changes of the API from impacting my application.
[01:31] Using that client, I can call the get method and then we'll supply the needed parameters for our get request starting with the index, which is going to be Simpsons. The type is going to be an Episode. The ID that we want is number one.
[01:47] Though the values I provided for the index and the type was data that I already knew from the work I did creating the sample data, but by the end of this course, you're going to learn how to figure these values out for yourself for any dataset on any Elasticsearch cluster.
[02:04] Our get operation provides a callback and will have two parameters, an error and a response. We'll check for that error object and if it exists, we'll write it out to the console. Otherwise, we'll write our response out to the console.
[02:20] I can run that by calling the node command and passing in the name of the file. In response, it looks identical to the response we saw using the curl and HTTP end point. That's because really the client is doing the exact same thing.
[02:34] We have our index and our type, the ID, the version, the found flag, and then the underscore source object with our query results. There's some other parameters available with the get method we can use.
[02:47] I can supply the source exclude parameter and that's an array. I can exclude the video URL from the output. If I save that and then rerun the request, you can see that the video URL is missing or is not returned.
[03:03] I can do the same with the number in the season and the views and any other value that's returned here. Whenever we run that, those results aren't included. We can also use the source include, which provides the opposite feature and just specify the title here. When we run that, the title is the only thing that is recorded or returned.
[03:29] If you're not a fan of callbacks or perhaps you're working with an application that's already using promises instead, the client offers promises as well. To get a promise returned, all we do is omit the callback and then we'll use .then and we can just log that response out. Now when we run this, it's just like we saw in the callback version, it returns the document to us.
[03:55] What happens if we have an error though? Like if we specify an invalid ID. Let's try and see. If you watched a previous lesson, you know that there isn't an episode 999 in the cluster. If I specify that as the ID, when we execute our application, the client really doesn't do anything. It just returns.
[04:15] The client actually threw an error, but we never caught it. Let's take care of that. After our then method, we're going to do a .catch. We'll have an error parameter there. Here we can log out the error that we received. Now if we run that same application with the invalid ID, we get our error message back, which is a 404 not found because that document doesn't exist.
[04:41] One last thing. You can also have inside that .then method, you could catch the error there, but if this gets chained in with other things, that error will actually get swallowed there. Using the .catch method, when you have a bunch of promises chained together, if there's an error in any one of those that gets passed down where it will get caught by the catch method and not get swallowed.