Skip to content

Latest commit

 

History

History
116 lines (95 loc) · 2.77 KB

README.md

File metadata and controls

116 lines (95 loc) · 2.77 KB

egg-elasticsearch

NPM version build status Test coverage David deps Known Vulnerabilities npm download

egg plugin for elasticsearch

Install

$ npm i egg-elasticsearch2 --save

Usage

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

Configuration

单实例

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

多实例

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

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

Example

单实例

'use strict';

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

多实例

'use strict';

module.exports = app => {
  app.get('/', function* () {
    try {
      const result1 = yield app.elasticsearch.get('es1').search();
      const result2 = yield app.elasticsearch.get('es2').search();

      this.body = {result1, result2};
    } catch (error) {
      this.status = 500;
      this.body = error;
    }
  });
};

Questions & Suggestions

Please open an issue here.

License

MIT