@condor-labs/elasticsearch

1.0.7 • Public • Published

This module provide and usefull helper to use Elasticsearch library.

See official documentation here.

Compatibility

The minimum supported version of Node.js is v8.

The library is compatible with all Elasticsearch versions since 5.x

How to use it

To use the library you just need to follow the following steps Install the library with npm

npm install @condor-labs/elasticsearch

Import the library:

const elasticsearch = require('@condor-labs/elasticsearch')(settings);

settings object properties

Property Default Description
protocol (String) http Specify http protocol
host (String) 127.0.0.1 The host name of the ES instance you are connecting to.
port (Number) 9200 The port number to connect to.
user (String) The username for authentication.
password (String) The password for authentication.

Examples

constants.js

module.exports = {
    settings: {
        protocol: 'http',
        user: '',
        password: '',
        host: 'localhost',
        port: 9200
    },
    indexBody: {
        index: 'game-of-thrones',
        type: 'characters',
        body: {
            character: 'Ned Stark',
            quote: 'Winter is coming.'
        }
    },
    searchBody: {
        size: 100,
        from: 0,
        body: {
            query: {
                match: {
                    character: 'Stark'
                }
            }
        }
    }
};

index.js

const {
    settings,
    indexBody,
    searchBody
} = require('./constants');

try {
    const es = require('@condor-labs/elasticsearch')(settings);
    (async () => {
        // get client
        let client = es.getClient();

        //---------------- 
        // Let's start by indexing some data
        //---------------- 
        let resp = await client.index(indexBody)
        // validate response
        console.log(resp && resp.statusCode === 201 ? "Saving document is OK!" : "Saving document didn't work :(");
        console.log('---------------------------------------');

        //---------------- 
        // search data just added:
        //---------------- 

        // promise API
        resp = await client.search(searchBody);

        console.log(resp && resp.body && resp.body.hits && resp.body.hits.total > 0 ? `${resp.body.hits.total} Docs found via Promise!!!` : `NO Docs found via promises :( `);
        console.log('---------------------------------------');

        resp = await client.search(
            searchBody,
            (err, resp) => {
                if (err) {
                    console.error(err)
                }
                console.log(resp && resp.body && resp.body.hits && resp.body.hits.total > 0 ? `${resp.body.hits.total} Docs found via CB!!!` : `NO Docs found via CB :( `);
                console.log('---------------------------------------');
                // close app
                process.exit(1);
            }
        );
    })();
} catch (error) {
    console.error(error)
}

How to Publish

Increasing package version

You will need to update the package.json file placed in the root folder.

identify the property version and increase the right number in plus one.

Login in NPM by console.

 npm login
 [Enter username]
 [Enter password]
 [Enter email]

If all is ok the console will show you something like this : Logged in as USERNAME on https://registry.npmjs.org/.

Uploading a new version

 npm publish --access public

Ref: https://docs.npmjs.com/getting-started/publishing-npm-packages

Note: you will need to have a NPM account, if you don't have one create one here: https://www.npmjs.com/signup

Contributors

The original author and current lead maintainer of this module is the @condor-labs development team.

More about Condorlabs Here.

License

MIT

Dependents (1)

Package Sidebar

Install

npm i @condor-labs/elasticsearch

Weekly Downloads

67

Version

1.0.7

License

MIT

Unpacked Size

13.4 kB

Total Files

6

Last publish

Collaborators

  • daniel.castillo
  • kevin.pedroza_condorlabs.io
  • federico-garcia
  • jorgesanes
  • hjimenez-condorlabs
  • awilches
  • jorgelozano95