1.0.3 • Published 5 years ago

egg-elasticsearch-js v1.0.3

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

egg-elasticsearch-js

Install

$ npm i egg-elasticsearch-js --save

Usage

// {app_root}/config/plugin.js
exports.elasticsearch = {
  enable: true,
  package: 'egg-elasticsearch-js',
};

Configuration

Single instance

// {app_root}/config/config.default.js
exports.elasticsearch = {
  client: {
    node: 'host:port'
  }
  // more options: https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/client-configuration.html
};

Multiple instances

exports.elasticsearch = {
  clients: {
    es1: {
      node: 'host1:port',
    },
    es2: {
      node: 'host2:port',
    }
  }
};

see config/config.default.js for more detail.

Example

Single instance

'use strict';

module.exports = app => {
  app.get('/', async function () {
    try {
      const result = await app.elasticsearch.info();
      this.body = result;
    } catch (error) {
      this.status = 500;
      this.body = error;
    }
  });
};

Multiple instances

'use strict';

module.exports = app => {
  app.get('/', async function () {
    try {
      const result1 = await app.elasticsearch.get('es1').info();
      const result2 = await app.elasticsearch.get('es2').info();
      this.body = {result1, result2};
    } catch (error) {
      this.status = 500;
      this.body = error;
    }
  });
};

License

MIT