Access Your App’s Data On ElasticSearch Via JavaScript (NodeJS & AngularJS)

ElasticSearch is a wavy (wavy = ‘cool’ in my vocabulary) search engine, and it allows you to do full-text based search on your app’s data. (Think about searching for a song on a playlist)

 

First, let’s learn some quick ElasticSearch (ES) terminology:

 

Index: Think of this like a ‘database’. It is simply a container for all similar types (categories) of data. (Example index -> Music albums)

 

Type:  Think of this like a ‘table’. One index can have one, or multiple types. This contains all data of a specific type; (Example -> Index: Music albums, type: Hip hop albums)

 

Document: Think of this like a ‘row’ of data. Each type can contain multiple documents. In our example, one hip hop album (and all its associated information, like artist, region, etc) is stored in one document. Each document is represented as a JSON string.

 

Below are two working examples on how you can connect to our ES cluster and access Hip Hop album data in JSON format
Before we begin, here are some key things to note:

 

    1. Note the URL of the primary node in your ES cluster (example: “http://182.122.39.32:8000”)
      • This is the IP that will be specified in your JavaScript code to connect to the ES cluster and access the Hip Hop album data

 

    1. The index I have used in this example is music-albums’.

 

    1. The type for index ‘music-albums’ is ‘hip-hop-albums’

 

  1. The ‘hip-hop-albums’ type contains ~ 3 million+ documents.

 

Connect to ES cluster and access Hip Hop Album data: 2 working examples
 
Let’s get started: 

 

Server-side access
NodeJS -> Goal: Retrieve all data for hip hop album with id 23729136 in JSON format.
Key files: app.js & elasticsearch.js
  1. In app.js, we ‘require’ the ES JavaScript library (elasticsearch.js).
var elasticsearch = require('./elasticsearch');

 

  2. Open elasticsearch.js and see the files it ‘requires’. Make sure all paths are correct.
function es() {
  throw new Error('Looks like you are expecting the previous "elasticsearch" module. ' +
    'It is now the "es" module. To create a client with this module use ' +
    '`new es.Client(params)`.');
}
es.Client = require('./lib/client');
es.ConnectionPool = require('./lib/connection_pool');
es.Transport = require('./lib/transport');
es.errors = require('./lib/errors');
module.exports = es;

  3. In app.js, we create a new instance of an ES client. Here, specify the URL.
var client = new elasticsearch.Client({
  host: 'http://182.122.39.32:8000',
  log: 'trace'
});
  4. Next in app.js, we specify our query to search for the hip hop album with id 23729136. As you may notice, we specify the index, type (optional), and id value we are interested in
client.search({
  index: 'music-albums',
  type: 'hip-hop-albums',
  body: {
    "query": {
            "ids" : {
                "values" : ["23729136"]
            }
        }
    
  }
})

5. Run app.js. In your terminal, first, ‘cd’ (change directory) into the folder containing your app.js file. Example: ‘cd ESHipHop’. Once you’re in the folder, execute the command ‘node app.js’

Here is a snippet of the JSON response you should get from performing the above query
{
    "took": 739,
    "timed_out": false,
    "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
    },
    "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
        {
          "_index": "music-albums",
          "_type": "hip-hop-albums",
          "_id": "23729136",
          "_score": 1,
          "_source": {
            "album_id": 23729136,
            "album_name": "Views From The 6",
            "artist_firstname": "Aubrey",
            "artist_middlename": "Graham",
            "artist_lastname": "Drake",
            "city": "toronto",
            "features": [
              "The Weeknd"
            ],....cont}


The ‘_source’ JSON object contains all fields (Hip Hop album columns) about the album “Views From The 6”.

Client-side access
AngularJS -> Goal: Retrieve all data for hip hop album with id 23729136 in JSON format.
Here is a general overview of this example: http://plnkr.co/edit/vfapGG?p=info
Key files: index.html & es_connect.js
   1. In es_connect.js we require ‘elasticsearch.angular.js’
var EsConnector = angular.module('EsConnector', ['elasticsearch']);
   2. We are creating an ES service from an esFactory and specifying the url
EsConnector.service('es', function (esFactory) {
  return esFactory({ host: 'http://182.122.39.32:8000' });
});
    3. Next in es_connect.js, we specify our query to search for hip hop album with id 23729136. As you may notice, we specify the index, type (optional), and id value we are interested in
EsConnector.controller('QueryController', function($scope, es) {
// search for documents
    es.search({
    index: 'music-albums',
	type: 'hip-hop-albums',
    body: {
    "query":
        {
            "ids" : {
                    "values" : ["23729136"]
                }  
        },
    }

4. In index.html, we simply bind the JSON response object (‘hits’ is the JSON object containing all relevant hip hop album information) to an HTML element of your choice

		<table>
          <tr ng-repeat="item in hits">
          <td>{{item}}</td></tr>
        </table>

5. Open index.html in your browser.

Here is a snippet of the JSON response you should get from performing the above actions

        {
          "_index": "music-albums",
          "_type": "hip-hop-albums",
          "_id": "23729136",
          "_score": 1,
          "_source": {
            "album_id": 23729136,
            "album_name": "Views From The 6"
            "artist_firstname": "Aubrey",
            "artist_middlename": "Graham",
            "artist_lastname": "Drake",
            "city": "toronto",
            "features": [
              "The Weeknd"
            ],....cont}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s