Quick tips from the dance floor from a Java specialist, enterprise developer, and mobile technology enthusiast.
I like my ElasticSearch a la Node.js
While ElasticSearch is easy enough to work with via its RESTful HTTP API, there are myriad client libraries available in almost every conceivable programming language. If Node.js is your language of choice, then there’s at least two actively supported libraries available.
My favorite is dubbed, albeit rather dully, ”Elastic Search Client”, but don’t let the library’s unimaginative name fool you: this is a handy library that allows you to do everything you could do via cURL with the added benefit of JavaScript callbacks. Best of all, you can use the Node Elastic Search Client in Coffeescript, which is a handy language that makes JavaScript less verbose and that ultimately compiles into JavaScript.
Accordingly, if you’re familiar with the typical RESTful API calls for creating and mapping indexes, plus indexing and searching documents, then you’ll find Elastic Search Client easy enough to pick up.
To get started, add the library as a dependency in your NPM package.json file like so:
My package.json file that lists the latest version of elasticsearchclient.
Since I happen to prefer CoffeeScript over JavaScript, I’ve also included CoffeeScript as a dependency.
Like any Node library, you’ll need to include a library it via a require statement to make use of it. In this case, I’ll require 'elasticsearchclient' and then connect to a local instance like so:
Going forward, I’m going to reference a few variables, namely indexName, which is “beer_recipes” and objName, which is “beer”. What’s more, this code is using Mocha and should, so that’ll explain the various specification related statements in the examples below.
With a connection to an Elasticsearch server, I can consequently create an index like so:
In this case, the actual mapping JSON document is identical to what I’d have to pass via cURL, for instance.
You should start to notice a pattern with respect to how the Elastic Search Client deals with callbacks – using an on method, you can register a callback for 'data', which essentially entails the response from the server; moreover, you can also register a listener for 'error', which as you can imagine, gets invoked if there is a problem.
Deleting an index is just as easy as creating one too: