Elastic Search With Node.JS Part 2

Posted By : Hotam Singh | 30-Jan-2018

In the previous article "Elastic Search With Node.JS Part 1", I had discussed about a brief introduction of elasticsearch, creating an index and deleting an existing index in elasticsearch using Node.JS.

Today, I am going to discuss below points:

  1. Insert single document inside an index
  2. Insert documents in a bulk inside an index
  3. Count documents in index
  4. Delete a document from an index
  5. Delete all documents from an index

For the demonstration purpose, we will be using "etp" index and two types of documents i.e users, blocks. So, let's start with above points one by one.

 

Insert Single Document

Create a new file document_add.js and paste the below code.

client.index({
    index: 'etp',
    id: '1',
    type: 'users',
    body: {
        "username":'hotam',
        "status":1,
        "isDelegate":0,
        "u_isDelegate":0,
        "secondSignature":0,
        "u_secondSignature":0,
        "u_username":null,
        "address":"15546849747111093123E",
        "publicKey":null,
        "secondPublicKey":null,
        "balance":"5000000000000",
        "u_balance":"5000000000000",
        "vote":"0",
        "rate":"0",
        "delegates":null,
        "u_delegates":null,
        "multisignatures":null,
        "u_multisignatures":null,
        "multimin":0,
        "u_multimin":0,
        "multilifetime":0,
        "u_multilifetime":0,
        "blockId":"7807109686729042739",
        "nameexist":0,
        "u_nameexist":0,
        "producedblocks":0,
        "missedblocks":0,
        "fees":"0",
        "rewards":"0",
        "acc_type":0,
        "transferedAmount":"0",
        "endTime":null,
        "totalFrozeAmount":"0",
        "virgin":1
    }
}, function (err, resp, status) {
    console.log('error : ' + err);
    console.log('resp :' + resp);
    console.log('status :' + status);
});

save this file and run using node document_add.js, you will see response like this. You will get status 201, it means document is created successfully.

Note: You can not use index name or type in uppercase letters. If you will use uppaercase letters, you will get an exception as below image. I am using index name ETP insteadof etp. I got this error:

 

Insert Documents in bulk

Create a file document_bulk.js and paste following code:

utils.makeBulk(rows, indexName, function (err, resp) {
    utils.indexall(resp, tableName, function (err) {
        if (err) {
            console.log('err : ' + err);
        } else {
            console.log('bulk insertion successful');
        }
    });
});

Here rows is an array with bulk information and indexName is the name of an index.

Now create one more file utils.js and paste following code:

/**
 * Make bulk data to be saved on elasticsearch server.
 *
 * @param {Object} list
 * @param {Object} bulk
 */
exports.makeBulk = function (list, index, cb) {
    var bulk = [], indexId;
    for (var current in list) {
        if (list[current].id) {
            indexId = list[current].id;
        } else if (list[current].transactionId) {
            indexId = list[current].transactionId;
        } else if (list[current].address) {
            indexId = list[current].address;
        } else {
            indexId = list[current].height;
        }
        bulk.push(
            { index: { _index: index, _type: index, _id: indexId } },
            list[current]
        );
    }
    cb(null, bulk);
};

/**
 * Index data on elasticsearch server.
 *
 * @param {Object} list
 * @param {Object} bulk
 */
exports.indexall = function (bulk, index, cb) {
    esClient.bulk({
        maxRetries: 5,
        index: index,
        type: index,
        body: bulk
    }, function (err, resp, status) {
        if (err) {
            cb(err);
        } else {
            cb(null);
        }
    })
};



 

Count Documents in an Index 

Here we will find total number of documents inside an index. elasticsearch provides count() method to count total number of documents. So, go to the code part and create a new file count.js and paste the following code:

var client = require('./connection.js');

client.count({ index: 'etp', type: 'users' }, function (err, resp, status) {
  console.log("response :", resp);
});

When you run it, you will see something like this

 

Delete a document

Deleting a document is as simple as deleting an index. Elasticsearch provides delete() method that deletes documents from an index.

var client = require('./connection.js');

client.delete({
    index: 'etp',
    id: '1',
    type: 'users'
}, function (err, resp, status) {
    console.log(resp);
});

When you will run this, you will get result as deleted as shown in below image.

Note: If you want to remove more than one documents, do not specify id. You need to specify index name and type only.

 

 

Next

In the next article, we will be discussing searching API of elastic search in details.

About Author

Author Image
Hotam Singh

Hotam has 1.5 years of experience in Node.JS. He has worked on Front End, JavaScript, Jquery, Backbone.JS, Database: MySQL, MongoDB. His hobbies are playing Sudoku/Puzzles and watching movies.

Request for Proposal

Name is required

Comment is required

Sending message..