Skip to content

eggjs-community/egg-elasticsearch

Repository files navigation

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